🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Python Notes
Topic #137

Polynomial Regression


Polynomial Regression

If your data points clearly will not fit a linear regression (a straight line through all data points), it might be ideal for polynomial regression.

Polynomial regression, like linear regression, uses the relationship between the variables x and y to find the best way to draw a line through the data points.

image


How Does it Work?

Python has methods for finding a relationship between data-points and to draw a line of polynomial regression. We will show you how to use these methods instead of going through the mathematic formula.

In the example below, we have registered 18 cars as they were passing a certain tollbooth.

We have registered the car's speed, and the time of day (hour) the passing occurred.

The x-axis represents the hours of the day and the y-axis represents the speed:

Example

  import matplotlib.pyplot as plt

x = [1,2,3,5,6,7,8,9,10,12,13,14,15,16,18,19,21,22]
y = [100,90,80,60,60,55,60,65,70,70,75,76,78,79,90,99,99,100]

  plt.scatter(x, y)
plt.show()

image

Example

  import numpy
import matplotlib.pyplot as plt

x = [1,2,3,5,6,7,8,9,10,12,13,14,15,16,18,19,21,22]
y =
  [100,90,80,60,60,55,60,65,70,70,75,76,78,79,90,99,99,100]

mymodel =
  numpy.poly1d(numpy.polyfit(x, y, 3))

myline = numpy.linspace(1, 22, 100)

plt.scatter(x, y)
plt.plot(myline, mymodel(myline))
  plt.show()

image

Example Explained

Import the modules you need.

You can learn about the NumPy module in our NumPy Tutorial.

You can learn about the SciPy module in our SciPy Tutorial.

<p><code class="pythonHigh">import numpy<br/>import matplotlib.pyplot as plt</code></p>

Create the arrays that represent the values of the x and y axis:

<p><code class="pythonHigh">
  x = [1,2,3,5,6,7,8,9,10,12,13,14,15,16,18,19,21,22]<br/>y =
  [100,90,80,60,60,55,60,65,70,70,75,76,78,79,90,99,99,100]
</code></p>

NumPy has a method that lets us make a polynomial model:

<p><code class="pythonHigh">
  mymodel =
  numpy.poly1d(numpy.polyfit(x, y, 3))</code></p>

Then specify how the line will display, we start at position 1, and end at position 22:

<p><code class="pythonHigh">
  myline = numpy.linspace(1, 22, 100)</code></p>

Draw the original scatter plot:

<p><code class="pythonHigh">
plt.scatter(x, y)</code></p>

Draw the line of polynomial regression:

<p><code class="pythonHigh">
  plt.plot(myline, mymodel(myline))</code></p>

Display the diagram:

<p><code class="pythonHigh">
  plt.show()</code></p>

R-Squared

It is important to know how well the relationship between the values of the x- and y-axis is, if there are no relationship the polynomial regression can not be used to predict anything.

The relationship is measured with a value called the r-squared.

The r-squared value ranges from 0 to 1, where 0 means no relationship, and 1 means 100% related.

Python and the Sklearn module will compute this value for you, all you have to do is feed it with the x and y arrays:

Example

  import numpy
from sklearn.metrics import r2_score

x =
  [1,2,3,5,6,7,8,9,10,12,13,14,15,16,18,19,21,22]
y =
  [100,90,80,60,60,55,60,65,70,70,75,76,78,79,90,99,99,100]

mymodel =
  numpy.poly1d(numpy.polyfit(x, y, 3))

print(r2_score(y, mymodel(x)))

Note: The result 0.94 shows that there is a very good relationship, and we can use polynomial regression in future predictions.


Predict Future Values

Now we can use the information we have gathered to predict future values.

Example: Let us try to predict the speed of a car that passes the tollbooth at around the time 17:00:

To do so, we need the same mymodel array from the example above:

<p><code class="pythonHigh">
  mymodel = numpy.poly1d(numpy.polyfit(x, y, 3))</code></p>

Example

  import numpy
from sklearn.metrics import r2_score

x =
  [1,2,3,5,6,7,8,9,10,12,13,14,15,16,18,19,21,22]
y =
  [100,90,80,60,60,55,60,65,70,70,75,76,78,79,90,99,99,100]

mymodel =
  numpy.poly1d(numpy.polyfit(x, y, 3))

speed = mymodel(17)
print(speed)

The example predicted a speed to be 88.87, which we also could read from the diagram:

image


Bad Fit?

Let us create an example where polynomial regression would not be the best method to predict future values.

Example

  import numpy
import matplotlib.pyplot as plt

x =
  [89,43,36,36,95,10,66,34,38,20,26,29,48,64,6,5,36,66,72,40]
y =
  [21,46,3,35,67,95,53,72,58,10,26,34,90,33,38,20,56,2,47,15]

mymodel =
  numpy.poly1d(numpy.polyfit(x, y, 3))

myline = numpy.linspace(2, 95, 100)

plt.scatter(x, y)
plt.plot(myline, mymodel(myline))
plt.show()

image

And the r-squared value?

Example

  import numpy
from sklearn.metrics import r2_score

x =
  [89,43,36,36,95,10,66,34,38,20,26,29,48,64,6,5,36,66,72,40]
y =
  [21,46,3,35,67,95,53,72,58,10,26,34,90,33,38,20,56,2,47,15]

mymodel =
  numpy.poly1d(numpy.polyfit(x, y, 3))

print(r2_score(y, mymodel(x)))

Note: The result: 0.00995 indicates a very bad relationship, and tells us that this data set is not suitable for polynomial regression.

Want to go beyond the notes?

Join CodingNow 2.0's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Polynomial Regression – FAQs

Quick answers about learning Polynomial Regression in Python.

This free note from CodingNow 2.0 explains Polynomial Regression in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on CodingNow 2.0, including Polynomial Regression, is 100% free with no signup required.
With focused practice, most students grasp Polynomial Regression in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now