Polynomial Regression¶
Importing the libraries¶
In [12]:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
Importing the dataset¶
In [13]:
df = pd.read_csv('Position_Salaries.csv')
df.head(10)
Out[13]:
| Position | Level | Salary | |
|---|---|---|---|
| 0 | Business Analyst | 1 | 45000 |
| 1 | Junior Consultant | 2 | 50000 |
| 2 | Senior Consultant | 3 | 60000 |
| 3 | Manager | 4 | 80000 |
| 4 | Country Manager | 5 | 110000 |
| 5 | Region Manager | 6 | 150000 |
| 6 | Partner | 7 | 200000 |
| 7 | Senior Partner | 8 | 300000 |
| 8 | C-level | 9 | 500000 |
| 9 | CEO | 10 | 1000000 |
In [14]:
X = df.iloc[:, 1:-1].values
y = df.iloc[:, -1].values
Splitting the dataset into the Training set and Test set
In [15]:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
Training the Polynomial Regression model on the whole dataset¶
Generating the polynormial features
In [16]:
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 4)
X_poly = poly_reg.fit_transform(X)
In [22]:
from sklearn.linear_model import LinearRegression
# Create and fit the polynomial regressor
regressor = LinearRegression()
regressor.fit(X_poly, y)
Out[22]:
LinearRegression()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Parameters
| fit_intercept | True | |
| copy_X | True | |
| tol | 1e-06 | |
| n_jobs | None | |
| positive | False |
Visualising the Polynomial Regression results (for higher resolution and smoother curve)¶
In [23]:
X_grid = np.arange(min(X.flatten()), max(X.flatten()), 0.1)
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(X, y, color = 'red')
plt.plot(X_grid, regressor.predict(poly_reg.fit_transform(X_grid)), color = 'blue')
plt.title('Truth or Bluff (Polynomial Regression)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()
Predicting a new result with Polynomial Regression¶
Qsn: Predict the salary of a person in level 7.5
In [26]:
regressor.predict(poly_reg.fit_transform([[6.5]]))
Out[26]:
array([158862.45265155])