-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfeature_select.py
228 lines (189 loc) · 8.81 KB
/
feature_select.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
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
import numpy as np
import scipy
import sys
import heapq
def read_liblinear_model(model):
model_parameters = {}
model_parameters["nr_feature"] = model.nr_feature
model_parameters["nr_class"] = model.nr_class
model_parameters["solver_type"] = model.param
model_parameters["bias"] = model.bias
model_parameters["label"] = model.label
model_matrix =np.ctypeslib.as_array(model.w,(model.nr_feature, model.nr_class))
for index,value in enumerate(model_matrix.flat):
model.w[index] = value
return model_parameters, model_matrix
def write_liblinear_model(m, model_matrix):
for index,value in enumerate(model_matrix.flat):
model.w[index] = value
return m
def large_weighted_features(fC_Bookkeaper,model,limit):
large_feature_list = []
parameters, matrix = read_liblinear_model(model)
for i in range(int(model.nr_feature)):
max_abs_value = abs(matrix[i, :]).max()
if max_abs_value > float(limit):
large_feature_list.append(fC_Bookkeaper.noToFeat[(i+1)])
return large_feature_list
def largest_weighted_features(fC_Bookkeeper, lC_Bookkeeper, model,count):
maximum_abs_values = []
parameters, matrix = read_liblinear_model(model)
for featIndex in range(int(model.nr_feature)):
absRow = abs(matrix[featIndex, :])
maxVal = absRow.max()
labelIndex = absRow.argmax()
maximum_abs_values.append((-1*maxVal, featIndex, labelIndex, matrix[featIndex, labelIndex]))
#maximum_abs_values.append((-1 * (abs(matrix[i, :]).max()), i + 1))
h = []
heapq.heapify(h)
largest_weighted_feature_list = []
for item in maximum_abs_values:
heapq.heappush(h, item)
for i in range(int(count)):
featNo, labelNo, weight = heapq.heappop(h)[1:]
featName = fC_Bookkeeper.noToFeat[(featNo+1)]
labelName = lC_Bookkeeper.noToFeat[(labelNo+1)]
largest_weighted_feature_list.append((weight, featName, labelName))
return largest_weighted_feature_list
class LiblinearModel(object):
def __init__(self, weightMatrix = None, labelToName = None, nameToLabel = None,
featureToName = None, nameToFeature = None, modelParameters = None):
self.weightMatrix = weightMatrix
self.labelToName = labelToName
self.nameToLabel = nameToLabel
self.featureToName = featureToName
self.nameToFeature = nameToFeature
self.modelParameters = modelParameters
def max_in_coloumn(self, index):
if self.weightMatrix == None:
raise ValueError("No matrix given")
else:
return self.weightMatrix[:, index].max(), int(self.weightMatrix.argmax(axis = 0)[index])
def max_in_row(self, index):
if self.weightMatrix == None:
raise ValueError("No matrix given")
else:
return self.weightMatrix[index, :].max(), int(self.weightMatrix.argmax(axis = 1)[index])
def max_absolute_in_coloumn(self, index):
if self.weightMatrix == None:
raise ValueError("No matrix given")
else:
return abs(self.weightMatrix[:, index]).max(), int(abs(self.weightMatrix).argmax(axis = 0)[index])
def max_absolute_in_row(self, index):
if self.weightMatrix == None:
raise ValueError("No matrix given")
else:
return abs(self.weightMatrix[index, :]).max(), int(abs(self.weightMatrix).argmax(axis = 1)[index])
def large_weighted_features(self,limit):
if self.weightMatrix == None:
raise ValueError("No matrix given")
if self.featureToName == None:
raise ValueError("No number-feature mapping given")
else:
large_feature_list = []
for i in range(int(self.modelParameters["nr_feature"])):
max_abs_value = abs(self.weightMatrix[i, :]).max()
if max_abs_value > float(limit):
large_feature_list.append(self.featureToName[str(i+1)])
return large_feature_list
def set_small_weights_to_zero(matrix, limit):
for i in range(int(self.modelParameters["nr_feature"])):
max_abs_value = abs(self.matrix[i, :]).max()
if max_abs_value < limit:
self.weightMatrix[i] = numpy.zeros(self.modelParameters["nr_class"])
return matrix
def nonzero_weighted_features(self):
return self.large_weighted_features(0)
def largest_weighted_features(self,count):
if self.weightMatrix == None:
raise ValueError("No matrix given")
if self.featureToName == None:
raise ValueError("No number-feature mapping given")
else:
maximum_abs_values = []
for i in range(int(self.modelParameters["nr_feature"])):
maximum_abs_values.append((-1 * (abs(self.weightMatrix[i, :]).max()), i + 1))
h = []
heapq.heapify(h)
largest_weighted_feature_list = []
for item in maximum_abs_values:
heapq.heappush(h, item)
for i in range(int(count)):
feature = heapq.heappop(h)[1]
largest_weighted_feature_list.append(self.featureToName[str(feature)])
return largest_weighted_feature_list
def largest_among_large_weights(self, value, count):
if self.weightMatrix == None:
raise ValueError("No matrix given")
if self.featureToName == None:
raise ValueError("No number-feature mapping given")
else:
maximum_abs_values = []
for i in range(int(self.modelParameters["nr_feature"])):
if abs(self.weightMatrix[i, :]).max() > float(value):
maximum_abs_values.append((-1 * (abs(self.weightMatrix[i, :]).max()), i + 1))
h = []
heapq.heapify(h)
largest_weighted_feature_list = []
for item in maximum_abs_values:
heapq.heappush(h, item)
for i in range(int(count)):
if not len(h) == 0:
feature = heapq.heappop(h)[1]
largest_weighted_feature_list.append(self.featureToName[str(feature)])
else:
break
return largest_weighted_feature_list
def largest_model_weights(self, count):
if self.weightMatrix == None:
raise ValueError("No matrix given")
if self.featureToName == None:
raise ValueError("No number-feature name mapping given")
if self.model_parameters == None:
raise ValueError("No model parameters given")
if self.labelToName == None:
raise ValueError("No number-label name mapping given")
else:
matrix_abs = abs(self.weightMatrix)
matrix_abs_line = matrix_abs.reshape(int(self.model_parameters["nr_feature"]), int(self.model_parameters["nr_class"]))
@staticmethod
def read_mapping(f):
number_name = {}
name_number = {}
for line in f:
name, number = line.strip().split('\t')
number_name[number] = name
name_number[name] = number
return number_name, name_number
@staticmethod
def read_weight(f):
model_parameters = {}
options, matrix = f.read().split("w")
for option in options.split('\n'):
if not option.strip():
continue
fields = option.strip().split(' ')
name = fields[0]
value = fields[1:]
if len(value) == 1:
model_parameters[name.strip()] = value[0].strip()
else:
model_parameters[name.strip()] = value
matrix_2 = matrix.split('\n')[1:]
data_string = ''.join(matrix_2)
model_array = np.fromstring(data_string, sep =' ')
model_parameters["nr_class"]
model_matrix = model_array.reshape(int(model_parameters["nr_feature"]), int(model_parameters["nr_class"]))
return model_parameters, model_matrix
if __name__ == '__main__':
from liblinearutil import *
from tools import *
modelName = sys.argv[1]
model = load_model('{0}.model'.format(modelName))
featCounter = BookKeeper()
featCounter.readFromFile('{0}.featureNumbers'.format(modelName))
labelCounter = BookKeeper()
labelCounter.readFromFile('{0}.labelNumbers'.format(modelName))
topFeats = largest_weighted_features(featCounter, labelCounter, model, int(sys.argv[2]))
for weight, name, label in topFeats:
print weight, name, label