>扩展资料记录
Scikit-Learn文档:Model persistence
pickle — Python object serialization
>Joblib
这里用的Joblib进行python对象固化:
# 保存该对象-joblib
from sklearn.externals import joblib
# 压缩存储
joblib.dump(my_object, 'my_objectpikcel.pkl', compress=True)
# 载入模型
reload_obj = joblib.load('my_object.pkl')
>实例:鸢尾花数据集(逻辑回归)预测模型的本地固化
样例代码(Jupyter文件)下载:https://download.csdn.net/download/shenpibaipao/10388901
功能函数:
# 制图
import matplotlib.pyplot as plt
def paint(predicted, target):
fig, ax = plt.subplots() # import matplotlib.pyplot as plt
ax.plot(range(len(predicted)), predicted, 'gx', label='Predicted Class')
ax.plot(range(len(target)), target, 'r--', label='True Class')
plt.show()# http://blog.csdn.net/shenpibaipao
# 计算正确率
def acc(predicted, target):
sums = 0
for i in range(len(predicted)):
if predicted[i] == target[i]:
sums += 1
print 'ACC: ', sums * 100.0 / len(predicted), "%"
# 计算正确率&制图
def acc_and_paint(predicted, target):
acc(predicted, target)
paint(predicted, target)
训练与预测模型:
from sklearn import datasets
from sklearn import linear_model
from sklearn import model_selection
# 导入数据集
iris = datasets.load_iris() # from sklearn import datasets
# 切分数据集
train_X, test_X, train_y, test_y = \
model_selection.train_test_split(iris.data,
iris.target,
test_size=0.2,
random_state=45)
# 导入模型
lg = linear_model.LogisticRegression(multi_class='ovr') # 采用 one-vs-rest 的多分类策略
lg.fit(train_X, train_y)
predicted = lg.predict(test_X)
# 判断分类正误率并制图
acc_and_paint(predicted, test_y)
保存模型并载入模型进行使用:
# 保存该模型-joblib
from sklearn.externals import joblib
# 压缩存储
joblib.dump(lg, 'joblib_my_lg_pikcel.pkl', compress=True)
# 载入模型
reload_lg=joblib.load('joblib_my_lg_pikcel.pkl')
# 利用载入好的模型进行预测
predicted = reload_lg.predict(test_X)
# 判断分类正误率并制图
acc_and_paint(predicted, test_y)
结果如下: