06.08.2021
Part 2
Regression
(2) Polynomial Linear Regression
Q:为什么Polynomial function,但依然可以被称为linear regression呢?(A polynomial function is a function such as a quadratic, a cubic, a quartic, and so on, involving only non-negative integer powers of x.)
A:Because when we are talking about linear or non-linear, we are acutally not talking about the x variables. When you talk about about the class of Regression, wether it's linear or non-linear, you are talking about the coefficients. Polynomial Linear Regression is one of the special case of Multiple Linear Regression.
Training the Polynomial Regression model on the whole dataset
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 4) # y = b0 + b1x1 + b2x1^2 + ... + ...^4
X_poly = poly_reg.fit_transform(X)
lin_reg_2 = LinearRegression()
lin_reg_2.fit(X_poly, y)
Visualising the Polynomial Regression results
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 Linear Regression)"
plt.xlable = "Position level"
plt.ylable = "Salary"
plt.show()
Visualising the Polynomial Regression results (for higher resolution and smoother curve)
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()
Predicting a new result with Polynomial Regression
print(lin_reg_2.predict(poly_reg.fit_transform([[6.5]])))