在使用sklearn对模型进行训练时需要保存模型数据,官方文档对此提供了两种方案:
http://scikit-learn.org/stable/modules/model_persistence.html
>>> from sklearn import svm
>>> from sklearn import datasets
>>> clf = svm.SVC()
>>> iris = datasets.load_iris()
>>> X, y = iris.data, iris.target
>>> clf.fit(X, y)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
方案1
>>> import pickle
>>> s = pickle.dumps(clf)
>>> clf2 = pickle.loads(s)
>>> clf2.predict(X[0:1])
array([0])
>>> y[0]
0
或者:
#保存
import cPickle
with open('filename.pkl', 'wb') as f:
cPickle.dump(forest, f)
#读取
with open('filename.pkl', 'rb') as f:
forest = cPickle.load(f)
方案2
>>> from sklearn.externals import joblib
>>> joblib.dump(clf, 'filename.pkl')
>>> clf = joblib.load('filename.pkl')
但是有一个问题需要注意,即32位python和64位python并不能无缝连接使用,即如果用32python保存,然后用64位python进行读取大概率会报错,反之亦然:
报错如下:
ValueError: Buffer dtype mismatch, expected 'SIZE_t' but got 'int'
记录这个是因为工作的时候遇到这个问题,费了不少劲。
如果想查看自己的python是32位还是64位的,可以这样:
In [287]: import platform
In [288]: platform.architecture()
Out[288]: ('32bit', 'WindowsPE')