0
$\begingroup$

I am creating a polynomial regression model with Python sci kit learn package, and I was wondering how I can use the predict features in machine learning algorithms.

To start with Python scikit-learn I am fitting the model (shown in my other post) and printing rsme & r2 which outputs:

2.359112756782707
0.9829246178225791

The plotted model is purple:

enter image description here

If I print the coefficients:

print(model.coef_)

print(model.intercept_)

I get an array:

[[ 0.00000000e+00 -4.17544080e-01  2.87295974e-02 -2.06211620e-04]]
[73.99115377]

This may sound like a silly question, but how do I use this to predict values? For example, can I use my model to predict a y value? Would an x value of 40 according to my scatter plot equate to an approximate y value of ~90?

Xnew = [[40]]
ynew = model.predict(Xnew)

ynew 

This gives me an error as I do not understand this concepts of linear algebra/vector.. I think I need a format that matches my print(model.coef_) like this below but I don't understand enough to put it to use. Any really basic 101 tips would be greatly appreciated...

Xnew = [[...], [...], [...], [...]]
ynew = model.predict(Xnew)
$\endgroup$
2
  • $\begingroup$ The basic of polynomial regression is that before you fit your model, you first add additional variables to your data. These variables are derived from your original data in the following way: If you add quadratic terms to your regression formula, you just square your (numerical) data. If you add a cubic term, you take it to the power of 3 etc... With these new variablse you then fit a normal linear regression. $\endgroup$
    – PeterD
    Jan 10, 2019 at 18:24
  • $\begingroup$ Thanks Ill have to do some research on these steps.. $\endgroup$
    – bbartling
    Jan 10, 2019 at 18:29

1 Answer 1

1
$\begingroup$

You didn't apply the polynomial transformation to Xnew, so it doesn't have the appropriate size, nor is Xnew the polynomial data that you desire.

To fix this, apply the polynomial transformation to Xnew. Here's how that looks:

x_new_poly = polynomial_features.transform(Xnew)
model.predict(x_new_poly)
$\endgroup$
3
  • $\begingroup$ Can you elaborate more in your answer, sorry I don't understand. $\endgroup$
    – bbartling
    Jan 10, 2019 at 18:28
  • 1
    $\begingroup$ What don't you understand? The model object has polynomially-transformed data as an input. However, when you call predict, you did not transform the input to a polynomial. In other words, model doesn't know that you need to transform the data; model just expects to receive data of a specific size, and then it does a matrix-vector multiply. End of story. All transformations are incumbent on you. $\endgroup$
    – Sycorax
    Jan 10, 2019 at 18:30
  • $\begingroup$ That is also what i mean with extra variables (the polynomially transformed data) $\endgroup$
    – PeterD
    Jan 10, 2019 at 18:35

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.