Polynomial Regression
Learn all about Polynomial Regression in this comprehensive tutorial.
- •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.
- •Python has methods for finding a relationship between data-points and to draw a line of polynomial regression.
- •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.
- •Now we can use the information we have gathered to predict future values.
- •Let us create an example where polynomial regression would not be the best method to predict future values.
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.

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:
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.
Create the arrays that represent the values of the x and y axis:
NumPy has a method that lets us make a polynomial model:
Then specify how the line will display, we start at position 1, and end at position 22:
Draw the original scatter plot:
Draw the line of polynomial regression:
Display the diagram:
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:
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:
The example predicted a speed to be 88.87, which we also could read from the diagram:

Bad Fit?
Let us create an example where polynomial regression would not be the best method to predict future values.
And the r-squared value?
Module quiz
2 questionsWhich of the following is true about Polynomial Regression?
What is the most common pitfall when working with Polynomial Regression?
Answer all questions to submit.