机器学习基础之《回归与聚类算法(6)—模型保存与加载》

编程入门 行业动态 更新时间:2024-10-23 23:30:34

机器学习基础之《回归与聚类<a href=https://www.elefans.com/category/jswz/34/1770096.html style=算法(6)—模型保存与加载》"/>

机器学习基础之《回归与聚类算法(6)—模型保存与加载》

一、背景

现在我们预测每次都要重新运行一遍模型。完整的流程应该是不断调整阈值重复计算。
当训练或者计算好一个模型之后,那么如果别人需要我们提供结果预测,就需要保存模型(主要是保存算法的参数)。

二、sklearn模型的保存和加载API

1、import joblib
保存:joblib.dump(rf, "test.pkl")
    rf:是预估器estimator
    test.pkl:是保存的名字
    将预估器序列化保存在本地    
加载:estimator = joblib.load("test.pkl")

2、代码

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, Ridge
from sklearn.metrics import mean_squared_error
import joblibdef linear1():"""正规方程的优化方法对波士顿房价进行预测"""# 1、获取数据boston = load_boston()# 2、划分数据集x_train,x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=10)# 3、标准化transfer = StandardScaler()x_train = transfer.fit_transform(x_train)x_test = transfer.transform(x_test)# 4、预估器estimator = LinearRegression()estimator.fit(x_train, y_train)# 5、得出模型print("正规方程-权重系数为:\n", estimator.coef_)print("正规方程-偏置为:\n", estimator.intercept_)# 6、模型评估y_predict = estimator.predict(x_test)print("预测房价:\n", y_predict)error = mean_squared_error(y_test, y_predict)print("正规方程-均方误差为:\n", error)return Nonedef linear2():"""梯度下降的优化方法对波士顿房价进行预测"""# 1、获取数据boston = load_boston()# 2、划分数据集x_train,x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=10)# 3、标准化transfer = StandardScaler()x_train = transfer.fit_transform(x_train)x_test = transfer.transform(x_test)# 4、预估器estimator = SGDRegressor()estimator.fit(x_train, y_train)# 5、得出模型print("梯度下降-权重系数为:\n", estimator.coef_)print("梯度下降-偏置为:\n", estimator.intercept_)# 6、模型评估y_predict = estimator.predict(x_test)print("预测房价:\n", y_predict)error = mean_squared_error(y_test, y_predict)print("梯度下降-均方误差为:\n", error)return Nonedef linear3():"""岭回归对波士顿房价进行预测"""# 1、获取数据boston = load_boston()# 2、划分数据集x_train,x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=10)# 3、标准化transfer = StandardScaler()x_train = transfer.fit_transform(x_train)x_test = transfer.transform(x_test)# 4、预估器estimator = Ridge()estimator.fit(x_train, y_train)# 保存模型joblib.dump(estimator, "my_ridge.pkl")# 5、得出模型print("岭回归-权重系数为:\n", estimator.coef_)print("岭回归-偏置为:\n", estimator.intercept_)# 6、模型评估y_predict = estimator.predict(x_test)print("预测房价:\n", y_predict)error = mean_squared_error(y_test, y_predict)print("岭回归-均方误差为:\n", error)return Nonedef linear4():"""岭回归对波士顿房价进行预测"""# 1、获取数据boston = load_boston()# 2、划分数据集x_train,x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=10)# 3、标准化transfer = StandardScaler()x_train = transfer.fit_transform(x_train)x_test = transfer.transform(x_test)# 加载模型estimator = joblib.load("my_ridge.pkl")# 5、得出模型print("岭回归-权重系数为:\n", estimator.coef_)print("岭回归-偏置为:\n", estimator.intercept_)# 6、模型评估y_predict = estimator.predict(x_test)print("预测房价:\n", y_predict)error = mean_squared_error(y_test, y_predict)print("岭回归-均方误差为:\n", error)return Noneif __name__ == "__main__":# 代码1:正规方程的优化方法对波士顿房价进行预测linear1()# 代码2:梯度下降的优化方法对波士顿房价进行预测linear2()# 代码3:岭回归对波士顿房价进行预测linear3()# 代码4:加载模型linear4()

更多推荐

机器学习基础之《回归与聚类算法(6)—模型保存与加载》

本文发布于:2023-11-14 14:30:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587625.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法   模型   加载   机器   基础

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!