Machine learning 理解sci工具包学习中的岭线性回归
我试图理解岭回归是如何在scikit learn中实现的 岭回归具有最小化(y-Xw)^2+\alpha*| w | ^2的闭式解,即(X'*X+\alpha*I)^{-1}X'y 拟合模型的截距和系数似乎与闭式解不同。有没有关于如何在scikit learn中实现岭回归的想法Machine learning 理解sci工具包学习中的岭线性回归,machine-learning,scikit-learn,linear-regression,Machine Learning,Scikit Learn,Linear Regression,我试图理解岭回归是如何在scikit learn中实现的 岭回归具有最小化(y-Xw)^2+\alpha*| w | ^2的闭式解,即(X'*X+\alpha*I)^{-1}X'y 拟合模型的截距和系数似乎与闭式解不同。有没有关于如何在scikit learn中实现岭回归的想法 from sklearn import datasets from sklearn.linear_model import Ridge import matplotlib.pyplot as plt import num
from sklearn import datasets
from sklearn.linear_model import Ridge
import matplotlib.pyplot as plt
import numpy as np
# prepare dataset
boston = datasets.load_boston()
X = boston.data
y = boston.target
# add the w_0 intercept where the corresponding x_0 = 1
Xp = np.concatenate([np.ones((X.shape[0], 1)), X], axis=1)
alpha = 0.5
ridge = Ridge(fit_intercept=True, alpha=alpha)
ridge.fit(X, y)
# 1. intercept and coef of the fit model
print np.array([ridge.intercept_] + list(ridge.coef_))
# output:
# array([ 3.34288615e+01, -1.04941233e-01, 4.70136803e-02,
2.52527006e-03, 2.61395134e+00, -1.34372897e+01,
3.83587282e+00, -3.09303986e-03, -1.41150803e+00,
2.95533512e-01, -1.26816221e-02, -9.05375752e-01,
9.61814775e-03, -5.30553855e-01])
# 2. the closed form solution
print np.linalg.inv(Xp.T.dot(Xp) + alpha * np.eye(Xp.shape[1])).dot(Xp.T).dot(y)
# output:
# array([ 2.17772079e+01, -1.00258044e-01, 4.76559911e-02,
-6.63573226e-04, 2.68040479e+00, -9.55123875e+00,
4.55214996e+00, -4.67446118e-03, -1.25507957e+00,
2.52066137e-01, -1.15766049e-02, -7.26125030e-01,
1.14804636e-02, -4.92130481e-01])
棘手的一点是拦截。您拥有的封闭式解决方案是针对缺少截距,当您在数据中添加一列1时,您也会在截距项上添加L2惩罚。Scikit学习岭回归不会 如果您想对偏差进行L2惩罚,那么只需在
Xp
上调用ridge(并在构造函数中关闭fitting bias),就可以得到:
>>> ridge = Ridge(fit_intercept=False, alpha=alpha)
>>> ridge.fit(Xp, y)
>>> print np.array(list(ridge.coef_))
[ 2.17772079e+01 -1.00258044e-01 4.76559911e-02 -6.63573226e-04
2.68040479e+00 -9.55123875e+00 4.55214996e+00 -4.67446118e-03
-1.25507957e+00 2.52066137e-01 -1.15766049e-02 -7.26125030e-01
1.14804636e-02 -4.92130481e-01]
棘手的一点是拦截。您拥有的封闭式解决方案是针对缺少截距,当您在数据中添加一列1时,您也会在截距项上添加L2惩罚。Scikit学习岭回归不会 如果您想对偏差进行L2惩罚,那么只需在
Xp
上调用ridge(并在构造函数中关闭fitting bias),就可以得到:
>>> ridge = Ridge(fit_intercept=False, alpha=alpha)
>>> ridge.fit(Xp, y)
>>> print np.array(list(ridge.coef_))
[ 2.17772079e+01 -1.00258044e-01 4.76559911e-02 -6.63573226e-04
2.68040479e+00 -9.55123875e+00 4.55214996e+00 -4.67446118e-03
-1.25507957e+00 2.52066137e-01 -1.15766049e-02 -7.26125030e-01
1.14804636e-02 -4.92130481e-01]
解析解是正确的 (X'X+αI)-1x'y 但问题是X和y是什么。实际上有两种不同的解释:
alpha = 0.5
ridge = Ridge(fit_intercept=True, alpha=alpha)
ridge.fit(X, y - np.mean(y))
# 1. intercept and coef of the fit model
print np.array([ridge.intercept_] + list(ridge.coef_))
Xp = Xp - np.mean(Xp, axis=0)
# 2. the closed form solution
print np.linalg.inv(Xp.T.dot(Xp) + alpha * np.eye(Xp.shape[1])).dot(Xp.T).dot(y)
然后您将看到相同的结果。您正确地认为解析解是正确的 (X'X+αI)-1x'y 但问题是X和y是什么。实际上有两种不同的解释:
alpha = 0.5
ridge = Ridge(fit_intercept=True, alpha=alpha)
ridge.fit(X, y - np.mean(y))
# 1. intercept and coef of the fit model
print np.array([ridge.intercept_] + list(ridge.coef_))
Xp = Xp - np.mean(Xp, axis=0)
# 2. the closed form solution
print np.linalg.inv(Xp.T.dot(Xp) + alpha * np.eye(Xp.shape[1])).dot(Xp.T).dot(y)
然后您将看到相同的结果。作为lejlot答案的补充:有多个解算器可用
solver='cholesky'
正是使用了这种封闭形式的解决方案。(如答案中所述:差异隐藏在其他地方)作为lejlot答案的补充:有多个解算器可用solver='cholesky'
正是使用了这种封闭形式的解决方案。(如回答中所述:差异隐藏在其他地方)很抱歉回答了同样的问题,本质上-在回答之前开始回答,但在回答之后结束。回答得好。@lejlot谢谢你的回答,但是有没有一个表达式可以产生与“ridge=ridge(fit_intercept=True,alpha=alpha);ridge.fit(X,y)”相同的结果?w=(X'X+aI)^(-1)X'(y-avg y),intercept=avg y
应该产生同样的结果对不起,回答同样的问题,基本上是在你回答之前就开始回答了,但之后就结束了。回答得好。@lejlot感谢您的回答,但是有没有一个表达式可以产生与“ridge=ridge(fit_intercept=True,alpha=alpha);ridge.fit(X,y)”相同的结果?w=(X'X+aI)^(-1)X'(y-avg y),intercept=avg y
应该产生相同的结果