Loading

【学习笔记】模型的保存与加载

sklearn中模型的保存与加载的api:sklearn.externals.joblib

【学习笔记】回归算法-线性回归中的波士顿房价的模型进行保存:

from sklearn.externals import joblib
...
# 正则方程求解预测结果
lr = LinearRegression()
lr.fit(x_train, y_train)

# 保存训练好的模型
joblib.dump(lr, "./lr_model.pkl")

上例中保存的文件的扩展名为:pkl

加载上面保存的模型:

# 预测房价结果
model = joblib.load("./lr_model.pkl")
y_predict = std_y.inverse_transform(model.predict(x_test))
print("保存的模型的预测结果:", y_predict)

输出结果:

保存的模型的预测结果: [[17.37118212]
 [34.56709952]
 [17.4305089 ]
 [23.35163525]
 [16.75507239]
 [38.7172448 ]
 [21.60892137]
 [35.84302277]
 [29.98418551]
 [13.74507248]
 [20.41994648]
 [33.9901789 ]
 [25.11577134]
 [ 8.53038073]
 [20.60776675]
 [21.90426029]
 [13.45733183]
 [22.46376949]
 [20.39371985]
 [18.76864034]
 [11.38671154]
 [20.05953434]
 [12.83015496]
 [12.03010661]
 [18.23773943]
 [31.06620129]
 [ 5.56241134]
 [12.98516251]
 [10.91820687]
 [13.11476316]
 [ 3.78231428]
 [28.73669394]
 [10.77064138]
 [17.84583808]
 [25.70115301]
 [18.45386837]
 [30.85911707]
 [19.05063058]
 [26.20586891]
 [12.48191789]
 [13.47998438]
 [14.06211429]
 [19.62317357]
 [19.44512303]
 [27.88735019]
 [15.32864261]
 [22.35533616]
 [30.43356824]
 [39.3659543 ]
 [28.09146432]
 [12.90029862]
 [15.80092028]
 [41.12776075]
 [35.45080887]
 [18.28501264]
 [24.91455836]
 [20.79142213]
 [36.23018652]
 [28.69445038]
 [15.13743074]
 [11.12377075]
 [ 7.1662545 ]
 [18.92895135]
 [25.14331425]
 [22.24401089]
 [ 9.26097072]
 [19.36257124]
 [ 5.46575337]
 [26.35382739]
 [19.46779945]
 [17.61702798]
 [20.11687972]
 [21.58956195]
 [25.27759462]
 [13.37850839]
 [25.97373011]
 [12.4729385 ]
 [24.9088518 ]
 [19.71461561]
 [12.47105092]
 [22.26188696]
 [29.08661824]
 [14.97712477]
 [40.28048188]
 [12.564701  ]
 [15.18255318]
 [41.16108541]
 [22.25338689]
 [28.38662329]
 [28.86476611]
 [29.88912828]
 [ 0.90732544]
 [27.64437037]
 [18.17414487]
 [15.46396621]
 [19.57395703]
 [43.21673774]
 [38.70313648]
 [19.01216829]
 [ 8.95379812]
 [16.32508425]
 [13.84733386]
 [19.38368994]
 [17.64480329]
 [16.73515891]
 [28.46209791]
 [25.58264861]
 [27.29229673]
 [18.42422801]
 [22.47274896]
 [22.60092951]
 [14.65879169]
 [25.35595994]
 [ 8.36124205]
 [31.96224201]
 [22.12208782]
 [22.64038758]
 [21.70722241]
 [21.08181869]
 [14.5844319 ]
 [20.27973381]
 [22.58921349]
 [31.44559491]
 [35.07616818]
 [21.12770672]
 [37.09917083]
 [16.49457446]]
posted @ 2019-03-28 15:36  coder-qi  阅读(526)  评论(0编辑  收藏  举报