-1
$\begingroup$

I wrote a program to find the best combination of coefficients to describe a variable. However, the coefficients from the gridsearchcv do not match well with the expected line. This is a sample of my data:

enter image description here

pipe = make_pipeline(process, SelectKBest(f_regression), model)
gs=GridSearchCV(pipe,params,n_jobs=-1,cv=5, return_train_score = False);
gs.fit(x_train, y_train)
fin = gs.best_estimator_.steps[2][1]; 
coef = fin.coef_; 
intercept = fin.intercept_

and these are the coefficients given:

enter image description here

Then if I plot the line with the coefficients:

xplot = 16.15589 + 1.13934372*df_loc.ChargeAmount + 1.605411*df_loc.PatientPrice + 6.81365603*df_loc.LastCost
plt.scatter(xplot, df_locpre.MSRP, color = 'black');
plt.plot(df_locpre.MSRP, df_locpre.MSRP, color = 'blue')

I obtain the following figure:

enter image description here

I think that either the scale is wrong, or some coefficients might be negative. Could you help me figure out where I am going wrong?

$\endgroup$

2 Answers 2

0
$\begingroup$

After some digging, it seems that the coefficient are simply scaled by the scaler I use. I have to reverse the transformation. I have not found a good way to do this automatically, so I followed this: https://www.tutorialguruji.com/python/how-linear-regression-coefficients-are-stored-in-sklearn-pipelines/amp/

If you know anything that might spare me from hard computing the values, please let me know!

$\endgroup$
3
  • $\begingroup$ Assuming that you are using StandardScaler to scale your data, you can simply use the inverse_transform method to transform the data back. $\endgroup$
    – Oxbowerce
    Dec 15, 2021 at 11:36
  • $\begingroup$ @Oxbowerce does this still apply if the transformation is inside a pipeline inside a specified search method (gridcv here)? I could not make good use of it to find coefficients $\endgroup$ Dec 15, 2021 at 22:46
  • $\begingroup$ The GridSearchCV class also has an inverse_transform method, so that might work. Otherwise you can access the different steps within the pipeline using simple the steps attribute, so you could use that to select the scaler and use the inverse_transform method. $\endgroup$
    – Oxbowerce
    Dec 16, 2021 at 8:06
0
$\begingroup$

I would just lean on the pipeline: predict instead of manually using the coefficients.

xplot = pipe.predict(df_locpre)
$\endgroup$
1
  • $\begingroup$ would I still be able to get the unscaled coefficients from predict? The issue is that I have to output these coefficients and not just the resulting prediction. $\endgroup$ Dec 15, 2021 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.