博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Bobo老师机器学习笔记第五课-线性回归算法的评估指标
阅读量:4170 次
发布时间:2019-05-26

本文共 1890 字,大约阅读时间需要 6 分钟。

评价线性回归的指标有四种,均方误差(Mean Squared Error)、均方根误差(Root Mean Squared Error)、平均绝对值误差(Mean Absolute Error)以及R Squared方法。 sklearnz中使用的,也是大家推荐的方法是R Squared方法。

1、均方误差 MSE

MSE的值越小,说明预测模型描述实验数据具有更好的精确度 

2、均方根误差 RMSE

3、平均绝对值误差 MAE

4、 R平方 R2S 

四种代码实现:

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之间的RMSE"""    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)
import numpy as npimport matplotlib.pyplot as pltfrom sklearn.datasets import load_bostonfrom model_selection import train_test_splitfrom simplelinerregression import SimpleLineRegession2from metrics import *boston = load_boston()x = boston.data[:, 5]y = boston.targetx = x[y < 50.0]y = y[y < 50.0]x_train, x_test, y_train, y_test = train_test_split(x, y)reg = SimpleLineRegession2()reg.fit(x_train, y_train)print 'MAE:',  mean_absoulte_error(y_test, reg.predict(x_test))print 'RMSE:', root_mean_squared_error(y_test, reg.predict(x_test))print 'MSE:', mean_squared_error(y_test, reg.predict(x_test))print 'RSE:', r2_score(y_test, reg.predict(x_test))plt.scatter(x, y)plt.plot(x, reg.predict(x), color="red")plt.show()

以上用波士顿房价数据,其中选择一个维度进行的分析:

不同指标的分数:

MAE: 4.341835956360974

RMSE: 6.2500402273
MSE: 39.063002842901284
RSE: 0.47025653581866034

参考文章:

要是你在西安,感兴趣一起学习AIOPS,欢迎加入QQ群 860794445

你可能感兴趣的文章
PostgreSQL数据库管理第十章Repmgr
查看>>
合适普通人的学习方法
查看>>
微服务Consul简介
查看>>
小富靠勤-大富靠命
查看>>
Linux使用技巧VNC
查看>>
PG数据库部署linux参数调整
查看>>
Linux shell正则表达式-sed-awk-grep应用
查看>>
linux系统管理—第五章Linux-bashshell
查看>>
PostgreSQL数据库管理 第二章体系结构
查看>>
PostgreSQL数据库管理 第三章实例管理与管理工具
查看>>
PostgreSQL数据库管理第七章流复制
查看>>
PostgreSQL数据库管理第十章Repmgr
查看>>
PostgreSQL数据库管理 第八章日常运维
查看>>
PostgreSQL数据库管理 第五章表空间管理
查看>>
PostgreSQL数据库管理第九章备份恢复
查看>>
PostgreSQL数据库管理数据导入导出
查看>>
MySQL数据库管理-体系结构
查看>>
MySQL数据库部署linux参数调整
查看>>
MySQL数据库管理-安装
查看>>
MySQL数据库单进程多线程数据库
查看>>