-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeNode.c
160 lines (129 loc) · 2.67 KB
/
TreeNode.c
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
#include "TreeNode.h"
#include <math.h>
#define DIVISION_SIDE(data, node) ((data->contAttrs[node->dividingAttr] > node->divisionPoint)?1:0)
float ResultingEntropy(TreeNode *t)
{
int i,j;
int nums[3][NUM_TARGET_CATEGORIES];
int total[3];
Record *r;
float categoryEntropy;
float divisionWeight;
float entropy;
float divisionEntropy;
for (i=0; i<(t->dividingAttribute == Sex)?3:2 ; i++)
{
total[i]=0;
for (j=0; j<= NUM_TARGET_CATEGORIES; j++)
nums[i][j]=0;
}
//this only works for sex
if (t->dividingAttribute == Sex)
{
for (r=t->data; r; r=r->next)
{
nums[r->sex][r->rings]++;
total[r->sex]++;
}
entropy=.0;
for (j=0; j<3; j++)
{
divisionEntropy=.0;
divisionWeight= ((float)total[j])/(total[0]+total[1]+total[2]);
for (i=0; i<NUM_TARGET_CATEGORIES; i++)
if (nums[i]>0)
{
categoryEntropy= ((float) nums[j][i])/total[j];
divisionEntropy+= categoryEntropy * log2(categoryEntropy);
}
entropy+= divisionWeight * divisionEntropy;
}
}
else
{
for (r=t->data; r; r=r->next)
{
nums[DIVISION_SIDE(r, t)][r->rings]++;
total[DIVISION_SIDE(r, t)]++;
}
entropy=.0;
for (j=0; j<2; j++)
{
divisionEntropy=.0;
divisionWeight= ((float)total[j])/(total[0]+total[1]);
for (i=0; i<NUM_TARGET_CATEGORIES; i++)
if (nums[i]>0)
{
categoryEntropy= ((float) nums[j][i])/total[j];
divisionEntropy+= categoryEntropy * log2(categoryEntropy);
}
entropy+= divisionWeight * divisionEntropy;
}
}
return -entropy;
}
float AttributeDivisionPoint(TreeNode *t)
{
int n;
float x;
float y;
float xy;
float xsq;
Record *r;
n=0;
xbar=ybar=xy=xsq=.0;
for (r= t->data->next; r; r= r->next)
{
n++;
x+= data->contAttrs[t->dividingAttribute];
y+= data
}
}
AttrIndex ChooseDividingAttribute(TreeNode *t)
{
int i;
int bestI;
float bestE;
int bestV=0; //the one who is versus all!!!
float entropy;
t->dividingAttribute= bestI= Sex;
bestE= ResultingEntropy(t);
for (i=Sex; i<= ShellWeight; i++)
{
t->dividingAttribute= i;
t->divisionPoint= AttributeDivisionPoint(t);
entropy= ResultingEntropy(t);
if (entropy < bestE)
{
bestE=entropy;
bestI= i;
}
}
return i;
}
/*float Entropy(Record *data)
{
int nums[NUM_TARGET_CATEGORIES];
int total;
int i;
float entropy;
float categoryEntropy;
for (i=0; i<NUM_TARGET_CATEGORIES; i++)
nums[i]=0;
total=0;
for (data=data->next; data; data=data->next)
{
nums[data->rings]++;
total++;
}
entropy=.0;
for (i=0; i<NUM_TARGET_CATEGORIES; i++)
{
if (nums[i]>0)
{
categoryEntropy= ((float) nums[i])/total;
entropy+= categoryEntropy * log2(categoryEntropy);
}
}
return -entropy;
}*/