In [24]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
In [25]:
np.set_printoptions(precision=3, suppress=True)
In [26]:
dataset = pd.read_csv('Position_Salaries.csv')
In [27]:
X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, -1].values
In [28]:
from sklearn.linear_model import LinearRegression
In [29]:
lin_reg = LinearRegression()
lin_reg.fit(X, y)
Out[29]:
In [30]:
from sklearn.preprocessing import PolynomialFeatures
In [31]:
poly_reg = PolynomialFeatures(degree=4)
X_poly = poly_reg.fit_transform(X)
poly_reg.fit(X_poly, y)
Out[31]:
In [32]:
lin_reg_2 = LinearRegression()
lin_reg_2.fit(X_poly, y)
Out[32]:
In [33]:
plt.scatter(X, y, color='red')
plt.plot(X, lin_reg.predict(X), color='blue')
plt.title('Truth or Bluff (Linear Regression)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()
In [34]:
plt.scatter(X, y, color='red')
plt.plot(X, lin_reg_2.predict(poly_reg.fit_transform(X)), color='blue')
plt.title('Truth or Bluff (Polynomial Regression)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()
In [35]:
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='blue')
plt.title('Truth or Bluff (Polynomial Regression)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()
In [36]:
lin_reg.predict(6.5)
Out[36]:
In [37]:
lin_reg_2.predict(poly_reg.fit_transform(6.5))
Out[37]: