(Scikit-Learn)线性回归 基函数的含义详解

线性回归:将数据拟合成一条直线。直线拟合的模型方程为 y = ax + b,其中 a 是直线斜率, b 是直线截距。

看看下面的数据,它们是从斜率为 2、截距为 -5 的直线中抽取的散点

%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
import numpy as np

rng = np.random.RandomState(1)
x = 10 * rng.rand(50)
y = 2 * x - 5 + rng.randn(50)
plt.scatter(x,y)

在这里插入图片描述
用 Scikit-Learn 的 LinearRegression 评估器来拟合数据,并获得最佳拟合直线

from sklearn.linear_model import LinearRegression
model = LinearRegression(fit_intercept=True)
model.fit(x[:, np.newaxis], y)

xfit = np.linspace(0, 10, 1000)
yfit = model.predict(xfit[:, np.newaxis])

plt.scatter(x, y)
plt.plot(xfit, yfit, c='red')

在这里插入图片描述
模型的参数:

print('Model slope: ', model.coef_[0])
print('Model intercept: ', model.intercept_)

在这里插入图片描述


LinearRegression 评估器能做的可远不止这些——除了简单的直线拟合,它还可以处理多维度的线性回归模型:
y = a0 + a1x1 + a2x2 + …
这个模型是拟合三维空间中的一个平面,或者是为更高维度的数据点拟合一个超平面。

rng = np.random.RandomState(1)
X = 10 * rng.rand(100, 3)
y = 0.5 + np.dot(X, [1.5, -2, 1.])

model.fit(X, y)
print(model.coef_)
print(model.intercept_)

在这里插入图片描述
通过这种方式,就可以用一个 LinearRegression 评估器拟合数据的回归直线、平面和超平 面了。虽然这种方法还是有局限性,因为它将变量限制在了线性关系上,但是不用担心, 还有其他方法。


基函数:将数据投影到高维空间

对y = a0 + a1x + a2x^2 + a3x^3 + …这种单变量多项式函数的情况
这里我们可以将x^2, x^3…看成变量x2,x3,那么函数就转换成了y = a0 + a1x + a2x2 + a3x3 + …
是关于三个变量的线性函数,因此我们可以使用线性回归来拟合


基函数作用:
例如使用多项式去拟合sin(x)函数:y=sin(x)
数据集如下:
在这里插入图片描述
为了得到更好的结果,我们假设y是x的3次多项式。于是我们可以将数据扩展为:x, x^2, x^3三维数据,然后在这个三维空间中寻找三个变量与y的线性关系。
在这里插入图片描述
我们将x, x^2, x^3…看成变量x1,x2,x3,那么拟合函数就转换成了y = a0 + a1x1 + a2x2 + a3x3 + …

对于任何一个新的测试数据Test_x,我们将会先对其进行扩展,然后使用模型预测:
在这里插入图片描述
最终,我们就得到了,有关x和y的非线性函数关系了。


使用基函数对原始数据进行变换,Scikit-Learn 内置了 PolynomialFeatures 转换器实现这个功能

from sklearn.preprocessing import PolynomialFeatures
x = np.array([2, 3, 4])
poly = PolynomialFeatures(3, include_bias=False)
poly.fit_transform(x[:, None])

在这里插入图片描述
转换器通过指数函数,将一维数组转换成了三维数组。

让我们创建一个 7 次多项式回归模型,来拟合sin(x)函数

from sklearn.pipeline import make_pipeline

poly_model = make_pipeline(PolynomialFeatures(7), LinearRegression())

rng = np.random.RandomState(1)
x = 10 * rng.rand(50)
y = np.sin(x) + 0.1 * rng.randn(50)

poly_model.fit(x[:, np.newaxis], y)
yfit = poly_model.predict(xfit[:, np.newaxis])

plt.scatter(x, y)
plt.plot(xfit, yfit)

在这里插入图片描述
创建一个1,3,5,7奇数阶的多项式拟合sin(x)

rng = np.random.RandomState(1)
x = 10 * rng.rand(50)
y = np.sin(x) + 0.1 * rng.randn(50)

poly = PolynomialFeatures(7, include_bias=False)
x_p = poly.fit_transform(x[:,np.newaxis])
print(x_p.shape)
x_p = x_p[:, [0,2,4,6]]
x_p.shape
model = LinearRegression()
model.fit(x_p, y)

xfit_p = poly.fit_transform(xfit[:,np.newaxis])
xfit_p = xfit_p[:,[0,2,4,6]]

yfit = model.predict(xfit_p)

plt.scatter(x, y)
plt.plot(xfit_p[:,0], yfit, c='red')

在这里插入图片描述


正则化
基函数通过扩展(转换)数据的方式,使得线性模型具有了更加强大的功能(比如:可以使用单变量多项式函数,拟合任意复杂的函数
但是,当扩展的维度太大时,会出现过拟合的现象。下面我们使用 16 次方的函数去拟合sin(x)函数,拟合的结果并不好。

from sklearn.pipeline import make_pipeline

poly_model = make_pipeline(PolynomialFeatures(16), LinearRegression())

rng = np.random.RandomState(1)
x = 10 * rng.rand(50)
y = np.sin(x) +  0.1 * rng.randn(50)

poly_model.fit(x[:, np.newaxis], y)
yfit = poly_model.predict(xfit[:, np.newaxis])

plt.scatter(x, y)
plt.plot(xfit, yfit)

在这里插入图片描述
将数据投影到 16 维的基函数上,模型就会变得过于灵活,从而能够适应数据中不同位置的异常值

对较大的模型参数进行惩罚(penalize),从而抑制模型剧烈波动

岭回归(L2范数正则化) 获得较均衡的模型系数
在这里插入图片描述
其中, α 是一个自由参数,用来控制惩罚的力度。这种带惩罚项的模型内置在 Scikit-Learn 的 Ridge 评估器中

from sklearn.pipeline import make_pipeline
from sklearn.linear_model import Ridge

poly_model = make_pipeline(PolynomialFeatures(16), Ridge(alpha=0.1))

rng = np.random.RandomState(1)
x = 10 * rng.rand(50)
y = np.sin(x) +  0.1 * rng.randn(50)

poly_model.fit(x[:, np.newaxis], y)
yfit = poly_model.predict(xfit[:, np.newaxis])

plt.scatter(x, y)
plt.plot(xfit, yfit)

在这里插入图片描述
Lasso正则化(L1范数)
另一种常用的正则化被称为 Lasso,其处理方法是对模型系数绝对值的和(L1 范数)进行惩罚:
在这里插入图片描述
Lasso 正则化倾向于构建稀疏模型;也就是说,它更喜欢将模型系数设置为 0。

from sklearn.linear_model import Lasso

poly_model = make_pipeline(PolynomialFeatures(16), Lasso(alpha=0.01))
poly_model.fit(x[:, np.newaxis], y)
yfit = poly_model.predict(xfit[:, np.newaxis])

plt.scatter(x, y)
plt.plot(xfit, yfit)

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值