LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.
Python Data Science Machine Learning Lesson 8: Polynomial Regression
Polynomial Regression method is used when our data points won't fit in a Linear Regression i.e. in a straight line passing through all data points.Just like the linear regression, polynomial regression uses the relationship between the variables x and y in order to find the best way to plot a line through all the data points.
For doing this, you have to first import numpy as under
import numpy as np
Now, the python code for plotting Polynomial Regression will be as under.
code
import numpy as np
import matplotlib.pyplot as plt
time = [1,2,3,5,6,7,8,9,10,12,13,
14,15,16,18,19,21,22]
speed = [90,75,85,70,65,50,65,60,
75,72,77,74,80,78,92,97,94,99]
mymodel = np.poly1d(np.polyfit(time, speed, 3))
myline = np.linspace(1, 22, 100)
plt.scatter(time, speed)
plt.plot(myline, mymodel(myline))
plt.show()
The output will be
>
How to predict the speed of the train at a specific time?
In order to predict the speed at a particular time, you can use the code in the following manner:
import numpy as np
import matplotlib.pyplot as plt
time = [1,2,3,5,6,7,8,9,10,12,13,14,15,16,
18,19,21,22]
speed = [90,75,85,70,65,50,65,60,75,
72,77,74,80,78,92,97,94,99]
mymodel = np.poly1d(np.polyfit(time, speed, 3))
myline = np.linspace(1, 22, 100)
s = mymodel(4)
print(s)
The output will be
70.34475730296373
How to calculate R-squared value?
R-squared value is calculated to determine whether a model is a good fit or a bad fit.This value always lies between the range of 0 to 1.
The closer it is to 1, the model is considered to be a good fit.
The closer it is to 0, the model is considered to be a bad fit.
You have a module in python called sklearn, which can be used for calculating R-squared value.
You can use the following code to find the R-squared value.
code
import numpy as np
from sklearn.metrics import r2_score
time = [1,2,3,5,6,7,8,9,10,12,13,14,15,16,
18,19,21,22]
speed = [90,75,85,70,65,50,65,60,75,
72,77,74,80,78,92,97,94,99]
mymodel = np.poly1d(np.polyfit(time, speed, 3))
print(r2_score(speed, mymodel(time)))
The output will be
0.8274754
Conclusion:
The model is best fit for this data because the value of R-squared is very close to 1.On the other hand, if it was found that the value of R-squared is closer to 0, it would have been a bad fit.
Note: if you have python installed on your pc you can install matplotlib as under.
Open Command Prompt from the start menu.
Inside the command prompt, type
pip install matplotlib
press enter
This command will install matplotlib on your computer after which you can run on python.
Note: if you have python installed on your pc you can install numpy as under.
Open Command Prompt from the start menu.
Inside the command prompt, type
pip install numpy
press enter
This command will install numpy on your computer after which you can run on python.
Note: if you have python installed on your pc you can install sklearn as under.
Open Command Prompt from the start menu.
Inside the command prompt, type
pip install sklearn
press enter
This command will install sklearn on your computer after which you can run on python.