-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlr_model.py
229 lines (194 loc) · 5.64 KB
/
lr_model.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
#!/usr/bin/env python
# -*- coding: gb18030 -*-
"""
file: lr_model.py
Created: 2016/10/08 14:40:31
Author: ZS
Version:
Usage:
Brief:
Input:
Output:
"""
import sys
import os
import re
import math
import json
reload(sys)
sys.setdefaultencoding('gbk')
class LRModel(object):
"""
lr model predict
"""
_solver_type = "LR"
_nr_class = 2
_label = ['label', '0', '1']
_nr_feature = 0
_bias = -1
_coef_w = {}
_fmap = {}
def __init__(self, model_path, fmap_path=None):
"""
init:
Args: model_path
Returns: No
Raises:
Author:
"""
self.load_model(model_path)
if (fmap_path):
fmap = self.load_fmap(fmap_path)
if (fmap):
self._fmap = fmap
def load_model(self, file_path):
"""
load_model:
Args: file_path
Returns: No
Raises:
Author:
"""
if (not os.path.exists(file_path)):
print("file not exists!")
return -1
fp = open(file_path)
line = fp.readline()
while(line):
if (-1 != line.find("solver_type")):
line_arr = line.split()
self._solver_type = line_arr[1]
line = fp.readline()
if (-1 != line.find("nr_class")):
line_arr = line.split()
self._nr_class = int(line_arr[1])
line = fp.readline()
if (-1 != line.find("label")):
self._label = line.strip().split()
line = fp.readline()
if (-1 != line.find("nr_feature")):
line_arr = line.split()
self._nr_feature = int(line_arr[1])
line = fp.readline()
if (-1 != line.find("bias")):
line_arr = line.split()
self._bias = float(line_arr[1])
if (self._bias < 0.0):
self._bias = 0.0
line = fp.readline()
if (0 == line.find("w")):
line = fp.readline()
break
self._coef_w = {}
self._coef_w[0] = 0.0
index = 0
while(line):
index += 1
self._coef_w[index] = float(line.strip())
line = fp.readline()
#print json.dumps(self._coef_w)
fp.close()
return 0
def print_model(self):
"""
print_model:
Args:
Returns: No
Raises:
Author:
"""
print("solver_type ", self._solver_type)
print("nr_class ", self._nr_class)
print("label ", self._label)
print("nr_feature ", self._nr_feature)
print("bias ", self._bias)
for (key, value) in self._coef_w.items():
print(key, " ", value)
if (self._fmap):
for (key, value) in self._fmap.items():
print(key.encode("gbk"), " ", value)
def load_fmap(self, file_path):
"""
load_fmap:
Args:
Returns: dict
Raises:
Author:
"""
fmap = {}
if (not os.path.exists(file_path)):
print("file not exists!")
return fmap
fp = open(file_path, 'rb')
line = fp.readline()
while line:
line_arr = line.strip().decode('gbk').split()
if len(line_arr) >= 2:
fmap[line_arr[1].encode('gbk')] = line_arr[0]
line = fp.readline()
fp.close()
return fmap
def predict(self, X, need_lookup=True):
"""
predict:
Args: X-> feature in, need_lookup-> whether need feature2int transform
Returns: float score
Raises:
Author:
"""
feature_vec = {}
if (need_lookup):
for (key, value) in X.items():
#print key, value
if (key in self._fmap):
feature_vec[self._fmap[key]] = 1
else:
feature_vec = X
s_sum = self._bias
for (key, value) in feature_vec.items():
s_sum += float(value) * self._coef_w.get(key, 0)
score = 1.0 / (1.0 + math.exp(-s_sum))
return score
def predict_label(self, X, need_lookup=True):
"""
predict_label:
Args: X-> feature in, need_lookup-> whether need feature2int transform
Returns: float score
Raises:
Author:
"""
score = self.predict(X, need_lookup)
if (score > 0.5):
return int(self._label[1])
return int(self._label[2])
if __name__ == '__main__':
lr_model = LRModel("./lr_model.txt", "./fmap.lst")
#lr_model.print_model()
import traceback
count = 0
true_count = 0
for line in sys.stdin.readlines():
try:
line_arr = line.strip().split('\t')
label = 0
pre_label = 1
feature_dict = {}
for i in range(len(line_arr)):
if (0 == i):
label = int(line_arr[i])
else:
f_i = line_arr[i].split(':')
feature_dict[int(f_i[0])] = f_i[1]
score = lr_model.predict(feature_dict, False)
if (score > 0.5):
pre_label = 0
print label, score, pre_label, lr_model.predict_label(feature_dict, False)
count += 1
if (label == pre_label):
true_count += 1
except Exception as e:
print e
traceback.print_exc()
continue
print count, true_count
print float(true_count) / float(count)