forked from shibing624/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlr.py
More file actions
44 lines (36 loc) · 1.18 KB
/
lr.py
File metadata and controls
44 lines (36 loc) · 1.18 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
# -*- coding: utf-8 -*-
# Author: XuMing <[email protected]>
# Data: 18/1/25
# Brief:
import pickle
from sklearn.linear_model import LogisticRegression
class LR(object):
"""
LR model for text classification
"""
def __init__(self, lr_name):
self.lr_name = lr_name
self.init = False
def train_model(self, train_x, train_y):
self.clf = LogisticRegression()
self.clf.fit(train_x, train_y)
self.init = True
with open(self.lr_name, 'wb') as f:
pickle.dump(self.clf, f, True)
def load_model(self, lr_name):
with open(lr_name, 'rb') as f:
self.clf = pickle.load(f)
self.init = True
def test_model(self, test_x, test_y):
if not self.init:
print("not init lr model. load ...")
self.load_model(self.lr_name)
print("load lr model done.")
pred_y = self.clf.predict(test_x)
total = len(test_y)
correct = 0
for idx in range(total):
if pred_y[idx] == test_y[idx]:
correct += 1
print('Test LR: total_count, right_count, pred:', total, correct, correct * 1.0 / total)
return pred_y