持久化機器學習模型(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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM