线性回归算法

本文详细介绍了简单线性回归和多元线性回归的原理、公式及Python实现,包括最小二乘法求解参数和预测函数。同时,提供了误差评估指标如MSE、RMSE、MAE和R2分数的计算方法,并展示了线性回归模型的训练与预测流程。此外,还涉及了多元线性回归的矩阵表示及其计算过程。
摘要由CSDN通过智能技术生成

简单线性回归

  • 简单线性回归包含一个自变量(x)和一个因变量(y),如果包含两个以上的自变量,则称作多元回归分析(multiple regression),被用来描述因变量(y)和自变量(X)以及偏差(error)之间关系的方程叫做回归模型
    • 公式:
      ∑ m = 0 m ( y ( i ) − a ∗ x ( i ) − b ) 2 ,一元线性回归算法公式 \sum_{m=0}^{m} ({y^{(i)} -a*x^{(i)}-b)^2} \text {,一元线性回归算法公式} m=0m(y(i)ax(i)b)2,一元线性回归算法公式
    • 原理:找到a和b使得上述公式的值尽可能的小,经过最小二乘法求解可以计算出a值是:
      a = ∑ m = 0 m ( x ( i ) − x ‾ ) ∗ ( y ( i ) − y ˉ ) ∑ m = 0 m ( x ( i ) − x ˉ ) 2 ,a的值 a=\frac{ \sum_{m=0}^{m}(x^{(i)}-\overline{x})*(y^{(i)}-\bar{y })}{ \sum_{m=0}^{m}(x^{(i)}-\bar{x })^2} \text {,a的值} a=m=0m(x(i)xˉ)2m=0m(x(i)x)(y(i)yˉ)a的值
      b = y ˉ − a ∗ x ˉ ,b的值 b=\bar{y }-a*\bar{x }\text {,b的值} b=yˉaxˉb的值
    • 使用代码实现如下:这种方式实现的代码时间复杂度很大O(n^3)不建议使用
class SimpleLinearRegression1:
    def __init__(self):
        self.a_ = None
        self.b_ = None

    def fit(self, x_train, y_train):
        """根据训练数据集x_train,y_train训练Simple Linear Regression模型"""
        if x_train.ndim != 1:
            raise Exception("简单回归的维度只能是一维")
        if len(x_train) != len(y_train):
            raise Exception("x_train的行数需要和y_train的行数相同")
        """计算训练集平均值"""
        x_mean = np.mean(x_train)
        """计算训练集平均值"""
        y_mean = np.mean(y_train)

        num = 0.0  # 分子
        d = 0.0  # 分母
        """循环得出x,y的值"""
        for x, y in zip(x_train, y_train):
            """利用最小二乘法计算出a、b的值"""
            num += (x - x_mean) * (y - y_mean)
            d += (x - x_mean) ** 2
        self.a_ = num / d
        self.b_ = y_mean - self.a_ * x_mean
        return self

    def predict(self, x_predict):
        """x_predict是待预测的数据集,输入这个x_predict会得到一个预测值"""
        if x_predict.ndim != 1:
            raise Exception("简单回归的维度只能是一维")
        if self.a_ is None and self.b_ is None:
            raise Exception("预测之前需要先拟合")
        res = []
        for x in x_predict:
            res.append(self._predict(x))
            """封装为np.array格式数据"""
        return np.array(res)

    def _predict(self, x_single):
        """转化为函数y=a*x+b"""
        return self.a_ * x_single + self.b_
  • 第二方式实现
    a = ( x ( i ) − x ‾ ) ⋅ ( y ( i ) − y ˉ ) ( x ( i ) − x ˉ ) 2 ,a的值 a=\frac{(x^{(i)}-\overline{x})\cdot(y^{(i)}-\bar{y })}{ (x^{(i)}-\bar{x })^2} \text {,a的值} a=(x(i)xˉ)2(x(i)x)(y(i)yˉ)a的值
    b = y ˉ − a ∗ x ˉ ,b的值 b=\bar{y }-a*\bar{x }\text {,b的值} b=yˉaxˉb的值
    • 代码如下:
