线性和非线性回归问题

问题描述 投票:-1回答:1

我正在尝试使用散点图进行多项式回归,我有两个问题:

  1. 红线,即多项式回归,当与通过数据值绘制的图进行比较时,对我来说似乎是错误的。>

  2. 我如何计算每个回归的r平方

  3. 使用的X和Y数据的一部分(我从excel文件中获取了此数据):

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

#read the data
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

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9xUU5ybS5wbmcifQ==” alt =“在此处输入图像描述”>

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9CM3hUTi5wbmcifQ==” alt =“在此处输入图像描述”>

[我正在尝试使用散点图进行多项式回归,我有两个问题:红线,即与...相比,多项式回归对我来说是错误的...]]

python scikit-learn non-linear-regression
1个回答
0
投票
您发布的第一张图实际上是关于我的期望。大多数点几乎是水平的,最右边的一些点向上延伸。您应用了一条接近最佳拟合的扁平线,试图将误差最小化(这是您的预测值与实际值之间的距离)。这有意义吗?

应该注意,为了对指数数据进行线性回归,您需要对指数数据应用对数,这将把它变成线性数据集。这有意义吗?

© www.soinside.com 2019 - 2024. All rights reserved.