CS_sklearn_3 Regression

Regression

Introduction

线性回归是机器学习中最简单的回归算法,多元线性回归指的就是一个样本有多个特征的线性回归问题。对于一个有 个特征的样本 而言,它的回归结果可以写作一个几乎人人熟悉的方程:

y ^ i = w 0 + w 1 x i 1 + w 2 x i 2 + . . . + w n x i n \hat{y}_i = w_0 + w_1x_{i1} + w_2x_{i2} + ... + w_nx_{in} y^i=w0+w1xi1+w2xi2+...+wnxin

   w w w被统称为模型的参数,其中 w 0 w_0 w0被称为截距(intercept), w 1   w n w_1 ~ w_n w1 wn被称为回归系数(regression coefficient),有时也是使用 θ \theta θ或者 β \beta β来表示。这个表达式,其实就和我们小学时就无比熟悉的 y = a x + b y = ax + b y=ax+b是同样的性质。其中 y y y是我们的目标变量,也就是标签。 x i 1   x i n x_{i1} ~ x_{in} xi1 xin是样本 i i i上的特征不同特征。如果考虑我们有m个样本,则回归结果可以被写作:
y ^ = w 0 + w 1 x 1 + w 2 x 2 + . . . + w n x n \boldsymbol{\hat{y}} = w_0 + w_1 \boldsymbol{x_{1}} + w_2 \boldsymbol{x_{2}} + ... + w_n \boldsymbol{x_{n}} y^=w0+w1x1+w2x2+...+wnxn

其中 y \boldsymbol{y} y是包含了m个全部的样本的回归结果的列向量。注意,我们通常使用粗体的小写字母来表示列向量,粗体的大 写字母表示矩阵或者行列式。我们可以使用矩阵来表示这个方程,其中 w \boldsymbol{w} w可以被看做是一个结构为(n+1,1)的列矩阵 X \boldsymbol{X} X
是一个结构为(m,n+1)的特征矩阵,则有:
[ y ^ 1 y ^ 2 y ^ 3 . . . y ^ m ] = [ 1 x 11 x 12 x 13 . . . x 1 n 1 x 21 x 22 x 23 . . . x 2 n 1 x 31 x 32 x 33 . . . x 3 n . . . 1 x m 1 x m 2 x m 3 . . . x m n ] ∗ [ w 0 w 1 w 2 . . . w n ] \begin{bmatrix} \hat{y}_1\\ \hat{y}_2\\ \hat{y}_3\\ ...\\ \hat{y}_m\\ \end{bmatrix}= \begin{bmatrix} 1 & x_{11} & x_{12} & x_{13} &... & x_{1n} \\ 1 & x_{21} & x_{22} & x_{23} &... & x_{2n} \\ 1 & x_{31} & x_{32} & x_{33} &... & x_{3n} \\ & &...& \\ 1 & x_{m1} & x_{m2} & x_{m3} &... & x_{mn} \\ \end{bmatrix} * \begin{bmatrix} {w}_0\\ w_1\\ {w}_2\\ ...\\ {w}_n\\ \end{bmatrix} y^1y^2y^3...y^m=1111x11x21x31xm1x12x22x32...xm2x13x23x33xm3............x1nx2nx3nxmnw0w1w2...wn

y ^ = X w \boldsymbol{\hat{y}} = \boldsymbol{X} \boldsymbol{w} y^=Xw

  线性回归的任务,就是构造一个预测函数来映射输入的特征矩阵 和标签值 的线性关系,这个预测函数在不同的教 材上写法不同,可能写作 , ,或者 等等形式,但无论如何,这个预测函数的本质就是我们需要构建的 模型,而构造预测函数的核心就是找出模型的参数向量 。但我们怎样才能够求解出参数向量呢?
  记得在逻辑回归和SVM中,我们都是先定义了损失函数,然后通过最小化损失函数或损失函数的某种变化来将求解参 数向量,以此将单纯的求解问题转化为一个最优化问题。在多元线性回归中,我们的损失函数如下定义:
