Home > Enterprise >  Using coefficients on sklearn Ridge model to manually predict an entry
Using coefficients on sklearn Ridge model to manually predict an entry

Time:09-15

This is very likely a newbie question but I haven't been able to find an answer. I have created a model with sklearn which uses Ridge from linear_models. After I fit the model, I get the coefficients and the intercept to manually compute a prediction.

The problem I am having is that I'm getting a mismatch between manually multiplying each coefficient to its corresponding variable and later adding the intercept (wi*xi c) and the result given by model.predict(X). Is this an expected behavior? How could I correctly do a manual prediction (I wish to translate these results to a spreadsheet later)

Thanks!

CodePudding user response:

Not sure what happened, but if you take the dot product between coefficients and the dependent variable matrix, you should get the exact prediction:

from sklearn.linear_model import Ridge
import numpy as np
n_samples, n_features = 10, 5
rng = np.random.RandomState(0)
y = rng.randn(n_samples)
X = rng.randn(n_samples, n_features)
clf = Ridge(alpha=1.0)
clf.fit(X, y)

The prediction :

print(clf.predict(X))

array([ 0.95424223,  0.48712089,  1.10388121,  1.79105759,  1.05769745,
       -0.07915715,  1.12314109, -0.22941441,  0.64906337,  0.52259943])

If we use the the product :

print(np.dot(X,clf.coef_)   clf.intercept_)

[ 0.95424223  0.48712089  1.10388121  1.79105759  1.05769745 -0.07915715
  1.12314109 -0.22941441  0.64906337  0.52259943]
  • Related