-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
156 lines (137 loc) · 3.62 KB
/
Program.cs
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
// read diabets.csv
using DiabeteProject;
var diabetesData = new List<DiabetesData>();
int id = 0;
using(var reader = new StreamReader("diabetes.csv"))
{
// read data without header
reader.ReadLine();
while(!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
diabetesData.Add(new DiabetesData()
{
Id = id++,
Pregnancies = int.Parse(values[0]),
Glucose = int.Parse(values[1]),
BloodPressure = int.Parse(values[2]),
SkinThickness = int.Parse(values[3]),
Insulin = int.Parse(values[4]),
BMI = double.Parse(values[5]),
DiabetesPedigreeFunction = double.Parse(values[6]),
Age = int.Parse(values[7]),
Outcome = int.Parse(values[8])
});
}
}
const double TrainRate = 0.8;
var trainData = diabetesData.Take((int)(diabetesData.Count * TrainRate)).ToList();
var testData = diabetesData.Skip((int)(diabetesData.Count * TrainRate)).ToList();
var tree = new Tree();
var node = new Node() // can be root or child
{
Data = trainData,
IsUsedAttribute =new bool[8],
};
//var isUsedAttribute = new bool[8];
BuildTree(tree, node);
//tree.PrintTree(node, 10);
tree.PrintPretty(node, "", false);
var correct = 0;
foreach(var data in testData)
{
var result = Predict(node, data);
Console.WriteLine($"Result for {data.Id} is {result} and actual is {data.Outcome}");
if(result == data.Outcome)
{
correct++;
}
}
Console.WriteLine("===================");
Console.WriteLine($"Number of correct predicts : {correct} out of {testData.Count}");
Console.WriteLine("Accuracy: " + (double)correct / testData.Count);
int? Predict(Node node, DiabetesData data)
{
while(node.Outcome == null)
{
var value = double.Parse(data.GetType().GetProperty(node.Attribute.ToString()).GetValue(data).ToString());
if(value < node.SplitValue && node.Left != null)
{
node = node.Left;
}
else if(node.Right != null)
{
node = node.Right;
}
}
return node.Outcome;
}
void BuildTree(Tree tree, Node node)
{
// no attribute left
if(node.IsUsedAttribute.All(x => x == true))
{
var posCount = node.Data.Count(x => x.Outcome == 1);
if(posCount > node.Data.Count / 2)
{
node.Outcome = 1;
}
else
{
node.Outcome = 0;
}
return;
}
// entropy 0
var entropy = tree.CalculateEntropy(node.Data);
if(entropy == 0)
{
node.Outcome = diabetesData[0].Outcome;
return;
}
var bestAttribute = tree.GetBestAttribute(node.Data, node.IsUsedAttribute);
// TODO: get average of current or all???
var average = node.Data.Average(x => double.Parse(x.GetType().GetProperty(bestAttribute.ToString()).GetValue(x).ToString()));
var left = node.Data.Where(x => double.Parse(x.GetType().GetProperty(bestAttribute.ToString()).GetValue(x).ToString()) < average).ToList();
var right = node.Data.Where(x => double.Parse(x.GetType().GetProperty(bestAttribute.ToString()).GetValue(x).ToString()) >= average).ToList();
node.IsUsedAttribute[(int)bestAttribute] = true;
node.Attribute = bestAttribute;
node.SplitValue = average;
//node.Average = average;
var leftIsUsedAttribute = CreateDeepCopy(node.IsUsedAttribute);
var rightIsUsedAttribute = CreateDeepCopy(node.IsUsedAttribute);
if(left.Count > 0)
{
node.Left = new Node()
{
Data = left,
IsUsedAttribute = leftIsUsedAttribute,
};
}
if(right.Count > 0)
{
node.Right = new Node()
{
Data = right,
IsUsedAttribute = rightIsUsedAttribute,
};
}
if(node.Left != null)
{
BuildTree(tree, node.Left);
}
if(node.Right != null)
{
BuildTree(tree, node.Right);
}
}
bool[] CreateDeepCopy(bool[] isUsedAttribute)
{
var result = new bool[8];
for(int i = 0; i < isUsedAttribute.Length; i++)
{
result[i] = isUsedAttribute[i];
}
return result;
}