-
Notifications
You must be signed in to change notification settings - Fork 0
/
DecisionTree.java
164 lines (122 loc) · 4.63 KB
/
DecisionTree.java
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
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
import java.io.Serializable;
import java.util.ArrayList;
import java.text.*;
import java.lang.Math;
public class DecisionTree implements Serializable {
DTNode rootDTNode;
int minSizeDatalist; //minimum number of datapoints that should be present in the dataset so as to initiate a split
// Mention the serialVersionUID explicitly in order to avoid getting errors while deserializing.
public static final long serialVersionUID = 343L;
public DecisionTree(ArrayList<Datum> datalist , int min) {
minSizeDatalist = min;
rootDTNode = (new DTNode()).fillDTNode(datalist);
}
class DTNode implements Serializable{
//Mention the serialVersionUID explicitly in order to avoid getting errors while deserializing.
public static final long serialVersionUID = 438L;
boolean leaf;
int label = -1; // only defined if node is a leaf
int attribute; // only defined if node is not a leaf
double threshold; // only defined if node is not a leaf
DTNode left, right; //the left and right child of a particular node. (null if leaf)
DTNode() {
leaf = true;
threshold = Double.MAX_VALUE;
}
// this method takes in a datalist (ArrayList of type datum). It returns the calling DTNode object
// as the root of a decision tree trained using the datapoints present in the datalist variable and minSizeDatalist.
// Also, KEEP IN MIND that the left and right child of the node correspond to "less than" and "greater than or equal to" threshold
DTNode fillDTNode(ArrayList<Datum> datalist) {
//ADD CODE HERE
return this; //dummy code. Update while completing the assignment.
}
// This is a helper method. Given a datalist, this method returns the label that has the most
// occurrences. In case of a tie it returns the label with the smallest value (numerically) involved in the tie.
int findMajority(ArrayList<Datum> datalist) {
int [] votes = new int[2];
//loop through the data and count the occurrences of datapoints of each label
for (Datum data : datalist)
{
votes[data.y]+=1;
}
if (votes[0] >= votes[1])
return 0;
else
return 1;
}
// This method takes in a datapoint (excluding the label) in the form of an array of type double (Datum.x) and
// returns its corresponding label, as determined by the decision tree
int classifyAtNode(double[] xQuery) {
//ADD CODE HERE
return -1; //dummy code. Update while completing the assignment.
}
//given another DTNode object, this method checks if the tree rooted at the calling DTNode is equal to the tree rooted
//at DTNode object passed as the parameter
public boolean equals(Object dt2)
{
//ADD CODE HERE
return false; //dummy code. Update while completing the assignment.
}
}
//Given a dataset, this returns the entropy of the dataset
double calcEntropy(ArrayList<Datum> datalist) {
double entropy = 0;
double px = 0;
float [] counter= new float[2];
if (datalist.size()==0)
return 0;
double num0 = 0.00000001,num1 = 0.000000001;
//calculates the number of points belonging to each of the labels
for (Datum d : datalist)
{
counter[d.y]+=1;
}
//calculates the entropy using the formula specified in the document
for (int i = 0 ; i< counter.length ; i++)
{
if (counter[i]>0)
{
px = counter[i]/datalist.size();
entropy -= (px*Math.log(px)/Math.log(2));
}
}
return entropy;
}
// given a datapoint (without the label) calls the DTNode.classifyAtNode() on the rootnode of the calling DecisionTree object
int classify(double[] xQuery ) {
return this.rootDTNode.classifyAtNode( xQuery );
}
// Checks the performance of a DecisionTree on a dataset
// This method is provided in case you would like to compare your
// results with the reference values provided in the PDF in the Data
// section of the PDF
String checkPerformance( ArrayList<Datum> datalist) {
DecimalFormat df = new DecimalFormat("0.000");
float total = datalist.size();
float count = 0;
for (int s = 0 ; s < datalist.size() ; s++) {
double[] x = datalist.get(s).x;
int result = datalist.get(s).y;
if (classify(x) != result) {
count = count + 1;
}
}
return df.format((count/total));
}
//Given two DecisionTree objects, this method checks if both the trees are equal by
//calling onto the DTNode.equals() method
public static boolean equals(DecisionTree dt1, DecisionTree dt2)
{
boolean flag = true;
flag = dt1.rootDTNode.equals(dt2.rootDTNode);
return flag;
}
}