这是我使用的数据集的链接:数据集import numpy as npimport matplotlib.pyplot as pltimport pandas as pd#Lets begin with polynomial regressiondf = pd.read_excel('enes.xlsx', index='hacim')X=pd.DataFrame(df['hacim'])Y=pd.DataFrame(df['delay'])from sklearn.linear_model import LinearRegressionfrom sklearn.preprocessing import PolynomialFeaturespoly_reg = PolynomialFeatures(degree = 4)X_poly = poly_reg.fit_transform(X)lin_reg_2 = LinearRegression()lin_reg_2.fit(X_poly, Y)plt.scatter(X, Y, color = 'red')plt.plot(X, lin_reg_2.predict(poly_reg.fit_transform(X)), color = 'blue')plt.title('X Vs Y')plt.xlabel('hacim')plt.ylabel('delay')plt.show()最后 plt.show 显示了一个图表,其中有很多行,而不是我想要的单行多项式回归。出了什么问题,我该如何解决这个问题?数据,hacim,delay0,815,1.441,750,1.112,321,2.373,1021,1.444,255,1.095,564,1.616,1455,15.277,525,2.78,1118,106.989,1036,3.4710,396,1.3411,1485,21.4912,1017,12.2213,1345,2.7214,312,1.7115,742,33.7916,1100,39.6217,1445,4.8818,847,1.5519,991,1.8220,1296,10.7721,854,1.8122,1198,61.923,1162,8.2224,1463,42.2525,1272,4.3126,745,2.3627,521,2.1428,1247,94.3329,732,12.5530,489,1.0531,1494,12.7832,591,3.1833,257,1.1834,602,4.2435,335,2.0636,523,3.6337,752,7.6138,349,1.7639,771,0.7940,855,39.0841,948,3.9542,1378,97.2843,598,2.6944,558,1.6745,634,34.6946,1146,12.2247,1087,1.7448,628,1.0349,711,3.3450,1116,7.2751,748,1.0952,1212,14.1653,434,1.4254,1046,8.2555,568,1.3356,894,2.6157,1041,4.7958,801,1.8459,1387,11.560,1171,161.2161,734,2.4362,1471,17.4263,461,1.4264,751,2.3665,898,2.466,593,1.7467,942,3.3968,825,1.0969,715,20.2370,725,5.4371,1128,7.5772,1348,4.4973,1393,9.7774,1379,97.7675,859,2.5976,612,15.9877,1495,8.2278,887,1.8579,867,38.6580,1353,1.681,851,60.2582,1079,24.0583,1100,25.5884,638,1.2385,1115,1.9486,1443,4.7987,1421,10.3388,1279,7.2989,1176,173.4490,315,1.5391,1019,34.0392,1337,48.6793,576,28.8394,919,2.8895,361,1.596,989,1.4797,1286,32.11
1 回答
Qyouu
TA贡献1786条经验 获得超11个赞
让我们使用 pandas 绘图,它更容易:
X=pd.DataFrame(df['hacim'])
Y=pd.DataFrame(df['delay'])
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 4)
X_poly = poly_reg.fit_transform(X)
lin_reg_2 = LinearRegression()
lin_reg_2.fit(X_poly, Y)
df['y_pred'] = lin_reg_2.predict(poly_reg.fit_transform(X))
df = df.sort_values('hacim')
ax = df.plot.scatter('hacim','delay')
df.plot('hacim', 'y_pred', ax=ax, color='r')
plt.title('X Vs Y')
plt.xlabel('hacim')
plt.ylabel('delay')
plt.show()
输出:
绘制折线图时,散点线的根是未排序的数据。
你可以这样做:
plt.plot(X, lin_reg_2.predict(poly_reg.fit_transform(X)), color = 'blue', marker='o', linestyle='none')
输出:
添加回答
举报
0/150
提交
取消