-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecisionTree.java
More file actions
311 lines (296 loc) · 11.4 KB
/
DecisionTree.java
File metadata and controls
311 lines (296 loc) · 11.4 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import java.util.*;
import java.util.function.Function;
import java.util.stream.*;
public class DecisionTree {
private Node root;
private int numFeatures;
private List<String> colName;
private EncodeData EncodeData;
private double trainAccuracy;
DecisionTree(){
this.root = new Node(null);
this.EncodeData = null;
this.colName = null;
this.numFeatures = 0;
this.trainAccuracy = 0;
}
public void fit(DataFrame data, int targetIndex){
this.colName = data.columns();
List<Object> target = data.getColumn(targetIndex);
data.removeColumn(targetIndex);
fit(data.getData(), target);
}
public void fit(DataFrame data) {
this.colName = data.columns();
int targetIndex = -1;
for (int i = 0; i < this.colName.size(); i++) {
if (this.colName.get(i).trim().equalsIgnoreCase("target")) {
targetIndex = i;
break;
}
}
if (targetIndex == -1)
targetIndex = data.getData().get(0).size() - 1;
List<Object> target = data.getColumn(targetIndex);
data.removeColumn(targetIndex);
fit(data.getData(), target);
}
public void fit(List<List<Object>> data){
List<Object> target = new ArrayList<>();
int targetIndex = this.colName.contains("target") ? this.colName.indexOf("target") : data.get(0).size() - 1;
for(int i = 0; i < data.size(); i++){
target.add(data.get(i).get(targetIndex));
data.get(i).remove(targetIndex);
}
this.colName.remove(targetIndex);
data = dataEncode(data);
fit(data, target);
}
@SuppressWarnings("unchecked")
public void fit(List<List<Object>> data, List<Object> target){
data = dataEncode(data);
List<double[]> features = (List<double[]>) convertType(data);
List<String> labels = (List<String>) convertType(target);
if(labels.size() != features.size()){
System.out.println(labels.size());
System.out.println(features.size());
System.out.println("Null values present!!!");
return;
}
this.numFeatures = data.get(0).size();
this.root = build(features, labels);
this.trainAccuracy = accuracy(data, target);
}
private List<List<Object>> dataEncode(List<List<Object>> data){
for(int i = 0; i < data.get(0).size(); i++){
boolean canParse = false;
try {
Integer.parseInt(data.get(0).get(i).toString());
canParse = true;
try{
Double.parseDouble(data.get(0).get(i).toString());
}
catch (NumberFormatException e) {
canParse = false;
}
} catch (NumberFormatException e) {
canParse = false;
}
if (canParse) {
continue;
}
int[] encoded = encodeFeature(data, i);
for(int j = 0; j < data.size(); j++){
data.get(j).set(i, encoded[j]);
}
}
return data;
}
private String getMajorityLabel(List<String> labels) {
Map<String, Long> labelCounts = labels.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
return labelCounts.entrySet().stream()
.max(Map.Entry.comparingByValue())
.map(Map.Entry::getKey)
.orElse(null);
}
private Node build(List<double[]> features, List<String> labels){
Set<String> uniqueLabels = new HashSet<>(labels);
if (uniqueLabels.size() == 1) {
return new Node(labels.get(0));
}
if (features.isEmpty() || features.get(0) == null || features.get(0).length == 0) {
return new Node(getMajorityLabel(labels));
}
int bestFeatureIndex = -1;
double bestThreshold = 0;
double bestGini = Double.MAX_VALUE;
for (int featureIndex = 0; featureIndex < features.get(0).length; featureIndex++) {
Set<Double> thresholds = new HashSet<>();
for (double[] row : features) {
thresholds.add(row[featureIndex]);
}
for (double threshold : thresholds) {
double gini = Gini(featureIndex, threshold, features, labels);
if (gini < bestGini) {
bestGini = gini;
bestFeatureIndex = featureIndex;
bestThreshold = threshold;
}
}
}
if (bestFeatureIndex == -1) {
return new Node(getMajorityLabel(labels));
}
List<double[]> leftSplit = new ArrayList<>();
List<double[]> rightSplit = new ArrayList<>();
List<String> leftLabels = new ArrayList<>();
List<String> rightLabels = new ArrayList<>();
for (int i = 0; i < features.size(); i++) {
if (features.get(i)[bestFeatureIndex] <= bestThreshold) {
leftSplit.add(features.get(i));
leftLabels.add(labels.get(i));
} else {
rightSplit.add(features.get(i));
rightLabels.add(labels.get(i));
}
}
if (leftSplit.isEmpty() || rightSplit.isEmpty()) {
return new Node(getMajorityLabel(labels));
}
Node node = new Node(bestFeatureIndex, bestThreshold);
try{
if(!leftSplit.isEmpty() && !leftLabels.isEmpty())
node.left = build(leftSplit, leftLabels);
if(!rightSplit.isEmpty() && !rightLabels.isEmpty())
node.right = build(rightSplit, rightLabels);
} catch (Exception e){
System.out.println("Error: " + e.getMessage());
return null;
}
return node;
}
private double Gini(int featureIndex, double threshold, List<double[]> features, List<String> labels) {
List<String> leftLabels = new ArrayList<>();
List<String> rightLabels = new ArrayList<>();
for (int i = 0; i < labels.size(); i++) {
if (features.get(i)[featureIndex] <= threshold) {
leftLabels.add(labels.get(i));
} else {
rightLabels.add(labels.get(i));
}
}
double giniLeft = GiniForLabels(leftLabels);
double giniRight = GiniForLabels(rightLabels);
double weightLeft = (double) leftLabels.size() / labels.size();
double weightRight = (double) rightLabels.size() / labels.size();
return weightLeft * giniLeft + weightRight * giniRight;
}
private double GiniForLabels(List<String> labels) {
if (labels.isEmpty()) return 0;
Map<String, Long> labelCounts = labels.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
double gini = 1.0;
double totalLabels = labels.size();
for (long count : labelCounts.values()) {
double probability = (double) count / totalLabels;
gini -= probability * probability;
}
return gini;
}
@SuppressWarnings("unchecked")
private Object convertType(List<?> data) {
if (data.isEmpty()) return null;
if (!(data.get(0) instanceof List)) {
return data.stream().map(Object::toString).collect(Collectors.toList());
} else {
List<double[]> result = new ArrayList<>();
List<List<Object>> newData = (List<List<Object>>) (List<?>) data;
for (List<Object> row : newData) {
double[] featureArray = new double[row.size()];
for (int j = 0; j < row.size(); j++) {
try {
featureArray[j] = Double.parseDouble(row.get(j).toString());
} catch (NumberFormatException e) {
System.err.println("Failed to parse value: " + row.get(j));
throw new IllegalArgumentException("Non-numeric value encountered: " + row.get(j), e);
}
}
result.add(featureArray);
}
return result;
}
}
private int[] encodeFeature(List<List<Object>> data, int columnIndex) {
String key = this.colName == null ? "Column" + columnIndex : this.colName.get(columnIndex);
if(EncodeData==null){
EncodeData = new EncodeData();
}
if ((EncodeData.allData().isEmpty()) || !EncodeData.allData().containsKey(key)) {
EncodeData.newMap(key);
}
int[] values = new int[data.size()];
int k = 0;
for(int i = 0; i < data.size(); i++){
if(!(EncodeData.allData().get(key).containsKey(data.get(i).get(columnIndex).toString()))){
EncodeData.addSingleData(key, data.get(i).get(columnIndex).toString(), k++);
}
values[i] = EncodeData.getData(key, data.get(i).get(columnIndex).toString());
}
return values;
}
private List<Object> encodeInput(List<Object> input) {
for (int i = 0; i < input.size(); i++) {
Object value = input.get(i);
try{
Integer.parseInt(value.toString());
continue;
}
catch (Exception e){
//ignore
}
try{
Double.parseDouble(value.toString());
continue;
}
catch (Exception e){
//ignore
}
input.set(i, EncodeData.getData(this.colName.get(i), input.get(i).toString()));
}
return input;
}
public String predict(List<Object> input){
List<Object> input2 = encodeInput(input);
if(input.size()!=input2.size()){
System.out.println("Encoded input size not equal to actual input size");
}
double[] inputArr = new double[input2.size()];
for(int i = 0; i < input2.size(); i++){
try{
inputArr[i] = Double.parseDouble(input2.get(i).toString());
}
catch (Exception e){
System.out.println("Error: "+e);
return null;
}
}
if(inputArr.length!=input2.size()){
System.out.println("Size not equal 2");
}
return predict(inputArr);
}
public String predict(double[] input){
if(input.length != this.numFeatures){
return null;
}
Node node = this.root;
while (!node.isLeaf()) {
if (input[node.featureIndex] <= node.threshold) {
node = node.left;
} else {
node = node.right;
}
}
return node.label;
}
@SuppressWarnings("unchecked")
public double accuracy(List<List<Object>> data, List<Object> target){
List<double[]> features = (List<double[]>) convertType(data);
List<String> labels = (List<String>) convertType(target);
if(features.get(0).length != this.numFeatures)
return 0;
double correct = 0, wrong = 0;
for(int i = 0; i < features.size(); i++){
String pred = predict(features.get(i));
if(pred.equals(labels.get(i)))
correct+=1;
else
wrong+=1;
}
return correct/(correct+wrong);
}
public double train_score(){
return this.trainAccuracy;
}
}