Polynomial Regression:
If the data doesn’t fit in to linear regression then, polynomial regression works. Similarly, it is
also an relationship between x and y to draw a line through the data points.
Example:
import numpy
import [Link] 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([Link](x, y, 3)) // polynomial model
myline = [Link](1, 22, 100)
[Link](x, y)
[Link](myline, mymodel(myline))
[Link]()
R-Squared:
Relationship between x axis and y axis.
0- No relation
1- 100% related
mymodel = numpy.poly1d([Link](x, y, 3))
print(r2_score(y, mymodel(x)))
To predict future values:
Speed= mymodel(17) //time- 17:00 future predict
Print(speed)
Bad fit:
A bad fit for polynomial regression
For a bad fit the r- squared value will be very low. Somewhat close to 0.
Multiple Regression:
Similar to linear regression but has more than one independent value, i.e., two or more
variables.
It can be done with the help of pandas library and also by using sklearn also using linear
regression.
Example:
import pandas
from sklearn import linear_model
df = pandas.read_csv("[Link]")
X = df[['Weight', 'Volume']]
y = df['CO2']
regr = linear_model.LinearRegression()
[Link](X, y) //output- LinearRegression()
predictedCO2 = [Link]([[2300, 1300]])
print(predictedCO2)
Coeeficient:
Factor that describes relationship with an unknown variable (ex- 2x here x- variable 2-
coefficient)
Print(regr.coef_)
It can be used to predict how the value changes when the variable increase or decreases.
Scale:
When data has different values it will be difficult to compare them then we can use scale to
compare them easily. Kg comparision to metres/ time and altitude. It is difficult right? That’s
why we use scale.
So one of the methods that can be used for scaling is standardization.
Formula-
Z=(x-u)/s
Z=new value
X=original value
u- mean value
s- standard deviation
For example:
If you take the weight column from the data set above, the first value is 790, and the scaled
value will be:
(790 - 1292.23) / 238.74 = -2.1
If you take the volume column from the data set above, the first value is 1.0, and the scaled
value will be:
(1.0 - 1.61) / 0.38 = -1.59
Now you can compare -2.1 with -1.59 instead of comparing 790 with 1.0.
So, instead of doing this we can use the function – StandardScaler()
Example:
import pandas
from sklearn import linear_model
from [Link] import StandardScaler
scale = StandardScaler()
df = pandas.read_csv("[Link]")
X = df[['Weight', 'Volume']]
scaledX = scale.fit_transform(X)
print(scaledX)
This scale can also be used to predict values.
import pandas
from sklearn import linear_model
from [Link] import StandardScaler
scale = StandardScaler()
df = pandas.read_csv("[Link]")
X = df[['Weight', 'Volume']]
y = df['CO2']
scaledX = scale.fit_transform(X)
regr = linear_model.LinearRegression()
[Link](scaledX, y)
scaled = [Link]([[2300, 1.3]])
predictedCO2 = [Link]([scaled[0]])
print(predictedCO2)
Training and Testing of data:
To measure if a model is good enough
Dataset is split into 2- train and test data
80%- training 20%- testing
Example:
Import numpy
import [Link] as plt
[Link](2)
x=[Link](3,1,100)
y=[Link](150,40,100)/x
[Link](x,y)
[Link]()
Splitting of dataset:
Train_x=x[:80]
Train_y= y[:80]
Test_x=x[80:]
Test_y=y[80:]
Display dataset:
[Link](train_x, train_y)
[Link]()
[Link](test_x, test_y)
[Link]()
mymodel = numpy.poly1d([Link](train_x, train_y, 4))
myline = [Link](0, 6, 100)
[Link](train_x, train_y)
[Link](myline, mymodel(myline))
[Link]()