AdSense

Tuesday, May 26, 2020

Polynomial Regression (Fundamentals) in Python

Polynomial Regression (Fundamentals) in Python



0_MacOS_Python_setup.txt
# Install on Terminal of MacOS

#1. matplotlib
#pip3 install -U matplotlib

#2. NumPy
#pip3 install -U numpy

#3. scikit-learn (sklearn)
#pip3 install -U scikit-learn




1_MacOS_Terminal.txt
########## Run Terminal on MacOS and execute
### TO UPDATE
cd "YOUR_WORKING_DIRECTORY"

python3 prf01.py





Python files

prf01.py
########## Machine Learning - Polynomial Regression ##########
#
#
# Reference:
#
# Machine Learning - Polynomial Regression
# https://www.w3schools.com/python/python_ml_polynomial_regression.asp


#####Polynomial Regression


#####How Does it Work?

# x: the hours of the day
# y: represents the speed

### a scatter plot:

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.savefig("Figure_1_Scatter_Plot.png")    # added to save a figure
plt.show()



### a scatter plot with the line of polynominal 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))

# line from x = 1 to x = 22
myline = numpy.linspace(1, 22, 100)

plt.scatter(x, y)
plt.plot(myline, mymodel(myline))
plt.savefig("Figure_2_Scatter_Plot_with_the_line_of_polynominal_regression.png")    # added to save a figure
plt.show()


##### R-Squared

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)))
#0.9432150416451025



#####Predict Future Values

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

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)    #x = 17
print(speed)
#88.87331269697991 (when x = 17)



#####Bad Fit?

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.savefig("Figure_3_Bad_Fit.png")    # added to save a figure
plt.show()


#r-squared value
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)))
#0.009952707566680652




#####Predict Future Values (Training Data to build a model, Forecasted Data by the model, and Test Data which is not used to build the model)

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

import numpy
from sklearn.metrics import r2_score


### Training Data
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))

# line from x = 1 to x = 22
myline = numpy.linspace(1, 22, 100)

#plt.scatter(x, y)
plt.scatter(x, y, label='Training Data')
#plt.plot(myline, mymodel(myline))
plt.plot(myline, mymodel(myline), label='Polynominal Regession')

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


### Forecasted Data
x_not_for_training = 17
y_forecasted = mymodel(x_not_for_training)    #x_not_for_training = 17
print(y_forecasted)
#88.87331269697991 (when x_not_for_training = 17)
#
plt.scatter(x_not_for_training, y_forecasted, label='Forecasted Data')


### Test Data
x_test = [1,2,3,5,6,7,8,9,10,12,13,14,15,16,18,19,21,22]
y_test = [95,95,85,55,65,50,55,60,70,75,70,72,80,81,88,97,101,100]
plt.scatter(x_test, y_test, label='Test Data')


### Figure: adding a legend, save it, and then show it.
plt.legend()
plt.savefig("Figure_4_Scatter_Plot_with_the_line_of_polynominal_regression_plus_test_data.png")    # added to save a figure
plt.show()






Figures
Figure_1_Scatter_Plot.png

Figure_2_Scatter_Plot_with_the_line_of_polynominal_regression.png 

Figure_3_Bad_Fit.png

Figure_4_Scatter_Plot_with_the_line_of_polynominal_regression_plus_test_data.png



Reference

No comments:

Post a Comment

Deep Learning (Regression, Multiple Features/Explanatory Variables, Supervised Learning): Impelementation and Showing Biases and Weights

Deep Learning (Regression, Multiple Features/Explanatory Variables, Supervised Learning): Impelementation and Showing Biases and Weights ...