首页 > 代码库 > sklearn两种保存模型的方式

sklearn两种保存模型的方式

作者:卢嘉颖
链接:https://www.zhihu.com/question/27187105/answer/97334347
来源:知乎
著作权归作者所有,转载请联系作者获得授权。

1. pickle
>>> 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)

>>> import pickle
>>> s = pickle.dumps(clf)
>>> clf2 = pickle.loads(s)
>>> clf2.predict(X[0:1])
array([0])
>>> y[0]
0

2. joblib
>>> from sklearn.externals import joblib
>>> joblib.dump(clf, filename.pkl) 
>>> clf = joblib.load(filename.pkl) 

 

sklearn两种保存模型的方式