(六)Ridge回归--岭回归

2020-04-07  本文已影响0人  羽天驿

一、本质是改良后的最小二乘法。

原理1.png
原理2.png
作用.png

岭回归是一种专用于共线性数据分析的有偏估计回归方法,实质上是一种改良的最小二乘估计法,通过放弃最小二乘法的无偏性(精确解),以损失部分信息、降低精度为代价获得回归系数更为符合实际、更可靠的回归方法,对病态数据的拟合要强于OLS



二、代码

(一。岭回归)

这两个模型,都是解方程,构建模型

对比,建模的哪些不同

import numpy as np

from sklearn.linear_model import LinearRegression,Ridge

from sklearn import datasets
d:\python3.7.4\lib\importlib\_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject
  return f(*args, **kwds)
X,y = datasets.load_boston(True)
X.shape
(506, 13)
linear = LinearRegression()
linear.fit(X,y)
w_ = linear.coef_
b_ = linear.intercept_
print('普通线性回归斜率是:\n',w_)
print('普通线性回归截距是:',b_)
普通线性回归斜率是:
 [-1.08011358e-01  4.64204584e-02  2.05586264e-02  2.68673382e+00
 -1.77666112e+01  3.80986521e+00  6.92224640e-04 -1.47556685e+00
  3.06049479e-01 -1.23345939e-02 -9.52747232e-01  9.31168327e-03
 -5.24758378e-01]
普通线性回归截距是: 36.45948838509001
ridge = Ridge(alpha= 10)
ridge.fit(X,y)
w_ = ridge.coef_
b_ = ridge.intercept_
print('岭回归斜率是:\n',w_)
print('岭回归截距是:',b_)
岭回归斜率是:
 [-0.10143535  0.0495791  -0.0429624   1.95202082 -2.37161896  3.70227207
 -0.01070735 -1.24880821  0.2795956  -0.01399313 -0.79794498  0.01003684
 -0.55936642]
岭回归截距是: 27.467884964141252
ridge = Ridge(alpha= 0)
ridge.fit(X,y)
w_ = ridge.coef_
b_ = ridge.intercept_
print('岭回归斜率是:\n',w_)
print('岭回归截距是:',b_)
岭回归斜率是:
 [-1.08011358e-01  4.64204584e-02  2.05586264e-02  2.68673382e+00
 -1.77666112e+01  3.80986521e+00  6.92224640e-04 -1.47556685e+00
  3.06049479e-01 -1.23345939e-02 -9.52747232e-01  9.31168327e-03
 -5.24758378e-01]
岭回归截距是: 36.45948838508981
上一篇下一篇

猜你喜欢

热点阅读