简单线性回归
- 简单线性回归包含一个自变量(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=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)不建议使用
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ˉ??a?xˉ,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?+θ1?x1?+θ2?x2?+θ3?x3?+......+θn?xn?,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]) - 多元线性回归代码如下:
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()"
|