《Machine Learning A-Z》Part 2 (3) - Polynomial Linear Regression

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]])))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值