Machine Learning - Polynomial Regression

Polynomial Regression

If your data points are clearly not equal to linear regression (a straight line across all data points), they may be eligible for polynomial retrieval.

Polynomial retrieval, such as line decompression, uses the interactions between x and y transforms to determine the best way to draw a line at data points.

How Does it Work?

Python has ways of finding relationships between data points and drawing a polynomial retrieval line. We will show you how to use these methods instead of using a mathematical formula.

In the example below, we have registered 18 vehicles as they pass a particular tollbooth.

We have registered the speed of the vehicle, and the time of day (hour) it occurred.

The x-axis represents the hours of the day and the axis of y 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()
Example

Import numpy and matplotlib then draw the line of Polynomial Regression:

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()