4
$\begingroup$

I had constructed a simple Multiple linear regression model, where I have 2 independent variables and a target (dependent variable).

Now, I transformed my independent variable using StandardScaler.fit_transform() and ran my model. The problem is the coefficient that I am getting is for the scaled dataset now. They are insanely large for my use.

Is there any way that I can get a coefficient from the model that will work with my non-scaled data?

PS: What I am looking for is software implementation so that I can get intercept and 2 coefficients all of which will be used later in the program. Right now my coefficient will only work if my input data (test data) is also standardized.

$\endgroup$

1 Answer 1

2
$\begingroup$

Before using the model, you should also scale your test data with the same coefficients that you found using the training set.

Alternatively, you can also rewrite your regression equation as follows by hand: $$\begin{align}y&=\alpha x_1'+\beta x_2'+\theta=\alpha\frac{x_1-\mu}{\sigma_1}+\beta\frac{x_2-\mu_2}{\sigma_2}+\theta\\&=\frac{\alpha}{\sigma_1}x_1+\frac{\beta}{\sigma_2}x_2+\left(\theta-\frac{\alpha\mu_1}{\sigma_1}-\frac{\beta\mu_2}{\sigma_2}\right)\end{align}$$

Where $x_1',x_2'$ are standardized independent variables, and $\alpha, \beta, \theta$ are the coefficients of the standardized model. I'd definitely prefer the first approach as it is equivalent and you'll stay in the library implementation.

$\endgroup$
5
  • 1
    $\begingroup$ Thanks for the post. What I was looking for is software implementation so that I can get intercept and 2 coefficient which will be used later in the program. $\endgroup$ Commented Apr 14, 2022 at 8:52
  • $\begingroup$ You can get intercept_ and coef_ as described here: scikit-learn.org/stable/modules/generated/… $\endgroup$
    – gunes
    Commented Apr 14, 2022 at 8:55
  • $\begingroup$ Yes obviously these two method will give me the coefficient but they would only work with scaled values not with unscaled. $\endgroup$ Commented Apr 14, 2022 at 11:25
  • $\begingroup$ You can transform them as described above, or you can transform your test set with the StandardScaler object you fitted to the training set $\endgroup$
    – gunes
    Commented Apr 14, 2022 at 11:28
  • $\begingroup$ @gunes Thanks a lot. Your answer helped me. $\endgroup$ Commented Aug 15, 2022 at 22:00

Your Answer

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

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