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

一、背景

现在我们预测每次都要重新运行一遍模型。完整的流程应该是不断调整阈值重复计算。

当训练或者计算好一个模型之后,那么如果别人需要我们提供结果预测,就需要保存模型(主要是保存算法的参数)。

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

1、import joblib

保存:joblib.dump(rf, "test.pkl")

rf:是预估器estimator

test.pkl:是保存的名字

将预估器序列化保存在本地

加载:estimator = joblib.load("test.pkl")

2、代码

python 复制代码
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 joblib

def 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 None

def 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 None

def 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 None

def 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 None

if __name__ == "__main__":
  # 代码1:正规方程的优化方法对波士顿房价进行预测
  linear1()
  # 代码2:梯度下降的优化方法对波士顿房价进行预测
  linear2()
  # 代码3:岭回归对波士顿房价进行预测
  linear3()
  # 代码4:加载模型
  linear4()
相关推荐
极小狐2 小时前
如何解决极狐GitLab 合并冲突?
人工智能·git·机器学习·gitlab
云天徽上11 小时前
【数据可视化-21】水质安全数据可视化:探索化学物质与水质安全的关联
安全·机器学习·信息可视化·数据挖掘·数据分析
奋斗者1号13 小时前
逻辑回归:使用 S 型函数进行概率预测
算法·机器学习·逻辑回归
巷北夜未央14 小时前
杂谈-有感而发
人工智能·算法·机器学习
缘友一世14 小时前
机器学习中的“三态模型“:过拟合、欠拟合和刚刚好
人工智能·机器学习
最爱茄子包14 小时前
从0到1掌握机器学习核心概念:用Python亲手构建你的第一个AI模型(超多代码+可视化)
人工智能·python·机器学习
人猿泰飞17 小时前
【AI训练环境搭建】在Windows11上搭建WSL2+Ubuntu22.04+Tensorflow+GPU机器学习训练环境
windows·ubuntu·机器学习·wsl·gpu训练
硅谷秋水18 小时前
UniOcc:自动驾驶占用预测和预报的统一基准
人工智能·深度学习·机器学习·计算机视觉·自动驾驶
潦草通信狗18 小时前
Joint communication and state sensing under logarithmic loss
人工智能·深度学习·算法·机器学习·信号处理·信息论·通信感知一体化
AI大模型顾潇18 小时前
[特殊字符] 大模型对话风格微调项目实战——模型篇 [特殊字符]✨
人工智能·算法·机器学习·数据挖掘·大模型·微调·ai大模型