class SimpleLinearRegression2:
    def __init__(self):
        self.a_ = None
        self.b_ = None

    def fit(self, x_train, y_train):
        """根据训练数据集x_train,y_train训练Simple Linear Regression模型"""
        if x_train.ndim != 1:
            raise Exception("简单回归的维度只能是一维")
        if len(x_train) != len(y_train):
            raise Exception("x_train的行数需要和y_train的行数相同")
        x_mean = np.mean(x_train)
        y_mean = np.mean(y_train)

        num = 0.0  # 分子
        d = 0.0  # 分母
        num = (x_train - x_mean).dot(y_train - y_mean)
        d = (x_train - x_mean).dot(x_train - x_mean)
        self.a_ = num / d
        self.b_ = y_mean - self.a_ * x_mean
        return self

    def predict(self, x_predict):
        """x_predict是待预测的数据集,输入这个x_predict会得到一个预测值"""
        if x_predict.ndim != 1:
            raise Exception("简单回归的维度只能是一维")
        if self.a_ is None and self.b_ is None:
            raise Exception("预测之前需要先拟合")
        res = []
        for x in x_predict:
            res.append(self._predict(x))
        return np.array(res)

    def score(self, x_test, y_test):
        y_predict = self.predict(x_test)
        return r2_score(y_test, y_predict)

    def _predict(self, x_single):
        return self.a_ * x_single + self.b_
  • 评价一盒回归算法的好坏可以利用MSE(均方误差)、RMSE(均方根误差)、MAE(平均绝对误差)、R2(决定系数)实现代码如下
def mean_squared_error(y_true, y_predict):
    if len(y_true) != len(y_predict):
        raise Exception("y_ture的长度应该和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)


def r2_score(y_true, y_predict):
    return 1 - mean_squared_error(y_true, y_predict) / np.var(y_true)

多元线性回归:

  • 公式
    y = θ 0 + θ 1 x 1 + θ 2 x 2 + θ 3 x 3 + . . . . . . + θ n x n ,y的值 y=\theta_{0}+\theta_{1}x_{1}+\theta_{2}x_{2}+\theta_{3}x_{3}+......+\theta_{n}x_{n}\text {,y的值} y=θ0+θ1x1+θ2x2+θ3x3+......+θnxny的值
  • 分解这个函数可以得到:
    y ^ ( i ) = x 0 + x 1 + x 2 + x 3 + . . . . . . + x n \widehat{y}^{(i)}=x_{0}+x_{1}+x_{2}+x_{3}+......+x_{n} y (i)=x0+x1+x2+x3+......+xn
    θ = ( θ 0 + θ 1 + θ 2 + θ 3 + . . . . . . + θ n ) T \theta_=(\theta_{0}+\theta_{1}+\theta_{2}+\theta_{3}+......+\theta_{n}) ^{T} θ=(θ0+θ1+θ2+θ3+......+θn)T根据向量化运算可以得到 θ = ( x b ⋅ x b ) T ⋅ x b T ⋅ y \theta_=(x_{b}\cdot x_{b})^{T}\cdot x_{b}^{T}\cdot y θ=(xbxb)TxbTy 函数的截距就是 θ 0 \theta_{0} θ0系数是 θ 1 . . . . . . θ n \theta_{1}......\theta_{n} θ1......θn
  • y值,就是数学函数输出的值是
    y = x b ⋅ θ y=x_{b}\cdot \theta_{} y=xbθ θ = ( x b ⋅ x b ) T ⋅ x b T ⋅ y \theta_=(x_{b}\cdot x_{b})^{T}\cdot x_{b}^{T}\cdot y θ=(xbxb)TxbTy
    np.hstack():在水平方向上平铺
    X_b = np.hstack([np.ones((len(X_train), 1)), X_train])
  • 多元线性回归代码如下:
# -*- coding: utf-8 -*-
# @Time    : 2021/7/30 10:56
# @Email   : 2635681517@qq.co
import numpy as np

from metrics import r2_score


class LinearRegression:
    def __init__(self):
        self.coef_ = None
        self.intercept_ = None
        self.theta_ = None

    def fit_normal(self, X_train, y_train):
        """根据训练数据集X_train, y_train训练Linear Regression模型"""
        if X_train.shape[0] != y_train.shape[0]:
            raise Exception("X_train的行数需要和y_train的行数相同")
        X_b = np.hstack([np.ones((len(X_train), 1)), X_train])
        self.theta_ = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y_train)
        self.intercept_ = self.theta_[0]
        self.coef_ = self.theta_[1:]
        return self

    def predict(self, X_predict):
        """给定待预测数据集X_predict,返回表示X_predict的结果向量"""
        assert self.intercept_ is not None and self.coef_ is not None, \
            "must fit before predict!"
        assert X_predict.shape[1] == len(self.coef_), \
            "the feature number of X_predict must be equal to X_train"

        X_b = np.hstack([np.ones((len(X_predict), 1)), X_predict])
        return X_b.dot(self.theta_)

    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 "LinearRegression()"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值