-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
125 lines (119 loc) · 2.45 KB
/
Copy pathNode.java
File metadata and controls
125 lines (119 loc) · 2.45 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
import java.util.ArrayList;
public class Node {
ArrayList<Node> childnode = new ArrayList<Node>();
int attributeIndex=-1;//if =-1 by default, leaf node
int count[] = {0};// count for each class index domain value
int classIndex;
int classLable = -1;//by majority
boolean hasSetCount = false;
boolean isHomogeneous = false;
void addChild(Node c)
{
this.childnode.add(c);
}
void resetChild()
{
this.childnode = new ArrayList<Node>();
}
public void setCount(DataSet set) throws Exception
{
classIndex = set.attributes.classIndex;
count = new int[set.attributes.getClassAttribute().domain.size()];
for(int i = 0;i<set.examples.size();i++)
{
int temp = set.examples.get(i).get(classIndex).intValue();
count[temp]++;
}
hasSetCount = true;
//isHomogeneous
int c =0;
for(int i=0;i<count.length;i++)
{
if(count[i]>0) c++;
}
if(c>1)
isHomogeneous = false;
else isHomogeneous = true;
getClassLable();
}
boolean isHomogeneous()
{
if(!hasSetCount)//when building each non-root node, will set count immediately.
//so if hasSetCount = false, means it is the root node
{
return false;
}
int c =0;
for(int i=0;i<count.length;i++)
{
if(count[i]>0) c++;
}
if(c>1)
return false;
else return true;
}
int getClassLable()
{
int maxcount = 0;
for(int i=0;i<count.length;i++)
{
if(count[i]>maxcount)
{
maxcount = count[i];
classLable = i;
}
}
//System.out.println(classLable);
return classLable;
}
void setClassLable(int a)
{
this.classLable = a;
}
int getTotalNumber() //get n
{
int amount=0;
for(int i=0;i<count.length;i++)
{
amount += count[i];
}
return amount;
}
int getX()
{
int x = 0;
for(int i=0;i<count.length;i++)
{
if(i==classLable) continue;
else
x += count[i];
}
return x;
}
Double solveforP(Double z)
{
Double p = 0.0;
Double up1,up2,down;
int n = getTotalNumber();
if(n == 0) return 0.0;
int x = getX();
up1 = x + 0.5 + z*z/2;
up2 = (x+0.5)*(1-(x+0.5)/(double)n)+z*z/4;
up2 *= z*z;
up2 = java.lang.Math.sqrt(up2);
down = n+z*z;
p = (up1+up2)/down;
// System.out.println("solving "+up2+" "+down);
return p;
}
Double getAllChildP(Double z)
{
Double p = 0.0;
if(this.childnode.size()==0)
return Double.NEGATIVE_INFINITY;//if leaf node, child p always smaller, means don't prune
else
for(int i=0;i<this.childnode.size();i++)
p += this.childnode.get(i).solveforP(z);
return p;
}
}