Introduction Polynomial Regression
The polynomial regression technique could find the relationship between input features and the output variable in a better way even if the relationship is not linear. It uses the same formula as the linear regression:
Formula for linear regression is:-
Here is X is the independent variable and h is the dependant variable and the remaining is the model coefficient.
The formula of polynomial regression is also considered to be a linear model as the coefficients of the model are associated with the features that are still linear. x² in into the polynomial regression is used to curve fitted into the quadratic in nature.
- It provides the best approximation of the relationship between the dependent variable and independent variables.
- A broad range of functions can be fit under it.
- It fits a wide range of curvature.
- The presence of one or two outliers in the data can seriously affect the results of the nonlinear analysis.
- These are too sensitive to the outliers.
- in the model validation tools for the detection of outliers in nonlinear regression than there are for linear regression.
The below graph gives you a better understanding of how to fit the polynomial line when your passing different degree.
Implementation using Sklearn library of polynomial regression
# Import required library import operator import numpy as np import matplotlib.pyplot as plt # Import library to apply liner and polynomial regression from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error, r2_score #To ckeck the accuracy from sklearn.preprocessing import PolynomialFeatures # Generate Random Data from numpy library # Create independant and Dependant variable data np.random.seed(0) x = 2 - 3 * np.random.normal(0, 1, 20) y = x - 2 * (x ** 2) + 0.5 * (x ** 3) + np.random.normal(-3, 3, 20) # transforming the data to include axis x = x[:, np.newaxis] y = y[:, np.newaxis] # Apply polynomial regression with degree Three polynomial_features= PolynomialFeatures(degree=3) x_poly = polynomial_features.fit_transform(x) # Apply liner Model model = LinearRegression() model.fit(x_poly, y) y_poly_pred = model.predict(x_poly) # Check the accuracy rmse = np.sqrt(mean_squared_error(y,y_poly_pred)) r2 = r2_score(y,y_poly_pred) print('Root mean square error :-',rmse) print('R Square:-',r2) print('\n ----------------------plot Scatter plot with poly line-----------------------') plt.scatter(x, y, s=10) # sort the values of x before line plot sort_axis = operator.itemgetter(0) sorted_zip = sorted(zip(x,y_poly_pred), key=sort_axis) # Drow Poly line x, y_poly_pred = zip(*sorted_zip) plt.plot(x, y_poly_pred, color='m') plt.show() Output:- Root mean square error :- 3.449895507408724 R Square:- 0.9830071790386679
———————-plot Scatter plot with polyline———————–