线性回归之模型的保存和加载

news/2024/7/2 22:32:11

线性回归之模型的保存和加载

1 sklearn模型的保存和加载API

  • from sklearn.externals import joblib   【目前这行代码报错,直接写import joblib就可以了】
    • 保存:joblib.dump(estimator, 'test.pkl')
    • 加载:estimator = joblib.load('test.pkl')
    • 【注意:1.保存文件,后缀名是**.pkl;2.加载模型是需要通过一个变量进行承接】

2 线性回归的模型保存加载案例

from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import Ridge, RidgeCV
import joblibdef linear_model_demo():"""线性回归:岭回归:return:"""#  1.获取数据data = load_boston()#  2.数据集划分x_train, x_test, y_train, y_test = train_test_split(data.data, data.target, random_state=22)#  3.特征工程-标准化transter = StandardScaler()x_train = transter.fit_transform(x_train)x_test = transter.fit_transform(x_test)#  4.机器学习-线性回归(岭回归)# #  4.1模型训练# estimator = Ridge(alpha=1)# # estimator = RidgeCV(alphas=(0.1, 1, 10))# estimator.fit(x_train, y_train)# #  4.2模型保存# joblib.dump(estimator, "./test.pkl")#  4.3加载模型estimator = joblib.load("./test.pkl")#  5.模型评估#  5.1获取系数等值y_predict = estimator.predict(x_test)print("预测值为:\n", y_predict)print("模型中的系数为:\n", estimator.coef_)print("模型中的偏执为:\n", estimator.intercept_)#  5.2评价#  均方误差error = mean_squared_error(y_test, y_predict)print("误差为:\n", error)linear_model_demo()

运行结果:

注意:


http://lihuaxi.xjx100.cn/news/267139.html

相关文章

xp命令大全

winver---------检查Windows版本 wmimgmt.msc----打开windows管理体系结构(WMI) wupdmgr--------windows更新程序 wscript--------windows脚本宿主设置 write----------写字板 winmsd---------系统信息 wiaacmgr-------扫描仪和照相机向导 winchat--------XP自带局域网聊天 mem…

想了解3D结构光,看这份拆解就对了

点击上方“小白学视觉”,选择加"星标"或“置顶”重磅干货,第一时间送达上节我们提到无论是结构光、TOF还是双目立体成像方案,主要的硬件包括红外光发射器、红外光摄像头、可见光摄像头和图像处理芯片四部分,红外摄像头需…

python kafka 生产

from pykafka import KafkaClientclass KafkaProduct():def __init__(self,hosts,topic):"""初始化实例:param hosts: 连接地址:param topic:"""self.__client KafkaClient(hostshosts)self.__topic self.__client.topics[topic.encode()]def …

揭秘华为AI一站式开发平台,3步构建一个AI模型 | 华为昇腾师资培训沙龙西安场...

2018 年,在第三届 HUAWEI CONNECT(华为全联接大会)上,华为首次公布了 AI 战略与全栈全场景 AI 解决方案,其中包含全球首个覆盖全场景人工智能的华为昇腾(Ascend)系列处理器以及基于华为昇腾全栈…

灵隐寺招聘:没有KPI,佛系上班……

点击上方“视学算法”,选择加"星标"或“置顶”重磅干货,第一时间送达

python urllib2 开启调试

2019独角兽企业重金招聘Python工程师标准>>> 发一段在网上看见. USING HTTPLIB.HTTPCONNECTION.SET_DEBUGLEVEL() WITH URLLIB2 Posted on October 1, 2007, 9:52 pm, by jamiegrove, under python. I’ve been trying to get the debug level turned on in urll…

Poj_1274 The Perfect Stall -二分图裸题

题目:给牛找棚,每个棚只能容一只牛,牛在对应的棚才能产奶,问最多能让几只牛产奶。 /************************************************ Author :DarkTong Created Time :2016/7/31 10:51:05 File Name :Poj_1274.cpp…

Spring Boot 2.X整合Spring-cache,让你的网站速度飞起来

计算机领域有人说过一句名言:“计算机科学领域的任何问题都可以通过增加一个中间层来解决”,今天我们就用Spring-cache给网站添加一层缓存,让你的网站速度飞起来。本文目录 一、Spring Cache介绍二、缓存注解介绍三、Spring BootCache实战1、…