第五章 线性回归 学习笔记中
目录
5-5 衡量线性回归法的指标 MSE,RMS,MAE05-Regression-Metrics-MSE-vs-MAE
封装我们自己的评测函数
5-6 最好的衡量线性回归法的指标 R Squared
5-5 衡量线性回归法的指标 MSE,RMS,MAE05-Regression-Metrics-MSE-vs-MAE
其实是对训练数据集来说
衡量标准与样本数无关
量纲是平方,有时候会比较麻烦所以
x = x[y < 50.0]
y = y[y < 50.0]没想明白? x,y怎么都是判断y<50
SimpleLinearRegression.py
import numpy as npclass SimpleLinearRegression:def __init__(self):"""初始化Simple Linear Regression模型"""self.a_ = Noneself.b_ = Nonedef fit(self, x_train, y_train):"""根据训练数据集x_train训练Simple Linear Regression模型"""assert x_train.ndim == 1, \"Simple Linear Regressor can only solve single feature training data."assert len(x_train) == len(y_train), \"the size of x_train must be equal to the size of y_train"x_mean = np.mean(x_train)y_mean = np.mean(y_train)self.a_ = (x_train - x_mean).dot(y_train - y_mean) / (x_train - x_mean).dot(x_train - x_mean)self.b_ = y_mean - self.a_ * x_meanreturn selfdef predict(self, x_predict):"""给定待预测数据集x_predict,返回表示x_predict的结果向量"""assert x_predict.ndim == 1, \"Simple Linear Regressor can only solve single feature training data."assert self.a_ is not None and self.b_ is not None, \"must fit before predict!"return np.array([self._predict(x) for x in x_predict])def _predict(self, x_single):"""给定单个待预测数据x,返回x的预测结果值"""return self.a_ * x_single + self.b_def __repr__(self):return "SimpleLinearRegression()"
封装我们自己的评测函数
metrics.py
import numpy as np
from math import sqrtdef accuracy_score(y_true, y_predict):"""计算y_true和y_predict之间的准确率"""assert len(y_true) == len(y_predict), \"the size of y_true must be equal to the size of y_predict"return np.sum(y_true == y_predict) / len(y_true)def mean_squared_error(y_true, y_predict):"""计算y_true和y_predict之间的MSE"""assert len(y_true) == len(y_predict), \"the size of y_true must be equal to the size of y_predict"return np.sum((y_true - y_predict)**2) / len(y_true)def root_mean_squared_error(y_true, y_predict):"""计算y_true和y_predict之间的RMSE"""return sqrt(mean_squared_error(y_true, y_predict))def mean_absolute_error(y_true, y_predict):"""计算y_true和y_predict之间的MAE"""return np.sum(np.absolute(y_true - y_predict)) / len(y_true)
RMSE有放大错误值的趋势,而MAE没有, RMSE尽量小则其最大错误值比较小,其本质是在减小最大的识差的那个值
5-6 最好的衡量线性回归法的指标 R Squared
RMSE和MAE没有这样的体现
预测一真值,平均值—y
意义是什么?为什么好?
假设数据间有一定的线性关系
两个脚本在同一个目录或文件夹下,则一个引用另一个可以用 aaa.bbb 省略aaa则 .bbb
SimpleLinearRegression.py
import numpy as np
from .metrics import r2_scoreclass SimpleLinearRegression:def __init__(self):"""初始化Simple Linear Regression模型"""self.a_ = Noneself.b_ = Nonedef fit(self, x_train, y_train):"""根据训练数据集x_train训练Simple Linear Regression模型"""assert x_train.ndim == 1, \"Simple Linear Regressor can only solve single feature training data."assert len(x_train) == len(y_train), \"the size of x_train must be equal to the size of y_train"x_mean = np.mean(x_train)y_mean = np.mean(y_train)self.a_ = (x_train - x_mean).dot(y_train - y_mean) / (x_train - x_mean).dot(x_train - x_mean)self.b_ = y_mean - self.a_ * x_meanreturn selfdef predict(self, x_predict):"""给定待预测数据集x_predict,返回表示x_predict的结果向量"""assert x_predict.ndim == 1, \"Simple Linear Regressor can only solve single feature training data."assert self.a_ is not None and self.b_ is not None, \"must fit before predict!"return np.array([self._predict(x) for x in x_predict])def _predict(self, x_single):"""给定单个待预测数据x,返回x的预测结果值"""return self.a_ * x_single + self.b_def score(self, x_test, y_test):"""根据测试数据集 x_test 和 y_test 确定当前模型的准确度"""y_predict = self.predict(x_test)return r2_score(y_test, y_predict)def __repr__(self):return "SimpleLinearRegression()"
metrics.py
import numpy as np
from math import sqrtdef accuracy_score(y_true, y_predict):"""计算y_true和y_predict之间的准确率"""assert len(y_true) == len(y_predict), \"the size of y_true must be equal to the size of y_predict"return np.sum(y_true == y_predict) / len(y_true)def mean_squared_error(y_true, y_predict):"""计算y_true和y_predict之间的MSE"""assert len(y_true) == len(y_predict), \"the size of y_true must be equal to the size of y_predict"return np.sum((y_true - y_predict)**2) / len(y_true)def root_mean_squared_error(y_true, y_predict):"""计算y_true和y_predict之间的RMSE"""return sqrt(mean_squared_error(y_true, y_predict))def mean_absolute_error(y_true, y_predict):"""计算y_true和y_predict之间的MAE"""assert len(y_true) == len(y_predict), \"the size of y_true must be equal to the size of y_predict"return np.sum(np.absolute(y_true - y_predict)) / len(y_true)def r2_score(y_true, y_predict):"""计算y_true和y_predict之间的R Square"""return 1 - mean_squared_error(y_true, y_predict)/np.var(y_true)
scikit-learn中的LinearRegression中的score返回r2_score
https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html
第五章 线性回归 学习笔记中相关推荐
- 第五章 决策树——学习笔记
(一)决策树模型与学习 (二)特征选择 熵(entropy): H ( x ) = H ( p ) = − ∑ n j = 1 p i l o g p i H(x)=H(p)=- \underset ...
- 计算机网络(BYSEE)第五章 运输层 学习笔记(0606)
第 5 章 运输层 5.1 运输层协议概述 5.1.1 进程之间的通信 向应用层提供通信服务 运输层属于面向通信部分的最高层,同时也是用户功能中的最低层 只有网络边缘部分的主机的协议栈才有运输层,网络 ...
- 计算机网络第五章-运输层学习笔记
5.1 运输层协议概述 5.1.1 进程之间的通信 为何需要运输层? 运输层协议和网络层协议的主要区别 5.1.2 运输层中的两个协议 UDP TCP 5.1.3 端口 使用端口对应用进程进行唯一标识 ...
- Lasso线性回归学习笔记(公式与代码实现)
目录 Lasso线性回归学习笔记(公式与代码实现) 1 为什么要在线性回归中引入正则化项(简介) 2 常见正则化项 3 损失函数图像与正则化之后的图像 3.1损失函数图像 3.2 加了 L~1~ 正则 ...
- 小五思科技术学习笔记之SSH
下面介绍一下相关概念: SSH的概念: SSH的英文全称为Secure Shell,SSH使用TCP的22号端口,其目的是要在非安全的网络上提供安全的远程登陆和其他安全的网络服务, 为什么要使用SSH ...
- 电气EPlan软件第一章到第五章的学习
电气–EPlan软件第一章到第五章的学习 1.Eplan软件的简单介绍? ①介绍:CAD不能满足工程需要,所以用到Eplan,Eplan结合了windows和CAD的操作风格,使用户有了更好的体验.E ...
- 《Reids 设计与实现》第十五章 集群(中)
<Reids 设计与实现>第十五章 集群(中) 文章目录 <Reids 设计与实现>第十五章 集群(中) 四.在集群中执行命令 1.计算键属于哪个槽 2.判断槽是否由当前节点负 ...
- 控制系统仿真与CAD-薛定宇-第四章matlab学习笔记
控制系统仿真与CAD-薛定宇-第四章matlab学习笔记 04-02传递函数模型 tfdata() 传递函数属性法 04-07典型系统连接计算 pretty 用法 04-08方框图简化 04-09代数 ...
- 第五章 如何使用java中的线程打印偶数和奇数
你有两个线程.您需要使用一个线程打印奇数,使用另一个线程打印偶数.您需要以自然顺序打印最多 MAX. 例如: 如果 MAX 为 10,则需要打印: 1 2 3 4 5 6 7 8 9 10 所以 1 ...
- Elasticsearch7学习笔记(中)
Elasticsearch是实时全文搜索和分析引擎,提供搜集.分析.存储数据三大功能:是一套开放REST和JAVA API等结构提供高效搜索功能,可扩展的分布式系统.它构建于Apache Lucene ...
最新文章
- 这简历一看就是包装过的
- SpringCloud学习笔记:SpringCloud简介(1)
- spring+log4j
- Vue2中$forceUpdate()的使用
- JavaScript菜鸽子基础知识总结(一)
- 整合spring-boot-starter-data-redis报错解决
- 使用Web界面登陆vSphere
- java ojdbc14.jar_ojdbc14_g.jar
- 自定义控件+ViewPage+Fragment....各种收获
- python修改xml文件内容,不废话,拿来即用
- 关闭Tomcat报错The web application [ROOT] appears to have started a thread named [Abandoned connectio
- unity package manager ui 报错An ite m with the same key has alrea d ybeen added.Key
- 计算机到点就有音乐怎么清除缓存垃圾,如何自动清理网易音乐的缓存
- 宁波市第一医院附近的房屋调研
- 宏病毒的研究与实例分析02——复合文档格式分析
- java 事物 notsupport_Spring事务传播属性介绍(二).mandatory、not_supported、never、supports...
- IOS 将文字写绘制成图片并转换为像素数据
- 除了office,常用的办公软件还有这些
- 普通人学python有什么用 ?学好了能干什么
- dbd mysql 4.046安装_perl DBI、DBD-mysql、DBD-Oracle安装
热门文章
- iOS正确获取时间戳的代码
- java实现pdf打印工具类,Java PDF工具类(二)| 使用 wkhtmltox 实现 HTML转PDF(文字/图片/页眉页脚)...
- hive中实现行转列_##[函数]Hive中行列转换(行转列)
- html canvas php,关于HTML canvas的总结
- java 快排_总结Java中的排序算法:选择排序amp;快排amp;堆排序amp;归并排序(后附视频讲解)...
- rbac 一个用户对应多个账号_SaaS产品用户权限管理-RBAC
- python transform hive_Hive使用TRANSFORM运行Python脚本总结
- ATT的汇编格式X86内联汇编
- HTML基础知识回顾整理
- MySQL中serial关键字的作用