-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataTable.java
More file actions
210 lines (173 loc) · 6.82 KB
/
DataTable.java
File metadata and controls
210 lines (173 loc) · 6.82 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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
public class DataTable<T> {
protected List<Attribute<T>> attributes;
protected Attribute<T> targetAttribute;
public DataTable(List<Attribute<T>> attributes, Attribute<T> targetAttribute) {
this.attributes = attributes;
this.targetAttribute = targetAttribute;
}
// returns the attribute x which maximizes information gain of result given x
public Attribute<T> maxInfoGainAttribute() {
Attribute<T> maxInfoGainAttribute = attributes.get(0);
Double maxInfoGain = informationGain(targetAttribute, attributes.get(0));
for (Attribute<T> attribute : attributes) {
Double currentInfoGain = informationGain(targetAttribute, attribute);
if (currentInfoGain > maxInfoGain) {
maxInfoGainAttribute = attribute;
maxInfoGain = currentInfoGain;
}
}
return maxInfoGainAttribute;
}
/**
* The information gain of Y given X is the following,
* IG(Y|X) = H(Y) - H(Y|X)
*
* @param attributeY - the attribute for which IG is being calculated
* @param attributeX - the given attribute
*/
public Double informationGain(Attribute<T> attributeY, Attribute<T> attributeX) {
return entropyOf(attributeY) - conditionalEntropyOf(attributeY, attributeX);
}
/**
* The entropy of an attribute is the following,
* H(X) = -1 * sum( P(Xi) log_2 P(Xi) )
* where each Xi is a particular value of X and P(Xi) is the probability of
* that value
*/
public Double entropyOf(Attribute<T> attribute) {
HashMap<T,Double> probabilityHash = getProbabilities(attribute);
Double sum = 0.0;
for (T key : probabilityHash.keySet()) {
sum += entropyCalc(probabilityHash.get(key));
}
return sum;
}
/**
* The conditional entropy of Y, given X, is the following,
* H(Y|X) = -1 * sum(P(Xi) entropyOf(Yi))
* where Yi is only entries of Y where X = Xi
*/
public Double conditionalEntropyOf(Attribute<T> attributeY, Attribute<T> attributeX) {
HashMap<T,Double> xProbabilities = getProbabilities(attributeX);
Double sum = 0.0;
for (T xValue : xProbabilities.keySet()) {
Attribute<T> conditionalYVals = filterAttribute(attributeY, attributeX, xValue);
sum += xProbabilities.get(xValue) * entropyOf(conditionalYVals);
}
return sum;
}
public HashMap<T,Double> getProbabilities(Attribute<T> attribute) {
HashMap<T,Integer> supportHash = getSupportHash(attribute);
HashMap<T,Double> probabilityHash = new HashMap<T,Double>();
for (T key : supportHash.keySet()) {
Double probability = (double) supportHash.get(key) / attribute.size();
probabilityHash.put(key, probability);
}
return probabilityHash;
}
public Double entropyCalc(Double probability) {
return -1 * probability * (Math.log(probability) / Math.log(2));
}
// returns the most commonly occurring value of the targetAttribute
public T targetMajority() {
HashMap<T,Integer> supportHash = getSupportHash(targetAttribute);
T majorityValue = null;
for (T key : supportHash.keySet()) {
if (majorityValue == null || (supportHash.get(key) > supportHash.get(majorityValue))) {
majorityValue = key;
}
}
return majorityValue;
}
/**
* Returns a subset of the data set only containing rows where column
* attrNumber equals filterVal. The column on which the table is
* filtered is removed from the returned DataTable
*/
@SuppressWarnings("unchecked")
public DataTable<T> filterAttributes(int attrNumber, T attrValue) {
List<Attribute<T>> filteredAttributeList = new ArrayList<Attribute<T>>();
for (int i = 0; i < attributes.size(); i++) {
// Add all attributes, excluding the one used for filtering
if (i != attrNumber) {
// Filter the current attribute on the filter column and add to the list
Attribute<T> filteredAttribute = filterAttribute(attributes.get(i),
attributes.get(attrNumber),
attrValue);
filteredAttributeList.add(filteredAttribute);
}
}
// Filter the target attribute so it matches the rest of the table
Attribute<T> filteredTargetAttribute = filterAttribute(targetAttribute,
attributes.get(attrNumber),
attrValue);
// Return the subset table with the correct rows and column removed
return new DataTable(filteredAttributeList, filteredTargetAttribute);
}
// returns only values of attribute1 where attribute2 is equal to attr2Value
@SuppressWarnings("unchecked")
public Attribute<T> filterAttribute(Attribute<T> attribute1,
Attribute<T> attribute2,
T attr2Value) {
List<T> filteredAttributeList = new ArrayList<T>();
for (int i = 0; i < attribute1.size(); i++) {
// if ith entry of attribute2 is equal to attr2Value, add ith entry of attribute1
if (attribute2.get(i).equals(attr2Value)) {
filteredAttributeList.add(attribute1.get(i));
}
}
return new Attribute(attribute1.getAttributeName(), filteredAttributeList);
}
// returns a HashMap where each attribute value is hashed to the number of times it appears
public HashMap<T,Integer> getSupportHash(Attribute<T> attribute) {
HashMap<T,Integer> supportHash = new HashMap<T,Integer>();
for (T val : attribute.getValues()) {
Integer count = supportHash.get(val);
if (count == null) {
supportHash.put(val, 1);
} else {
supportHash.put(val, count + 1);
}
}
return supportHash;
}
public HashMap<String,Integer> getAttributeNameHash() {
HashMap<String,Integer> attributeNameHash = new HashMap<String,Integer>();
for (int i = 0; i < attributes.size(); i++) {
attributeNameHash.put(attributes.get(i).attributeName, i);
}
return attributeNameHash;
}
public void printTable() {
for (int i = 0; i < targetAttribute.size(); i++) {
for (Attribute<T> attr: attributes) {
System.out.print(attr.getValues().get(i).toString() + " ");
}
System.out.println(targetAttribute.getValues().get(i).toString());
}
}
public int indexOfAttribute(String attributeName) {
for (int i = 0; i < attributes.size(); i++) {
if (attributes.get(i).getAttributeName().equals(attributeName)) {
return i;
}
}
return 0;
}
public List<Attribute<T>> getAttributes() {
return this.attributes;
}
public Attribute<T> getTargetAttribute() {
return this.targetAttribute;
}
public boolean attributesEmpty() {
return attributes.isEmpty();
}
public boolean allTargetsSame() {
return targetAttribute.allEqual();
}
}