-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathID3.cs
More file actions
187 lines (144 loc) · 6.1 KB
/
ID3.cs
File metadata and controls
187 lines (144 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Collections.Generic;
using System.Linq;
namespace AlgorithmsAndDataStructures.DataStructures.DecisionTree;
public class Id3
{
public DecisionTreeNode CreateDecisionTree(
Dictionary<string, string>[] examples,
string targetAttributeName,
Dictionary<string, List<string>> attributes)
{
var dt = new DecisionTreeNode();
if (examples is null) return dt;
if (attributes is null) throw new ArgumentNullException(nameof(attributes));
dt.Examples.AddRange(examples);
if (CheckAllExamplesHaveTheSameAttributeValue(examples, targetAttributeName))
{
var example = examples.First();
var attributeValue = example[targetAttributeName];
dt.TargetAttribute = new KeyValuePair<string, string>(targetAttributeName, attributeValue);
return dt;
}
if (attributes.Count == 0)
{
var attributeValue = FindMostCommonAttributeValue(examples, targetAttributeName);
dt.TargetAttribute = new KeyValuePair<string, string>(targetAttributeName, attributeValue);
return dt;
}
var attributeName = GetBestClassifier(examples, targetAttributeName, attributes);
dt.TestAttributeName = attributeName;
foreach (var attributeValue in attributes[attributeName])
{
var filteredExamples = FilterExamples(examples, attributeName, attributeValue);
if (filteredExamples.Length == 0)
{
var mostCommonAttributeValue = FindMostCommonAttributeValue(examples, targetAttributeName);
var branch = new DecisionTreeNode
{
TargetAttribute =
new KeyValuePair<string, string>(targetAttributeName, mostCommonAttributeValue),
BranchForValue = attributeValue
};
dt.Children.Add(branch);
}
else
{
attributes.Remove(attributeName);
var branch = CreateDecisionTree(filteredExamples, targetAttributeName, attributes);
branch.BranchForValue = attributeValue;
dt.Children.Add(branch);
}
}
return dt;
}
private static bool CheckAllExamplesHaveTheSameAttributeValue(Dictionary<string, string>[] examples,
string targetAttribute)
{
if (examples.Length == 0) return true;
var attributeValue = examples.First()[targetAttribute];
#pragma warning disable HAA0401 // Possible allocation of reference type enumerator
foreach (var example in examples.Skip(1))
#pragma warning restore HAA0401 // Possible allocation of reference type enumerator
if (example[targetAttribute] != attributeValue)
return false;
return true;
}
private static string FindMostCommonAttributeValue(Dictionary<string, string>[] examples, string targetAttribute)
{
var score = new Dictionary<string, int>();
foreach (var example in examples)
{
var attributeValue = example[targetAttribute];
if (!score.ContainsKey(attributeValue)) score[attributeValue] = 0;
score[attributeValue]++;
}
return score
.OrderByDescending(arg => arg.Value)
.First()
.Key;
}
private static Dictionary<string, string>[] FilterExamples(Dictionary<string, string>[] examples,
string targetAttribute, string targetAttributeValue)
{
var filtered = new List<Dictionary<string, string>>();
foreach (var example in examples)
if (example[targetAttribute] == targetAttributeValue)
filtered.Add(example);
return filtered.ToArray();
}
private static float CalculateEntrophy(Dictionary<string, string>[] examples, string targetAttribute)
{
var scores = new Dictionary<string, int>();
foreach (var example in examples)
{
var attributeValue = example[targetAttribute];
if (!scores.ContainsKey(attributeValue)) scores[attributeValue] = 0;
scores[attributeValue] = scores[attributeValue] + 1;
}
float entropy = 0;
foreach (var score in scores)
{
var p = (float)score.Value / examples.Length;
entropy = entropy - p * (float)Math.Log(p, 2);
}
return entropy;
}
private static float CalculateInformationGane(Dictionary<string, string>[] examples, string testAttribute,
string targetAttribute)
{
var groups = new Dictionary<string, List<Dictionary<string, string>>>();
foreach (var example in examples)
{
var attributeValue = example[testAttribute];
if (!groups.ContainsKey(attributeValue)) groups[attributeValue] = new List<Dictionary<string, string>>();
groups[attributeValue].Add(example);
}
var informationGain = CalculateEntrophy(examples, targetAttribute);
foreach (var group in groups)
{
var p = (float)group.Value.Count / examples.Length;
var entropy = CalculateEntrophy(group.Value.ToArray(), targetAttribute);
informationGain -= p * entropy;
}
return informationGain;
}
private static string GetBestClassifier(
Dictionary<string, string>[] examples,
string targetAttributeName,
Dictionary<string, List<string>> attributes)
{
double maxInformationGain = 0;
string result = null;
foreach (var attribute in attributes)
{
var informationGain = CalculateInformationGane(examples, attribute.Key, targetAttributeName);
if (informationGain >= maxInformationGain)
{
maxInformationGain = informationGain;
result = attribute.Key;
}
}
return result;
}
}