Python数据分析及可视化实例之SKlearn训练结果持久化保存

发布时间:2021-12-03 公开文章

Base

Github加速

 
点此查看

Civil

土木分类资料

 
点此查看

Python

Python编程学习

 
点此查看

Games

JS前端编程学习

 
点此查看

Talk is cheap , show U the code.

 

该源码注释比较全面,需要对SKlearn有一定的了解,

当然,你也可以把它视作黑箱,做个调包侠也是大侠。

 

方法一(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]

方法二(joblib):

from sklearn.externals import joblib
>>> from sklearn import svm
>>> X = [[0, 0], [1, 1]]
>>> y = [0, 1]
>>> clf = svm.SVC()
>>> clf.fit(X, y)  
>>> clf.fit(train_X,train_y)
# 保存训练结果
>>> joblib.dump(clf, "train_model.m")

# 调用训练结果,并进行测试
>>> clf = joblib.load("train_model.m")
clf.predit(test_X)