python图形错误中的线性和非线性回归

Y代表代表总值的特定区域的每一列。

``````x=[1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980...]

y=[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.164, 0.16499999999999998, 0.16999999999999998, 0.175, 0.17200000000000001, 0.185, 0.189, 0.195, 0.201...]

Renew = pd.read_excel('bp-stats-review-2019-all-data.xlsx', sheet_name = 'Renewables - TWh', headers = 2, skiprows=2, usecols = range(55)).dropna(axis=0,how='all').iloc[:-10]
Renew.fillna('0',inplace=True)

#Taking only the Totals
Countries_Renew = Renew[~Renew['Terawatt-hours'].str.startswith('Total')].sort_values(['Terawatt-hours'])
Countries_Renew.set_index('Terawatt-hours', inplace=True)

#build the Linear plot regression by region
df=Countries_Renew_Total.drop(['Total World']).transpose()
n=0

for j in df.columns:
print('The region is: '+j)
print(n)
for i in range(1,3):
#import the dataset
x=df.index.values.reshape(-1,1)
y=df.iloc[:,int(n)].values.reshape(-1,1)

#Fit the linear regression
lin=LinearRegression()
lin.fit(x,y)

#Fit the Poly regression
poly = PolynomialFeatures(degree = i)
x_poly = poly.fit_transform(x)
poly.fit(x_poly,y)
lin2=LinearRegression()
lin2.fit(x_poly,y)

#Plot Poly regression
plt.scatter(x,y,color='blue')
plt.plot(x,lin2.predict(poly.fit_transform(x)),color='red')
plt.title('Polynomial Regression degree '+str(i))
plt.xlabel('Year')
plt.ylabel('Renewable Generation (TWh)')
plt.show()
print(lin2.predict(poly.fit_transform([[2019]])))
print(lin2.predict(poly.fit_transform([[2020]])))
n=n+1
``````