公式:
∑
m
=
0
m
(
y
(
i
)
−
a
∗
x
(
i
)
−
b
)
2
,一元线性回归算法公式
\sum_{m=0}^{m} ({y^{(i)} -a*x^{(i)}-b)^2} \text {,一元线性回归算法公式}
m=0∑m(y(i)−a∗x(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ˉ)2∑m=0m(x(i)−x)∗(y(i)−yˉ),a的值
b
=
y
ˉ
−
a
∗
x
ˉ
,b的值
b=\bar{y }-a*\bar{x }\text {,b的值}
b=yˉ−a∗xˉ,b的值
使用代码实现如下:这种方式实现的代码时间复杂度很大O(n^3)不建议使用
classSimpleLinearRegression1:def__init__(self):
self.a_ =None
self.b_ =Nonedeffit(self, x_train, y_train):"""根据训练数据集x_train,y_train训练Simple Linear Regression模型"""if x_train.ndim !=1:raise Exception("简单回归的维度只能是一维")iflen(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 inzip(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
defpredict(self, x_predict):"""x_predict是待预测的数据集,输入这个x_predict会得到一个预测值"""if x_predict.ndim !=1:raise Exception("简单回归的维度只能是一维")if self.a_ isNoneand self.b_ isNone: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ˉ−a∗xˉ,b的值
代码如下:
classSimpleLinearRegression2:def__init__(self):
self.a_ =None
self.b_ =Nonedeffit(self, x_train, y_train):"""根据训练数据集x_train,y_train训练Simple Linear Regression模型"""if x_train.ndim !=1:raise Exception("简单回归的维度只能是一维")iflen(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
defpredict(self, x_predict):"""x_predict是待预测的数据集,输入这个x_predict会得到一个预测值"""if x_predict.ndim !=1:raise Exception("简单回归的维度只能是一维")if self.a_ isNoneand self.b_ isNone:raise Exception("预测之前需要先拟合")
res =[]for x in x_predict:
res.append(self._predict(x))return np.array(res)defscore(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_
公式
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+......+θnxn,y的值
分解这个函数可以得到:
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
θ=(xb⋅xb)T⋅xbT⋅y 函数的截距就是
θ
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
θ=(xb⋅xb)T⋅xbT⋅y 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.coimport numpy as np
from metrics import r2_score
classLinearRegression:def__init__(self):
self.coef_ =None
self.intercept_ =None
self.theta_ =Nonedeffit_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
defpredict(self, X_predict):"""给定待预测数据集X_predict,返回表示X_predict的结果向量"""assert self.intercept_ isnotNoneand self.coef_ isnotNone, \
"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_)defscore(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()"