持久化機器學習模型(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()
相關文章
相關標籤/搜索