Polynomial Regression

What if the simple linear regression model can’t find any relationship between the target and the predictor variable?🤔

What is Polynomial Regression?

Complete Pipeline
A Polynomial Equation
#Import necessary libraties
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.preprocessing import PolynomialFeatures
#Change degree to reduce the error
polynomial_features= PolynomialFeatures(degree=2)
x_poly = polynomial_features.fit_transform(x_train)
model = LinearRegression()
model.fit(x_poly, y)y_poly_pred = model.predict(x_poly)
rmse = np.sqrt(mean_squared_error(y,y_poly_pred))
r2 = r2_score(y,y_poly_pred)
print(rmse)
print(r2)
#Plotting the polynomial 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)
x, y_poly_pred = zip(*sorted_zip)
plt.plot(x, y_poly_pred, color='m')
plt.show()
Visualizing the fit with different degree

The Bias vs Variance trade-off

Illness of model
Source: http://scott.fortmann-roe.com/docs/BiasVariance.html

CONCLUSION:

Learning and exploring this beautiful world with amazing tech.