关键词

持久化机器学习模型(joblib方式)

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.externals import joblib

X_train = [[5],[6], [8], [10], [14], [18], [20], [20.1]]
y_train = [[5],[7], [9], [13], [17.5], [18], [20], [25]]
X_test = [[6], [8], [11], [16]]
y_test = [[8], [12], [15], [18]]
regressor = LinearRegression()
regressor.fit(X_train, y_train)
xx = np.linspace(0, 26, 100)
#根据线性预测分析0-26的Y值
yy = regressor.predict(xx.reshape(xx.shape[0], 1))
#绘画X_Y关系直线
plt.plot(xx, yy)
plt.title('Pizza price regressed on diameter')
plt.xlabel('Diameter in inches')
plt.ylabel('Price in dollars')
plt.axis([0, 25, 0, 25])
plt.grid(True)
plt.scatter(X_train, y_train)

#持久化保存模型
joblib.dump(value=regressor,filename="regressorModel20191023.gz",compress=True)
print("model has saved!")
#加载先前保存的模型
model=joblib.load(filename="regressorModel20191023.gz")
print("model has loaded!")
print(type(model))
#导入模型后再次预测分析0-26的Y值
yy1= model.predict(xx.reshape(xx.shape[0], 1))
#绘画X_Y关系直线
plt.plot(xx, yy1)
plt.show()

 

本文链接:http://task.lmcjl.com/news/12705.html

展开阅读全文