How to predict short term fluctuations using Machine Learning(ML)?
Fitting higher degree curves, quadratic, cubic and more.
What you will learn in this post?
Learn to fit given input data into polynomials of different degrees. Linear, quadratic, and more.
Detect the coefficient of correlation. This is a measure of the accuracy of our fitted curve.
Find the degree that fits best.
Generate data of a sine function and then try to fit it into a 0 degree, 1 degree and 2 degree curve.
Plot the generated curves
Beginning from our previous post where we fitted a straight line(linear regreession)
We will use numpy for this task. Numpy documentation is available at
We will use the Polynomial.fit method whose documentation is at
We will be using the polyfit method whose documentation is at
The evaluation of the polynomials will be done using polyval whose documentation is at
We will determine the coefficient of correlation using r2_score from sklearn.metrics.
Lastly the plot and scatter functions in Matplotlib.pyplot
We will start programming as usual by doing the required imports
from numpy.polynomial import polynomial as poly
import matplotlib.pyplot as plt
from sklearn.metrics import r2_score
import numpy as np
import random
import math
The next section is the degree input section where we input the degree.
deg=0 means a constant, 1 means a straight line, 2 is a quadratic curve and so on.
deg=0
The next section defines the inputs. We have 2 inputs the X values and the corresponding Y values.
We will examine a linear input, quadratic input and also the trigonometric function sine.
Here is the relevant section. Please uncomment only one section and change the degree to view different outputs.
#Linear Input
actualx =[1,2,3,4]#actualy=[3*x+1 for x in actualx]
#Linear Inputactualy=[4, 7, 10, 13]
equation=poly.Polynomial.fit(actualx,actualy,deg=deg).convert()
# Equation based on the input dataprint("Equation=",equation)
#Print the generated equationprint(actualx)print(actualy)predictionx=np.linspace(1,10,10
)# values from 1 to 10. Predicted values input
#Quadratic Input
actualx =[1,2,3,4]
# actualy=[2*x*x-3*x+1 for x in actualx]
#Quadratic Input
actualy=[0, 3, 10, 21]equation=poly.Polynomial.fit(actualx,actualy,deg=deg).convert()
# Equation based on the input dataprint("Equation=",equation)
#Print the generated equation
print(actualx)print(actualy)predictionx=np.linspace(1,10,10)
# values from 1 to 10. Predicted values input
#Sin Input from 0 to pi/2
pi=math.pi
actualx=[0,pi/6,pi/4,pi/3,pi/2]
#actualy=[np.sin(x) for x in actualx]
[0.0, 0.5, 0.7071067811865475, 0.8660254037844386, 1.0]
equation=poly.Polynomial.fit(actualx,actualy,deg=deg).convert()
# Equation based on the input data
print("Equation=",equation)
#Print the generated equation
print(actualx)
print(actualy)
predictionx=np.linspace(-2*pi,2*pi,60)# values from 0 to 2pi.
# Predicted values input
#Sin Input from 0 to pi
pi=math.piactualx=[0,pi/6,pi/4,pi/3,pi/2,2*pi/3,3*pi/4,5*pi/6,pi]
actualy=[np.sin(x) for x in actualx]
actualy=[0.0, 0.49999999999999994, 0.7071067811865475, 0.8660254037844386, 1.0, 0.8660254037844387, 0.7071067811865476, 0.49999999999999994, 1.2246467991473532e-16]
equation=poly.Polynomial.fit(actualx,actualy,deg=deg).convert()
# Equation based on the input data
print("Equation=",equation)
#Print the generated equation
print(actualx)print(actualy)
predictionx=np.linspace(-2*pi,2*pi,60)# values from 1 to 10.
#Predicted values inputprint(predictionx)
Last section
#Sin Input from 0 to 2pi
pi=math.pi
actualx=[0,pi/6,pi/4,pi/3,pi/2,2*pi/3,3*pi/4,5*pi/6,pi, 7*pi/6,5*pi/4, 4*pi/3,3*pi/2, 3*pi/2 + pi/6,3*pi/2 + pi/4, 3*pi/2+pi/3,2*pi]
actualy=[np.sin(x) for x in actualx]
#actualy=[0.0, 0.49999999999999994, 0.7071067811865475, 0.8660254037844386, 1.0, 0.8660254037844387, 0.7071067811865476, 0.49999999999999994, 1.2246467991473532e-16]
equation=poly.Polynomial.fit(actualx,actualy,deg=deg).convert()
#Equation based on the input data
print("Equation=",equation)
#Print the generated equation
print(actualx)
print(actualy)
predictionx=np.linspace(-2*pi,2*pi,60)
# values from 0 to 2pi.
# Predicted values input
print(predictionx).
We will be experiment with this data for a few posts now. To start with I will choose the sin data from 0 to 2pi and fit curves from 0 to 3.
Here is the relevant section and the results.
With deg=0, we fit a constant
Equation= -1.861014203614313e-18 is basically 0.
With degree =1
Equation= 0.8161476533828682 - 0.2597878666574686·x¹
With degree=3
Equation= -0.10732038400916702 + 1.7550539670323964·x¹ - 0.8216658002336001·x² +
The Correlation 98.41395417660479 %
The code for this example is at.
Please message if you have any difficulty in understanding the code. Please give your comments.