∑ i = 1 m ( y i − y ^ i ) 2 = ∑ i = 1 m ( y i − X i w ) 2 \sum_{i = 1}^m (y_i - \hat{y}_i)^2 = \sum_{i=1}^m (y_i - \boldsymbol{X_{i}w})^2 i=1m(yiy^i)2=i=1m(yiXiw)2

Sample 1 Multiple linear regression

from sklearn.linear_model import LinearRegression as LR
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.datasets import fetch_california_housing as fch #加利福尼亚房屋价值数据集
import pandas as pd
import matplotlib.pyplot as plt

  打印数据

housevalue = fch()

print(housevalue.data)

  转换到DataFrame格式
在这里插入图片描述
  看一下列标题,把原来的[0, 1, 2, 3, 4, 5, 6, 7]列标题变成feature_names
在这里插入图片描述
  看一下把X的列标题修改之后的结果
在这里插入图片描述

  分开训练集和测试集,test_size = 0.3的意思是 train:test = 7:3

Xtrain,Xtest,Ytrain,Ytest = train_test_split(X,y,test_size = 0.3,
                                            random_state = 420)

在这里插入图片描述

  修正Xtrain,Xtest的行标题(index)

# 把Xtrain,Xtest的index从乱序变成 0,1,2,3,...,6192 和 0,1,2,3,...,14448
for i in[Xtrain,Xtest]:
    print(i.index)
    print(range(i.shape[0]))
    i.index = range(i.shape[0])

  如果希望进行数据标准化,还记得应该怎么做吗?先用训练集训练(fit)标准化的类,然后用训练好的类分别转化(transform)训练集和测试集

reg = LR().fit(Xtrain,Ytrain)
yhat = reg.predict(Xtest)
print(yhat.shape) #(6192,)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
  接下来就是研究回归类的模型评估指标,介绍一个评估指标均方误差MSE(mean squared error)来衡量我们的预测值和真实值的差异:
M S E = 1 m ∑ i = 1 m ( y i − y i ^ ) 2 MSE = \frac{1}{m} \sum_{i=1}^m (y_i - \hat{y_i})^2 MSE=m1i=1m(yiyi^)2
  均方误差,本质是在RSS的基础上除以了样本总量,得到了每个样本量上的平均误差。有了平均误差,我们就可以将平均误差和我们的标签的取值范围在一起比较,以此获得一个较为可靠的评估依据。
  在sklearn当中,我们有两种方式调用这个评估指标,一种是使用sklearn专用的模型评估模块metrics里的类mean_squared_error,另一种是调用 交叉验证的类cross_val_score并使用里面的scoring参数来设置使用均方误差。

from sklearn.metrics import mean_squared_error as MSE
from sklearn.metrics import mean_absolute_error as MAE
print(MSE(yhat,Ytest)) # 0.5309012639324571
print(MAE(yhat,Ytest)) # 0.5307069814636152

在这里插入图片描述

#为什么报错了?来试试看!
import sklearn
print(sorted(sklearn.metrics.SCORERS.keys()))
'''
['accuracy',
 'adjusted_mutual_info_score',
 'adjusted_rand_score',
 'average_precision',
 'balanced_accuracy',
 'brier_score_loss',
 'completeness_score',
 'explained_variance',
 'f1',
 'f1_macro',
 'f1_micro',
 'f1_samples',
 'f1_weighted',
 'fowlkes_mallows_score',
 'homogeneity_score',
 'jaccard',
 'jaccard_macro',
 'jaccard_micro',
 'jaccard_samples',
 'jaccard_weighted',
 'max_error',
 'mutual_info_score',
 'neg_log_loss',
 'neg_mean_absolute_error',
 'neg_mean_squared_error',
 'neg_mean_squared_log_error',
 'neg_median_absolute_error',
 'normalized_mutual_info_score',
 'precision',
 'precision_macro',
 'precision_micro',
 'precision_samples',
 'precision_weighted',
 'r2',
 'recall',
 'recall_macro',
 'recall_micro',
 'recall_samples',
 'recall_weighted',
 'roc_auc',
 'v_measure_score']
'''

  使用正确的参数就能调用MSE
在这里插入图片描述
  接下来就研究衡量是否拟合足够多信息的指标,sorted()函数就是排序
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值