-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHw1.java
More file actions
138 lines (121 loc) · 2.8 KB
/
Hw1.java
File metadata and controls
138 lines (121 loc) · 2.8 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
package entropy;
import java.util.*;
public class Hw1 {
public static int NUM_ATTRS = 6;
public static ArrayList<String> attrMap;
public static ArrayList<Integer> usedAttributes = new ArrayList<Integer>();
public static void main(String[] args) {
populateAttrMap();
Tree t = new Tree();
ArrayList<Record> records;
LearningSet learningSet = new LearningSet();
// read in all our data
records = FileReader.buildRecords();
Node root = new Node();
for(Record record : records) {
root.getData().add(record);
}
t.buildTree(records, root, learningSet);
traverseTree(records.get(12), root);
return;
}
public static void traverseTree(Record r, Node root) {
while(root.children != null) {
double nodeValue = 0;
for(int i = 0; i < r.getAttributes().size(); i++) {
if(r.getAttributes().get(i).getName().equalsIgnoreCase(root.getTestAttribute().getName())) {
nodeValue = r.getAttributes().get(i).getValue();
break;
}
}
for(int i = 0; i < root.getChildren().length; i++) {
if(nodeValue == root.children[i].getTestAttribute().getValue()) {
traverseTree(r, root.children[i]);
}
}
}
System.out.print("Prediction for Play Tennis: ");
if(root.getTestAttribute().getValue() == 0) {
System.out.println("No");
}
else if(root.getTestAttribute().getValue() == 0) {
System.out.println("Yes");
}
return;
}
public static boolean isAttributeUsed(int attribute) {
if(usedAttributes.contains(attribute)) {
return true;
}
else {
return false;
}
}
public static int setSize(String set) {
if(set.equalsIgnoreCase("Outlook")) {
return 3;
}
else if(set.equalsIgnoreCase("Wind")) {
return 2;
}
else if(set.equalsIgnoreCase("Temperature")) {
return 3;
}
else if(set.equalsIgnoreCase("Humidity")) {
return 2;
}
else if(set.equalsIgnoreCase("PlayTennis")) {
return 2;
}
return 0;
}
public static String getLeafNames(int attributeNum, int valueNum) {
if(attributeNum == 0) {
if(valueNum == 0) {
return "Sunny";
}
else if(valueNum == 1) {
return "Overcast";
}
else if(valueNum == 2) {
return "Rain";
}
}
else if(attributeNum == 1) {
if(valueNum == 0) {
return "Hot";
}
else if(valueNum == 1) {
return "Mild";
}
else if(valueNum == 2) {
return "Cool";
}
}
else if(attributeNum == 2) {
if(valueNum == 0) {
return "High";
}
else if(valueNum == 1) {
return "Normal";
}
}
else if(attributeNum == 3) {
if(valueNum == 0) {
return "Weak";
}
else if(valueNum == 1) {
return "Strong";
}
}
return null;
}
public static void populateAttrMap() {
attrMap = new ArrayList<String>();
attrMap.add("Outlook");
attrMap.add("Temperature");
attrMap.add("Humidity");
attrMap.add("Wind");
attrMap.add("PlayTennis");
}
}