This modification seems to give the same result for the direct version and the numpy version:
import numpy as np
A = np.asmatrix(np.random.rand(10,3))
b = np.asmatrix(np.random.rand(10,1))
I = np.identity(A.shape[1])
alpha = 1
x = np.linalg.inv(A.T*A + alpha * I)*A.T*b
print (x.T)
from sklearn.linear_model import Ridge
model = Ridge(alpha = alpha, tol=0.1, fit_intercept=False).fit(A ,b)
print model.coef_
print model.intercept_
, Ridge fit_intercept=True ( _BaseRidge) (source)
_solve_cholesky.
ridge.py,
X, y, X_mean, y_mean, X_std = self._center_data(
X, y, self.fit_intercept, self.normalize, self.copy_X,
sample_weight=sample_weight)
, , , 1. , , fit_intercept=False
: Ridge ?
solver.
, solver Ridge, solver='auto' ( solver='cholesky'). .
, _solve_cholesky numpy.linalg.solve numpy.inv. ,
np.linalg.solve(A.T*A + alpha * I, A.T*b)
,
np.linalg.inv(A.T*A + alpha * I)*A.T*b