当前位置: 首页 > news >正文

【机器学习实战】线性回归分析

本实验基于python语言,采用sklearn工具包进行线性回归实验。实验内容如下:
1,录入课本上关于房价预测的例程,并运行得出结果;
2,通过查找ptyhon编程资料,对给出的例程按要求进行修改,得出新的实验结果;
通过本实验应掌握如下内容:
1,训练样本与测试样本的随机选取;
2,通过sklearn工具包对数据进行线性回归拟合;
3,学会输出线性回归的统计量指标。

import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error, r2_score# Load the diabetes dataset
diabetes_X, diabetes_y = datasets.load_diabetes(return_X_y=True)# Use only one feature
diabetes_X = diabetes_X[:, np.newaxis, 3]# Split the data into training/testing sets
diabetes_X_train = diabetes_X[:-20]
diabetes_X_test = diabetes_X[-20:]# Split the targets into training/testing sets
diabetes_y_train = diabetes_y[:-20]
diabetes_y_test = diabetes_y[-20:]regr = linear_model.LinearRegression()# Train the model using the training sets
regr.fit(diabetes_X_train, diabetes_y_train)# Make predictions using the testing set
diabetes_y_pred = regr.predict(diabetes_X_test)# The coefficients
print("Coefficients: \n", regr.coef_)
# The mean squared error
print("Mean squared error: %.2f" % mean_squared_error(diabetes_y_test, diabetes_y_pred))
# The coefficient of determination: 1 is perfect prediction
print("Coefficient of determination: %.2f" % r2_score(diabetes_y_test, diabetes_y_pred))# Plot outputs
plt.scatter(diabetes_X_test, diabetes_y_test, color="black")
plt.plot(diabetes_X_test, diabetes_y_pred, color="blue", linewidth=3)plt.xticks(())
plt.yticks(())plt.show()

http://www.lryc.cn/news/582033.html

相关文章:

  • 【redis相关】
  • QML中的Item
  • TCP 事务全面研究:从原理到优化与故障排除
  • 百度开源文心 4.5 系列开源大模型 GitCode 本地化部署,硅基流动:文心 vs. DeepSeek vs. Qwen 3.0 深度测评
  • 剑指offer第2版:动态规划+记忆化搜索
  • 使用make编译ROS2节点
  • 如果让计算机理解人类语言- Word2Vec(Word to Vector,2013)
  • 利用英译法案例演示RNN中的注意力机制(基于PyTorch)
  • 超越存在性检查:掌握Linux中`ls`命令的终极指南
  • .net core mvc部署到win10本地的Ubuntu上
  • 【Linux | 网络】网络基础
  • 多模式编译器——vim的使用
  • FastMCP:用于构建MCP服务器的开源Python框架
  • UE 材质 变体 概念
  • C++11标准库算法:深入理解std::none_of
  • Pandas 学习教程
  • T01_神经网络
  • 【python实用小脚本-130】基于 Python 的 HTML 到 Markdown 转换工具:实现高效文档格式转换
  • 钉钉企业内部机器人实现单聊会话互动开发指南
  • 【LeetCode 热题 100】234. 回文链表——快慢指针+反转链表
  • TypeScript 基础与类型系统详解:从入门到实践
  • TB62216FTG,TB62216FNG东芝BiCD集成电路硅单片,PWM斩波型电机驱动集成电路
  • 【Chrome】‘Good助手‘ 扩展程序使用介绍
  • 【操作系统】页面置换
  • OpenWebUI(2)源码学习-后端retrieval检索模块
  • vulnhub靶机渗透:PWNLAB: INIT
  • 海外短剧系统开发:PC端与H5端的全栈实践与深度解析
  • Java-66 深入浅出 分布式服务 Netty详解 EventLoop
  • [特殊字符] Excel 读取收件人 + Outlook 批量发送带附件邮件 —— Python 自动化实战
  • 嵌入式硬件中电容的基本原理与实现详解02