save 保存自己的模型
pickle
用的是pickle的形勢(shì)得到一個(gè)pickle的文件
存儲(chǔ)
from sklearn import svm
from sklearn import datasets
import pickle
clf = svm.SVC()
iris = datasets.load_iris()
X,y = iris.data,iris.target
clf.fit(X,y)
with open('save/clf.pickle','wb') as f:
pickle.dump(clf,f)
取出進(jìn)行預(yù)測(cè)
利用pickle文件取出享甸,得到的clf2
根據(jù)第一行的數(shù)據(jù)預(yù)測(cè)出花的種類(lèi)是0
from sklearn import svm
from sklearn import datasets
import pickle
iris = datasets.load_iris()
X,y = iris.data,iris.target
with open('save/clf.pickle','rb') as f:
clf2 = pickle.load(f)
print(clf2.predict(X[0:1]))
#[0]
利用joblib
joblib其中會(huì)更快速,利用了多線(xiàn)程的技術(shù)
在文件的存儲(chǔ)上也可以看出差異
from sklearn import svm
from sklearn import datasets
from sklearn.externals import joblib
import pickle
clf = svm.SVC()
iris = datasets.load_iris()
X,y = iris.data,iris.target
clf.fit(X,y)
joblib.dump(clf,'save/clf.pkl')
clf3 = joblib.load('save/clf.pkl')
print(clf3.predict(X[0:1]))
#[0]