1、模型的保存和加载作用:将训练好的模型进行保存,方便下次直接加载进行使用。
2、API:sklearn.externals import joblib(注意:在这部分导入包时可能会报错,没有joblib这个包,如报错可以直接import joblib)
- 保存:joblib.dump(estimator, "./test.pkl")
- 加载:joblib.load("./test.pkl")
-
注意:
(1)、保存文件,后缀名是.pkl。
(2)、加载模型时需要通过一个变量来承接。
3、代码示范-1 保存时🌰:
# coding:utf-8
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression, SGDRegressor, RidgeCV, Ridge
from sklearn.metrics import mean_squared_error
# from sklearn.externals import joblib
import joblib
def dump_load():
"""
模型保存和加载
:return:None
"""
# 1.获取数据
boston = load_boston()
# print(boston)
# 2.数据基本处理
# 2.1 分割数据
x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=22, test_size=0.2)
# 3.特征工程-标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.fit_transform(x_test)
# 4.机器学习-模型训练-线性回归
# 4.1 模型训练
estimator = Ridge(alpha=1.0)
estimator = RidgeCV(alphas=(0.001, 0.01, 0.1, 1, 10, 100))
estimator.fit(x_train, y_train)
# 4.2 模型保存
joblib.dump(estimator, "./test.pkl")
# # 4.3 模型加载
# estimator = joblib.load("./test.pkl")
print("这个模型的偏置是:\n", estimator.intercept_)
print("这个模型的系数是:\n", estimator.coef_)
# 5.模型评估
# 5.1 预测值
y_pre = estimator.predict(x_test)
print("预测值是:\n", y_pre)
# 5.2 均方误差
ret = mean_squared_error(y_test, y_pre)
print("均方误差:\n", ret)
if __name__ == '__main__':
dump_load()
3.1 模型保存时的运行结果:
模型保存时的运行结果.png3、代码示范-2 加载时🌰:
# coding:utf-8
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression, SGDRegressor, RidgeCV, Ridge
from sklearn.metrics import mean_squared_error
# from sklearn.externals import joblib
import joblib
def dump_load():
"""
模型保存和加载
:return:None
"""
# 1.获取数据
boston = load_boston()
# print(boston)
# 2.数据基本处理
# 2.1 分割数据
x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=22, test_size=0.2)
# 3.特征工程-标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.fit_transform(x_test)
# 4.机器学习-模型训练-线性回归
# # 4.1 模型训练
# estimator = Ridge(alpha=1.0)
# estimator = RidgeCV(alphas=(0.001, 0.01, 0.1, 1, 10, 100))
# estimator.fit(x_train, y_train)
#
# # 4.2 模型保存
# joblib.dump(estimator, "./test.pkl")
# 4.3 模型加载
estimator = joblib.load("./test.pkl")
print("这个模型的偏置是:\n", estimator.intercept_)
print("这个模型的系数是:\n", estimator.coef_)
# 5.模型评估
# 5.1 预测值
y_pre = estimator.predict(x_test)
print("预测值是:\n", y_pre)
# 5.2 均方误差
ret = mean_squared_error(y_test, y_pre)
print("均方误差:\n", ret)
if __name__ == '__main__':
dump_load()
3.2、模型加载时运行结果:
模型加载时运行结果.png总结:模型加载时和保存时运行的结果是一样的,因为加载了以保存的模型,直接调用运行的。
网友评论