-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathUpdatePkl.py
37 lines (32 loc) · 985 Bytes
/
UpdatePkl.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
import pickle
import sqlite3
import numpy as np
from flask_web.vectorizer import vect
#更新模型方法,每次更新10000条评论
def update_pkl(db_path,clf,batch_size=10000):
conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute("SELECT * from review")
#获取到所有的评论
results = c.fetchmany(batch_size)
while results:
data = np.array(results)
#获取评论
X = data[:,1]
#获取
Y = int(data[:,2])
classes = np.array([0,1])
#将评论转成特征向量
x_train = vect.transform(X)
#更新模型
clf.partial_fit(x_train,Y,classes=classes)
results = c.fetchmany(batch_size)
conn.close()
return None
if __name__ == "__main__":
#加载模型
clf = pickle.load(open("pkl/classifier.pkl", "rb"))
#更新模型
update_pkl("db/move_review.db",clf)
#保存模型
pickle.dump(clf,open("pkl/classifier.pkl","wb"),protocol=4)