-
Notifications
You must be signed in to change notification settings - Fork 0
/
FeatureSelection.java
147 lines (114 loc) · 4.07 KB
/
FeatureSelection.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
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public class FeatureSelection {
/*
def MI(word):
"""
Compute the weighted mutual information of a term.
"""
T = totals[0] + totals[1]
W = pos[word] + neg[word]
I = 0
if W==0:
return 0
if neg[word] > 0:
# doesn't occur in -ve
I += (totals[1] - neg[word]) / T * log ((totals[1] - neg[word]) * T / (T - W) / totals[1])
# occurs in -ve
I += neg[word] / T * log (neg[word] * T / W / totals[1])
if pos[word] > 0:
# doesn't occur in +ve
I += (totals[0] - pos[word]) / T * log ((totals[0] - pos[word]) * T / (T - W) / totals[0])
# occurs in +ve
I += pos[word] / T * log (pos[word] * T / W / totals[0])
return I
totals[0] = sum(pos.values())
totals[1] = sum(neg.values())
*/
public static double MutualInfo(String word,int postotal,int negtotal,int posvalue,int negvalue)
{
double i=0;
int t= postotal + negtotal;
int w = posvalue + negvalue;
if(w==0)
{
return 0;
}
if(negvalue > 0)
{
//# doesn't occur in -ve
//# I += (totals[1] - neg[word]) / T * log ((totals[1] - neg[word]) * T / (T - W) / totals[1])
i += (negtotal - negvalue)/ t * Math.log((negtotal - negvalue) * t / ( t - w) / negtotal );
//# occurs in -ve
//I += neg[word] / T * log (neg[word] * T / W / totals[1])
i += negvalue / t * Math.log( negvalue * t / w /negtotal );
}
if(posvalue > 0)
{
//# doesn't occur in +ve
// I += (totals[0] - pos[word]) / T * log ((totals[0] - pos[word]) * T / (T - W) / totals[0])
i += (postotal - posvalue)/ t * Math.log((postotal - posvalue) * t / ( t - w) / postotal );
//# occurs in +ve
//I += pos[word] / T * log (pos[word] * T / W / totals[0])
i += posvalue / t * Math.log( posvalue * t / w /postotal );
}
return i;
}
/*
* Select top k features using mutual_information
*/
public static void FeatureSelection(HashMap<String, Integer> posCounts,HashMap<String, Integer> negCounts) throws FileNotFoundException, IOException
{
HashMap<String, Integer> CombinedMap = new HashMap<String, Integer>();
HashMap<String, Double> FeaturedSet = new HashMap<String, Double>();
Double mi_val=0.0;
String fileName = "FeaturedSet.properties";
int postotal=0;
int negtotal=0;
int posvalue=0;
int negvalue=0;
for (int val : posCounts.values()) {
postotal += val;
}
// System.out.println(posSum);
for (int val : negCounts.values()) {
negtotal += val;
}
CombinedMap.putAll(posCounts);
CombinedMap.putAll(negCounts);
for (String key : CombinedMap.keySet()) {
posvalue = posCounts.get(key);
negvalue = negCounts.get(key);
mi_val=FeatureSelection.MutualInfo(key,postotal, negtotal, posvalue, negvalue);
FeaturedSet.put(key, mi_val);
}
/*
* Write this to file
*/
FeatureSelection.saveMapToFile(FeaturedSet, fileName);
}
private static void saveMapToFile(HashMap<String, Double> posCounts, String fileName) throws FileNotFoundException, IOException {
// TODO Auto-generated method stub
Properties properties = new Properties();
for (Map.Entry<String,Double> entry : posCounts.entrySet()) {
properties.put(entry.getKey(), entry.getValue().toString());
}
properties.store(new FileOutputStream(fileName), null);
}
}