UNIT-1 Polynomial Regression
UNIT-1 Polynomial Regression
POLYNOMIAL REGRESSION
Polynomial Regression is a regression algorithm that models the relationship
between a dependent(y) and independent variable(x) as nth degree polynomial. The
Polynomial Regression equation is given below
When we compare the above three equations, we can clearly see that all three
equations are Polynomial equations but differ by the degree of variables. The
Simple and Multiple Linear equations are also Polynomial equations with a single
degree, and the Polynomial regression equation is Linear equation with the nth
degree. So if we add a degree to our linear equations, then it will be converted into
Polynomial Linear equations.
o Data Pre-processing
o Build a Linear Regression model and fit it to the dataset
o Build a Polynomial Regression model and fit it to the dataset
o Visualize the result for Linear Regression and Polynomial Regression
model.
o Predicting the output.
1. # importing libraries
2. import numpy as nm
3. import matplotlib.pyplot as mtp
4. import pandas as pd
5. #importing datasets
6. data_set= pd.read_csv('Position_Salaries.csv')
7. #Extracting Independent and dependent Variable
8. x= data_set.iloc[:, 1:2].values
9. y= data_set.iloc[:, 2].values
After executing the code, we will get another matrix x_poly, which can be seen
under the variable explorer option:
Next, we have used another LinearRegression object, namely lin_reg_2, to fit
our x_poly vector to the linear model.
Output:
As we can see in the above output image, the predictions are close to the real values. The
above plot will vary as we will change the degree.
For degree= 3:
If we change the degree=3, then we will give a more accurate plot, as shown in the below
image.
Degree= 4: Let's
again change the degree to 4, and now will get the most accurate
plot. Hence we can get more accurate results by increasing the degree of
Polynomial.
Predicting the final result with the Polynomial
Regression model:
Now, we will predict the final output using the Polynomial Regression model to
compare with Linear model. Below is the code for it:
1. poly_pred = lin_reg_2.predict(poly_regs.fit_transform([[6.5]]))
2. print(poly_pred)
Output:
[158862.45265153]