sklearn 学习5

save 保存自己的模型

pickle

用的是pickle的形势得到一个pickle的文件

存储

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)

取出进行预测

利用pickle文件取出,得到的clf2
根据第一行的数据预测出花的种类是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其中会更快速,利用了多线程的技术
在文件的存储上也可以看出差异

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]

你可能感兴趣的:(sklearn 学习5)