<jupyter_start><jupyter_text>Polynomial Linear Regression

$ y = b0 + b1x1^1 + b2x1^2 + b2x1^3 + ... bnx1^n $  Importing the libraries<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd<jupyter_output><empty_output><jupyter_text>Importing the dataset<jupyter_code>dataset = pd.read_csv('Position_Salaries.csv')
X = dataset.iloc[:, 1:-1].values
y = dataset.iloc[:, -1].values<jupyter_output><empty_output><jupyter_text>Training the Linear Regression model on complete dataset<jupyter_code>from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(X, y)<jupyter_output><empty_output><jupyter_text>Training the Polynomial Linear Regression model on complete dataset<jupyter_code>from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 4)
X_poly = poly_reg.fit_transform(X)
lin_reg_2 = LinearRegression()
lin_reg_2.fit(X_poly, y)<jupyter_output><empty_output><jupyter_text>Visualizing the Linear Regression Model Results<jupyter_code>plt.scatter(X, y, color = 'red')
plt.plot(X, lin_reg.predict(X), color = 'grey')
plt.title('Simple Linear Regression Model')
plt.xlabel('Position level')
plt.ylabel('Salary')<jupyter_output><empty_output><jupyter_text>Visualizing the Polynomial Linear Regression Model Results<jupyter_code>plt.scatter(X, y, color = 'red')
plt.plot(X, lin_reg_2.predict(poly_reg.fit_transform(X)), color = 'grey')
plt.title('Polynomial Linear Regression Model')
plt.xlabel('Position level')
plt.ylabel('Salary')
%%html
<marquee style='width: 50%; color: orange; font-size:150%;'><b>Fitting way better than Simple LR</b></marquee><jupyter_output><empty_output><jupyter_text>Improving the model 

By changing the hyperparameter degree that was set to 2 i.e

$ y = b0 + b1x1^1 + b2x1^2 $

Now by setting it to 4 we have:<jupyter_code>plt.scatter(X, y, color = 'red')
plt.plot(X, lin_reg_2.predict(poly_reg.fit_transform(X)), color = 'grey')
plt.title('Polynomial Linear Regression Model')
plt.xlabel('Position level')
plt.ylabel('Salary')
%%html
<marquee style='width: 50%; color: red; font-size:150%;'><b>Model is overfitting but thats ok 😎 but the curve is not smooth 😪</b></marquee><jupyter_output><empty_output><jupyter_text>Visualizing the Polynomial Regression results (For smoother curve)<jupyter_code>X_grid = np.arange(min(X), max(X), 0.1)
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(X, y, color = 'red')
plt.plot(X_grid, lin_reg_2.predict(poly_reg.fit_transform(X_grid)), color = 'grey')
plt.title('Polynomial Linear Regression Model')
plt.xlabel('Position level')
plt.ylabel('Salary')
%%html
<marquee style='width: 50%; color: red; font-size:150%;'><b>Better Now ✌</b></marquee><jupyter_output><empty_output><jupyter_text>Predicting a new result with Linear Regression Model<jupyter_code>lin_reg.predict([[6.5]])<jupyter_output><empty_output><jupyter_text>Predicting a new result with Polynomial Refgression Model<jupyter_code>lin_reg_2.predict(poly_reg.fit_transform([[6.5]]))
%%html
<marquee style='width: 50%; color: red; font-size:150%;'><b>WoW💥 Model predicted the salary nearest to the orignal salary👌</b></marquee><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13755458515283842, 'fraction_numerical': 0.017779164067373673, 'mean_word_length': 7.73841961852861, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '486652', 'license': 'Unlicense', 'n_tokens_mistral': 1241, 'n_tokens_neox': 1184, 'n_words': 243, 'path': '05. PolynomialRegression.ipynb', 'repo_name': 'Samrath49/AI_ML_DL', 'size': '3206'}