8-1 什么是多项式回归
Notbook 示例
Notbook 源码
1 [1] 2 import numpy as np 3 import matplotlib.pyplot as plt 4 [2] 5 x = np.random.uniform(-3, 3, size=100) 6 X = x.reshape(-1,1) 7 [3] 8 y = 0.5 * x**2 + x + 2 + np.random.normal(0, 1, size=100) 9 [4] 10 plt.scatter(x,y) 11 <matplotlib.collections.PathCollection at 0x1c6b9056910> 12 13 [5] 14 from sklearn.linear_model import LinearRegression 15 16 lin_reg = LinearRegression() 17 lin_reg.fit(X,y) 18 LinearRegression() 19 [6] 20 y_predict = lin_reg.predict(X) 21 [7] 22 plt.scatter(X, y) 23 plt.plot(x, y_predict, color='r') 24 [<matplotlib.lines.Line2D at 0x1c6bba7a4c0>] 25 26 解决方案, 添加一个特征 27 [8] 28 (X**2).shape 29 (100, 1) 30 [9] 31 X2 = np.hstack([X,X**2]) 32 [10] 33 X2.shape 34 (100, 2) 35 [11] 36 lin_reg2 = LinearRegression() 37 lin_reg2.fit(X2,y) 38 y_predict2 = lin_reg2.predict(X2) 39 [12] 40 plt.scatter(X, y) 41 plt.plot(x, y_predict2, color='r') 42 [<matplotlib.lines.Line2D at 0x1c6bbaeaaf0>] 43 44 [13] 45 plt.scatter(X, y) 46 plt.plot(np.sort(x), y_predict2[np.argsort(x)], color='r') 47 [<matplotlib.lines.Line2D at 0x1c6bbb5d580>] 48 49 [14] 50 lin_reg2.coef_ 51 array([1.20691609, 0.45458127]) 52 [15] 53 lin_reg2.intercept_ 54 2.0128010763968693
标签:plt,泛化,多项式,模型,reg2,LinearRegression,np,100,lin From: https://www.cnblogs.com/Cai-Gbro/p/16837442.html