- •Аннотация
- •Оглавление
- •Глава 1.Интеллектуальный анализ данных и Data Mining 8
- •Глава 2. Обзор алгоритмов обобщения с «учителем» 29
- •Глава 3. Программная реализация алгоритма cn2 и abcn2 49
- •Глава 4. Тестирование работы алгоритма и исследование полученных результатов 60
- •Введение
- •Глава 1.Интеллектуальный анализ данных и Data Mining
- •1.1. Задачи Data Mining
- •1.1.1. Задача классификации и регрессии
- •1.1.2. Задача кластеризации
- •1.1.3. Задача поиска ассоциативных правил
- •1.2. Модели Data Mining
- •1.3. Методы Data Mining
- •1.4. Общий подход в прогнозирующих методах
- •1.5. Классификация алгоритмов качественного обобщения по признакам
- •1.5.1.Способы представления исходной информации в интеллектуальных системах
- •1.5.2.Варианты реализации систем обобщения
- •1.5.2.1 Обучение "без учителя": основные понятия
- •1.5.2.2. Обучение "с учителем": постановка задачи
- •1.6 Выводы
- •Глава 2. Обзор алгоритмов обобщения с «учителем»
- •2.1.1 Алгоритм древ
- •2.1.2. Описание алгоритма древ
- •2.2. Алгоритм id3(индукция решающих деревьев)
- •2.3 Алгоритм aq
- •2.3.1 Описание алгоритма
- •2.3.2 Псевдокод алгоритма
- •2.4 Алгоритм cn2
- •2.4.1 Описание алгоритма
- •2.4.2 Псевдокод алгоритма
- •2.5 Алгоритм abcn2
- •2.5.1 Особенности аргументированного обучения
- •2.5.2 Аргументирование примеров
- •2.5.3 Оценка условий формируемых правил
- •2.5.4 Алгоритм извлечения продукционных правил
- •2.6 Выводы
- •Глава 3. Программная реализация алгоритма cn2 и abcn2
- •3.1 Постановка задачи
- •3.2 Требования к системе
- •3.3 Среда разработки
- •3.4. Форматы входных и выходных данных
- •3.5 Интерфейс
- •3.6. Ввод информации об атрибутах
- •3.7.Ввод обучающей выборки и построение аргументированных продукционных правил
- •3.8.Получение набора аргументированных продукционных правил
- •3.9.Вкладка «Экзамен»
- •3.10. Выводы
- •Глава 4. Тестирование работы алгоритма и исследование полученных результатов
- •4.1. Исследование правильности построения продукционных правил
- •4.2. Оценка точности классификации примеров(с помощью алгоритма cn2)
- •4.2.1 Результат классификации в задаче monk1(cn2)
- •4.2.2 Результат классификации в задаче monk2(cn2)
- •4.2.3 Результат классификации в задаче monk3(cn2)
- •4.3. Оценка точности классификации примеров (с помощью алгоритма abcn2)
- •4.3.1 Результат классификации в задаче monk1(abcn2)
- •Ниже представлены аналогичные результаты тестирования для задачи monk3.
- •4.3.2 Результат классификации в задаче monk3(abcn2)
- •4.4 Выводы
- •Заключение
- •Список литературы
- •Пространство имён DataStructures
- •Пространство имён uc
- •Пространство имён PercentageConverter.Cs
- •Пространство имён AttrinuteTypesView
- •Пространство имён LearningExamplesView
- •Пространство имён ArguedLearningExamplesView
- •Пространство имён ExamineExamolesView
- •Пространство имён Log
- •Пространство имён NumericUpDown
- •Проект WpfApp
Пространство имён ExamineExamolesView
ExamineExamplesDataGridItem.cs
using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using CN2.Core.DataStructures;
using CN2.UC.LearningExamplesView;
using Brush = System.Drawing.Brush;
using Brushes = System.Drawing.Brushes;
namespace CN2.UC.ExamineExamolesView
{
public class ExamineExamplesDataGridItem: IValidatable
{
public bool IsUse { get; set; }
public List<string> PredictiveAttributeValues { get; set; }
public string DecisiveAttributeValue { get; set; }
public string ExaminedAttributeValue { get; set; }
public string ProductionRule { get; set; }
public SolidColorBrush ExaminedAttributeValueColor
{
get
{
return DecisiveAttributeValue == null || ExaminedAttributeValue == null
? new SolidColorBrush(Colors.Black)
: DecisiveAttributeValue.Equals(ExaminedAttributeValue) ? new SolidColorBrush(Colors.Green) : new SolidColorBrush(Colors.Red);
}
set { }
}
public ExamineExamplesDataGridItem()
{
IsUse = true;
PredictiveAttributeValues = new List<string>();
}
public bool IsValid
{
get { return PredictiveAttributeValues.Count > 0 && !DecisiveAttributeValue.Equals(string.Empty) && !ExaminedAttributeValue.Equals(string.Empty); }
}
public ExamineExamplesDataGridItem(ExaminableExample examinedExample) : this()
{
//todo цикл должен быть по Attributes, а установка Result должна производиться отдельно
foreach (var attributeValue in examinedExample.AllAttributes)
{
PredictiveAttributeValues.Add(attributeValue.Value);
}
DecisiveAttributeValue = examinedExample.DecisiveAttribute.Value;
ExaminedAttributeValue = examinedExample.ExaminedAttribute.Value;
ProductionRule = string.Empty;
//ArguedExample = string.Empty;
}
//public ExamineExamplesDataGridItem(ExaminableExample examinedExample, ProductionRule productionRule, ArguedLearnableExample arguedExample) : this()
public ExamineExamplesDataGridItem(ExaminableExample examinedExample, ProductionRule productionRule) : this()
{
foreach (var attributeValue in examinedExample.AllAttributes)
{
PredictiveAttributeValues.Add(attributeValue.Value);
}
DecisiveAttributeValue = examinedExample.DecisiveAttribute.Value;
ExaminedAttributeValue = examinedExample.ExaminedAttribute.Value;
ProductionRule = productionRule.GetStringValue();
//ArguedExample = arguedExample.BecauseExpression.GetValueString();
}
}
}
ExamineExamplesView.xaml
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:CN2.UC.ExamineExamolesView"
xmlns:NumericUpDown="clr-namespace:CN2.UC.NumericUpDown" x:Class="CN2.UC.ExamineExamolesView.ExamineExamplesView"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<Image x:Key="SaveImage" Source="../../Images/Save_16x.png"/>
<Image x:Key="OpenImage" Source="../../Images/Open_16x.png"/>
<Image x:Key="ExamineImage" Source="../../Images/Examine_16x.png"/>
</UserControl.Resources>
<Grid>
<Menu x:Name="menu" Height="22" VerticalAlignment="Top">
<Button x:Name="buttonLoadExamples" Content="{StaticResource OpenImage}" Height="16" Width="20" Click="buttonLoadExamples_Click" ToolTip="Загрузить примеры из файла"/>
<Button x:Name="buttonSaveExamples" Content="{StaticResource SaveImage}" Height="16" Width="20" Click="buttonSaveExamples_Click" ToolTip="Сохранить примеры в файл"/>
<Label x:Name="labelCoverIndex" Content="# набора:" Padding="0"/>
<NumericUpDown:NumericUpDown x:Name="numericUpDownCoverIndex" MaxValue="0"/>
<Button x:Name="buttonExamine" Content="{StaticResource ExamineImage}" Height="16" Width="20" Click="buttonExamine_Click" ToolTip="Провести экзамен"/>
</Menu>
<DataGrid x:Name="dataGrid" Margin="0,22,0,0" ItemsSource="{Binding items}" AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" CanUserResizeRows="False" RowHeight="24" HeadersVisibility="Column" InitializingNewItem="dataGrid_InitializingNewItem" MouseDown="dataGrid_MouseDown" CellEditEnding="dataGrid_CellEditEnding" GotFocus="dataGrid_GotFocus" CanUserSortColumns="True">
</DataGrid>
</Grid>
</UserControl>
ExamineExamplesView.xaml.sc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;
using CN2.Core.DataStructures;
using Microsoft.Win32;
namespace CN2.UC.ExamineExamolesView
{
/// <summary>
/// Interaction logic for ExamineExamplesView.xaml
/// </summary>
public partial class ExamineExamplesView : UserControl
{
/// <summary>
/// Список типов атрибутов.
/// </summary>
private AttributeTypeSet _attributeTypeSet;
/// <summary>
/// <summary>
/// Индекс типа решающего атрибута в списке типов атрибутов.
/// </summary>
private int _decisiveAttributeIndex;
public int CoverId { get { return numericUpDownCoverIndex.Value - 1; } }
public ExamineExamplesView()
{
InitializeComponent();
}
public void SetAttributeTypes(AttributeTypeSet attributeTypeSet)
{
List<ExamineExamplesDataGridItem> items = new List<ExamineExamplesDataGridItem>();
_attributeTypeSet = attributeTypeSet;
dataGrid.Columns.Clear();
DataGridCheckBoxColumn isUseColumn = new DataGridCheckBoxColumn()
{
Header = "Использовать",
Width = 30,
Binding = new Binding("IsUse")
};
dataGrid.Columns.Add(isUseColumn);
for (int i = 0; i < _attributeTypeSet.PredictiveAttributeTypes.Count; i++)
{
DataGridComboBoxColumn attributeTypeColumn = new DataGridComboBoxColumn()
{
Header = _attributeTypeSet.PredictiveAttributeTypes[i].Name,
Width = 100,
ItemsSource = _attributeTypeSet.PredictiveAttributeTypes[i].Values,
TextBinding = new Binding("PredictiveAttributeValues[" + i + "]")
};
dataGrid.Columns.Add(attributeTypeColumn);
}
DataGridComboBoxColumn decisiveComboBoxColumn = new DataGridComboBoxColumn()
{
Header = _attributeTypeSet.DecisiveAttributeType.Name,
Width = 100,
ItemsSource = _attributeTypeSet.DecisiveAttributeType.Values,
TextBinding = new Binding("DecisiveAttributeValue"),
CellStyle =
new Style(typeof (DataGridCell)) {Setters = {new Setter() {Property = ForegroundProperty, Value = Brushes.Blue}}}
};
dataGrid.Columns.Add(decisiveComboBoxColumn);
DataGridTextColumn examinedColumn = new DataGridTextColumn()
{
Header = "Экзамен",
Width = 100,
Binding = new Binding("ExaminedAttributeValue"),
IsReadOnly = true,
CellStyle = new Style()
{
TargetType = typeof(DataGridCell),
Setters =
{
new Setter(DataGridCell.ForegroundProperty, new Binding("ExaminedAttributeValueColor"))
}
}
};
dataGrid.Columns.Add(examinedColumn);
dataGrid.ItemsSource = items;
//dataGrid.DataContext = items;
dataGrid.Items.Refresh();
}
public void SetArguedAttributeTypes(AttributeTypeSet attributeTypeSet)
{
SetAttributeTypes(attributeTypeSet);
DataGridTextColumn productionRuleColumn = new DataGridTextColumn()
{
Header = "Причина",
Width = 300,
Binding = new Binding("ProductionRule"),
IsReadOnly = true
};
dataGrid.Columns.Add(productionRuleColumn);
}
public void SetExaminedExamples(IEnumerable<ExaminableExample> examinedExamples)
{
List<ExamineExamplesDataGridItem> items = new List<ExamineExamplesDataGridItem>();
foreach (var examinedExample in examinedExamples)
{
items.Add(new ExamineExamplesDataGridItem(examinedExample));
}
dataGrid.ItemsSource = items;
//dataGrid.Items.Refresh();
}
public void SetExaminedExamples(IEnumerable<Tuple<ExaminableExample, ProductionRule>> examinedExamples)
{
List<ExamineExamplesDataGridItem> items = new List<ExamineExamplesDataGridItem>();
foreach (var examinedExample in examinedExamples)
{
items.Add(new ExamineExamplesDataGridItem(examinedExample.Item1, examinedExample.Item2));
}
dataGrid.ItemsSource = items;
}
public void SetCoversCount(int coversCount)
{
numericUpDownCoverIndex.MinValue = 1;
numericUpDownCoverIndex.MaxValue = coversCount;
numericUpDownCoverIndex.Value = 1;
}
public void ResetCoversCount()
{
numericUpDownCoverIndex.MinValue = 0;
numericUpDownCoverIndex.MaxValue = 0;
}
private void dataGrid_InitializingNewItem(object sender, InitializingNewItemEventArgs e)
{
List<ExamineExamplesDataGridItem> items = new List<ExamineExamplesDataGridItem>(dataGrid.ItemsSource.Cast<ExamineExamplesDataGridItem>());
items.Last().PredictiveAttributeValues = new List<string>();
for (int i = 0; i < _attributeTypeSet.PredictiveAttributeTypes.Count; i++)
{
items.Last().PredictiveAttributeValues.Insert(i, string.Empty);
}
items.Last().DecisiveAttributeValue = string.Empty;
items.Last().ExaminedAttributeValue = string.Empty;
}
#region средства для сериализации и десериализации списка примеров
private void buttonLoadExamples_Click(object sender, RoutedEventArgs e)
{
try
{
OpenFileDialog ofd = new OpenFileDialog()
{
Filter = SerializationData.FileDialogFilter,
Title = SerializationData.LoadExamplesFileDialogTitle
};
Nullable<bool> ofdResult = ofd.ShowDialog();
if (ofdResult == true)
{
List<ExamineExamplesDataGridItem> items = new List<ExamineExamplesDataGridItem>();
XDocument examplesXDocument = XDocument.Load(ofd.FileName);
foreach (var examplesXElement in examplesXDocument.Elements())
{
if (examplesXElement.Name.ToString().Equals(SerializationData.ExamplesNode))
{
foreach (var exampleXElement in examplesXElement.Elements())
{
if (exampleXElement.Name.ToString().Equals(SerializationData.ExampleNode))
{
ExamineExamplesDataGridItem item = new ExamineExamplesDataGridItem();
string[] predictiveValues = new string[_attributeTypeSet.PredictiveAttributeTypes.Count];
string decisiveValue = string.Empty;
// признак того, что следует перейти к следующзему примеру
bool isNeedContinue = false;
foreach (var exampleXAttribute in exampleXElement.Attributes())
{
if (exampleXAttribute.Name.ToString().Equals(SerializationData.IsUse))
{
bool isUse;
if (Boolean.TryParse(exampleXAttribute.Value, out isUse))
{
item.IsUse = isUse;
}
else
{
if (OnErrorOccured != null)
{
OnErrorOccured(this, @"Недопустимое значение атрибута ""Использовать"".");
isNeedContinue = true;
break;
}
}
}
}
if (isNeedContinue)
{
continue;
}
foreach (var exampleAttributeXElement in exampleXElement.Elements())
{
int j = -2;
string value = string.Empty;
foreach (var exampleAttributeXAttribute in exampleAttributeXElement.Attributes())
{
switch (exampleAttributeXAttribute.Name.ToString())
{
case SerializationData.ExampleAttributeTypeName:
{
if (_attributeTypeSet.DecisiveAttributeType.Name.Equals(exampleAttributeXAttribute.Value))
{
j = -1;
}
else
{
for (int i = 0; i < _attributeTypeSet.PredictiveAttributeTypes.Count; i++)
{
if (_attributeTypeSet.PredictiveAttributeTypes[i].Name.Equals(exampleAttributeXAttribute.Value))
{
j = i;
break;
}
}
}
if (j == -2)
{
OnErrorOccured(this,
@"Тип атрибута """ + exampleAttributeXAttribute.Value +
@""" не определён в текущем наборе типов атрибутов.");
isNeedContinue = true;
break;
}
}
break;
case SerializationData.ExampleAttrinuteValue:
{
value = exampleAttributeXAttribute.Value;
}
break;
}
if (isNeedContinue)
{
break;
}
}
//todo проверить наличие значения у типа данных и если всё хорошо
if (j == -1)
{
decisiveValue = value;
}
else
{
predictiveValues[j] = value;
}
if (isNeedContinue)
{
break;
}
}
if (isNeedContinue)
{
continue;
}
for (int i = 0; i < _attributeTypeSet.PredictiveAttributeTypes.Count; i++)
{
item.PredictiveAttributeValues.Insert(i, predictiveValues[i]);
}
item.DecisiveAttributeValue = decisiveValue;
item.ExaminedAttributeValue = string.Empty;
items.Add(item);
}
}
}
}
dataGrid.ItemsSource = items;
if (OnFileLoaded != null)
{
OnFileLoaded(sender, "Примеры загружены из файла " + ofd.FileName + ".");
}
}
}
catch (Exception ex)
{
if (OnErrorOccured != null)
{
OnErrorOccured(this, ex.Message);
}
}
}
private void buttonSaveExamples_Click(object sender, RoutedEventArgs e)
{
try
{
SaveFileDialog sfd = new SaveFileDialog()
{
Title = SerializationData.SaveExamplesFileDialogTitle,
Filter = SerializationData.FileDialogFilter,
FileName = "examples"
};
Nullable<bool> sfdResult = sfd.ShowDialog();
if (sfdResult == true)
{
XElement examplesXElement = new XElement(SerializationData.ExamplesNode);
foreach (var example in dataGrid.ItemsSource.Cast<ExamineExamplesDataGridItem>())
{
XElement exampleXElement = new XElement(SerializationData.ExampleNode, new XAttribute(SerializationData.IsUse, example.IsUse));
for (int i = 0; i < _attributeTypeSet.PredictiveAttributeTypes.Count; i++)
{
exampleXElement.Add(new XElement(SerializationData.ExampleAttributeNode,
new XAttribute(SerializationData.ExampleAttributeTypeName, _attributeTypeSet.PredictiveAttributeTypes[i].Name),
new XAttribute(SerializationData.ExampleAttrinuteValue, example.PredictiveAttributeValues[i])));
}
exampleXElement.Add(new XElement(SerializationData.ExampleAttributeNode,
new XAttribute(SerializationData.ExampleAttributeTypeName, _attributeTypeSet.DecisiveAttributeType.Name),
new XAttribute(SerializationData.ExampleAttrinuteValue, example.DecisiveAttributeValue)));
examplesXElement.Add(exampleXElement);
}
new XDocument(examplesXElement).Save(sfd.FileName);
if (OnFileSaved != null)
{
OnFileSaved(sender, "Примеры сохранены в файл " + sfd.FileName + ".");
}
}
}
catch (Exception ex)
{
if (OnErrorOccured != null)
{
OnErrorOccured(this, ex.Message);
}
}
}
#endregion средства для сериализации и десериализации списка примеров
public List<ExaminableExample> GetExamples()
{
List<ExaminableExample> examples = new List<ExaminableExample>();
foreach (var item in dataGrid.ItemsSource.Cast<ExamineExamplesDataGridItem>())
{
if (!item.IsUse)
{
continue;
}
List<AttributeValue> attributes = new List<AttributeValue>();
for (int i = 0; i < _attributeTypeSet.PredictiveAttributeTypes.Count; i++)
{
attributes.Add(new AttributeValue(_attributeTypeSet.PredictiveAttributeTypes[i], item.PredictiveAttributeValues[i]));
}
examples.Add(new ExaminableExample(attributes,
new AttributeValue(_attributeTypeSet.DecisiveAttributeType, item.DecisiveAttributeValue)));
}
return examples;
}
#region обработчики событий элемента управления dataGrid
private void dataGrid_GotFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() == typeof(DataGridCell))
{
dataGrid.BeginEdit(e);
}
}
private void dataGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
dataGrid.CommitEdit();
}
private void dataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
ExamineExamplesDataGridItem item = (ExamineExamplesDataGridItem)e.Row.Item;
if (item.IsValid)
{
e.Row.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0));
}
else
{
e.Row.Foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0));
}
}
private void buttonExamine_Click(object sender, RoutedEventArgs e)
{
if (OnExamine != null)
{
OnExamine(this, null);
}
}
#endregion обработчики событий элемента управления dataGrid
#region события
public delegate void ErrorOccuredHandler(object sender, string message);
public event ErrorOccuredHandler OnErrorOccured;
public delegate void FileSavedHandler(object sender, string message);
public event FileSavedHandler OnFileSaved;
public delegate void FileLoadedHandler(object sender, string message);
public event FileLoadedHandler OnFileLoaded;
public delegate void ExamineHandler(object sender, EventArgs e);
public event ExamineHandler OnExamine;
#endregion события
}
}
