机器学习基础之《回归与聚类算法(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()
相关推荐
natide21 分钟前
表示/嵌入差异-7-间隔/边际对齐(Alignment Margin)
人工智能·深度学习·算法·机器学习·自然语言处理·知识图谱
路人与大师2 小时前
大规模多变量AutoML调参实验报告
人工智能·深度学习·机器学习
夏日白云2 小时前
《PDF解析工程实录》第 12 章|别让模型贴着墙走:为什么加一圈空白,效果反而更好?
图像处理·机器学习·pdf·llm·大语言模型·rag·文档解析
生信碱移3 小时前
单细胞空转CNV分析工具:比 inferCNV 快10倍?!兼容单细胞与空转的 CNV 分析与聚类,竟然还支持肿瘤的亚克隆树构建!
算法·机器学习·数据挖掘·数据分析·聚类
WHS-_-20223 小时前
Monostatic Sensing With OFDM Under Phase Noise: From Mitigation to Exploitation
5g·机器学习
人工智能培训3 小时前
深度学习—卷积神经网络(4)
人工智能·深度学习·神经网络·机器学习·cnn·dnn
Yeats_Liao4 小时前
MindSpore开发之路(十七):静态图 vs. 动态图:掌握MindSpore的两种执行模式
人工智能·深度学习·机器学习
FL16238631294 小时前
电力场景输电线路电缆线异常连接处缺陷金属部件腐蚀检测数据集VOC+YOLO格式3429张5类别
人工智能·yolo·机器学习
算法与编程之美4 小时前
探索不同的损失函数对分类精度的影响.
人工智能·算法·机器学习·分类·数据挖掘
qq_430855885 小时前
线代第五章线性方程组第四节:相似矩阵的概念和性质
人工智能·机器学习