-
Notifications
You must be signed in to change notification settings - Fork 12
/
05featureselection.py
160 lines (125 loc) · 5.84 KB
/
05featureselection.py
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
#!/usr/bin/python
# input: beginning and ending year (e.g. 1996 2011), label to predict, feature type, algorithm
# output: p-independence for all features
import json
import os
import multiprocessing
import sys
import time
import itertools
import numpy
from sklearn.feature_selection import univariate_selection
class Features2Indices(object):
def __init__(self, featuresets):
raise NotImplemented
class Features2IndicesByFeature(Features2Indices):
""" Just uses the feature name itself, not particular values. """
def __init__(self, featuresets):
self.features2indices = {}
self.indices2features = []
for i, (key, all_values) in enumerate(sorted(featuresets.items())):
self.features2indices[key] = {"index": i,
"values": dict([ (j, v) for v, j in enumerate(sorted(all_values)) ])}
self.indices2features.append({"key": key,
"values": sorted(all_values)})
def get_num_features(self):
return len(self.features2indices)
def make_feature_array(self, dct):
lst = [ -1 ] * self.get_num_features()
for k, v in dct.iteritems():
if k == "label":
continue
idx = self.features2indices[k]["index"]
val = self.features2indices[k]["values"][v]
lst[idx] = val
return lst
def get_feature_name(self, idx):
return self.indices2features[idx]["key"]
class Features2IndicesByValue(Features2Indices):
""" Uses feature name+value as the feature name. """
@classmethod
def get_fname(cls, key, value):
return "%s::%s" % (key, value)
def __init__(self, featuresets):
self.features2indices = {}
self.indices2features = []
curpos = 0
for key, all_values in sorted(featuresets.items()):
for value in sorted(all_values):
fname = self.get_fname(key, value)
self.features2indices[fname] = curpos
self.indices2features.append(fname)
curpos += 1
def get_num_features(self):
return len(self.features2indices)
def make_feature_array(self, dct):
lst = [ -1 ] * self.get_num_features()
for k, v in dct.iteritems():
if k == "label":
continue
fname = self.get_fname(k, v)
idx = self.features2indices[fname]
lst[idx] = 1
return numpy.array(lst)
def get_feature_name(self, idx):
return self.indices2features[idx]
def print_significant_features(p_values, feature_mapper, start_year, end_year):
best_indices = sorted(range(len(p_values)),
key=(lambda x: p_values[x]))
lines = []
for idx in best_indices:
lines.append( [feature_mapper.get_feature_name(idx), str(p_values[idx])] )
max_width_by_col = [ max( len(lines[i][c]) for i in xrange(len(lines)) ) for c in xrange(len(lines[0])) ]
for fname, val in lines:
print fname.ljust(max_width_by_col[0]), "\t", val.ljust(max_width_by_col[1])
def _read_file(args):
fn, params, functions = args[0], args[1], args[2:]
results = []
for line in open(fn):
results.append([ f(line.strip(), **params) for f in functions ])
return results
def _get_label(line, **kwargs):
return 1 if json.loads(line)["label"] == kwargs["predicted_label"] else 0
def _make_arr(line, **kwargs):
return kwargs["feature_mapper"].make_feature_array(json.loads(line))
def _noop(line, **kwargs):
pass
def get_feature_array(start_year, end_year, feature_mapper, predicted_label):
pool = multiprocessing.Pool(processes=(multiprocessing.cpu_count()-1))
try:
def send_to_pool(args):
return itertools.chain(*pool.imap(_read_file, args, chunksize=1))
filenames = [ os.path.join("data", "training", "%d.tdata" % year) for year in range(start_year, end_year + 1) ]
args = [ (fn, {}, _noop) for fn in filenames ]
num_lines = sum( 1 for _ in enumerate(send_to_pool(args)) )
value_array = numpy.zeros( (num_lines, feature_mapper.get_num_features()) )
labels = numpy.zeros(num_lines)
kwargs = {"feature_mapper": feature_mapper, "predicted_label": predicted_label}
args = [ (fn, kwargs, _make_arr, _get_label) for fn in filenames ]
# rip through all data points again, this time populating the array
for i, (val_arr, label) in enumerate(send_to_pool(args)):
for j in xrange(len(val_arr)):
value_array[i][j] = val_arr[j]
labels[i] = label
return value_array, labels
finally:
pool.close()
def get_feature_mapper(start_year, end_year, feature_mapper_clss):
# grabs all possible features for a given start/end year and maps all keys and values to integers for arrays
featuresets = {}
for year in range(start_year, end_year + 1):
fn = os.path.join("data", "featuresets", "%d.json" % year)
d = json.load(open(fn))
for k, values in d.iteritems():
featuresets.setdefault(k, set()).update(set(values))
return feature_mapper_clss(featuresets)
def main():
start_year, end_year, predicted_label, feature_type, selection_type = int(sys.argv[1]), int(sys.argv[2]), sys.argv[3], sys.argv[4], sys.argv[5]
feature_mapper_clss = {"name": Features2IndicesByFeature, "nameval": Features2IndicesByValue}[feature_type]
selection_fn = {"chi2": univariate_selection.chi2, "anova": univariate_selection.f_classif}[selection_type]
feature_mapper = get_feature_mapper(start_year, end_year, feature_mapper_clss)
value_array, labels = get_feature_array(start_year, end_year, feature_mapper, predicted_label)
_, p_values = selection_fn(value_array, labels)
print_significant_features(p_values, feature_mapper, start_year, end_year)
if __name__ == "__main__":
main()