Why is the output from Matlab and Python vary for ridge regression?
I use the ridge
command in Matlab and scikit-learn
in Python for ridge regression.
Matlab
X = [1 1 2 ; 3 4 2 ; 6 5 2 ; 5 5 3];
Y = [1 0 0 1];
k = 10 % which is the ridge parameter
b = ridge(Y,X,k,0)
The coefficients are estimated as
b = 0.3057 -0.0211 -0.0316 0.1741
Python
import numpy as np
X = np.array([[1, 1, 2] , [3, 4, 2] , [6, 5, 2] , [5, 5, 3]])
Y = np.r_[1,0,0,1].T
from sklearn import linear_model
clf = linear_model.Ridge(alpha=10)
clf.fit(X, Y)
b = np.hstack((clf.intercept_, clf.coef_))
The coefficients are estimated as
b = 0.716 -0.037 -0.054 0.057
Why is this difference observed?
EDIT: For people who think that centering and scaling is the issue. The input data is not scaled or centered as I had used the scaled parameter as 0 as observed from
b = ridge(Y,X,k,0)
and ridge regression in scikit-learn
by default does not do normalization
>>clf
Ridge(alpha=10, copy_X=True, fit_intercept=True, max_iter=None, normalize=False, solver='auto', tol=0.001)
And here is the Matlab output when it is normalised b = ridge(Y,X,k,1)
:
b = -0.0467 -0.0597 0.0870