线性和非线性回归问题

时间:2020-05-09 11:41:13

标签: python scikit-learn non-linear-regression

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

  1. 多项式回归的红线与数据值与图的比较对我来说似乎是错误的

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

使用的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

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

您发布的第一张图实际上是关于我的期望。大多数点几乎是水平的,最右边的一些点向上延伸。您将应用一条接近最佳拟合的平坦线,以尽量减小误差(这是您的预测值与实际值之间的距离)。这有道理吗?

应注意,为了对指数数据进行线性回归,您需要对指数数据应用对数,这会将其转换为线性数据集。这有道理吗?

您的第二个示例有些混乱,因为我对多项式特征函数不熟悉,但是我同意曲线看起来不太准确。