【发布时间】:2019-03-02 04:31:58
【问题描述】:
我的问题是我正在对我的数据应用一个简单的线性回归:当我将数据拆分为训练和测试数据时,当 p 值和 r 平方和调整后的 r 平方结果时,我没有找到重要的模型在训练数据中是很好的结果。 这是更多解释的代码:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
from scipy import stats
data = pd.read_excel ("C:\\Users\\AchourAh\\Desktop\\PL14_IPC_03_09_2018_SP_Level.xlsx",'Sheet1') #Import Excel file
data1 = data.fillna(0) #Replace null values of the whole dataset with 0
print(data1)
X = data1.iloc[0:len(data1),5].values.reshape(-1, 1) #Extract the column of the COPCOR SP we are going to check its impact
Y = data1.iloc[0:len(data1),6].values.reshape(-1, 1) #Extract the column of the PAUS SP
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size =0.3, random_state = 0)
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, Y_train)
plt.scatter(X_train, Y_train, color = 'red')
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.title('SP00114585')
plt.xlabel('COP COR Quantity')
plt.ylabel('PAUS Quantity')
plt.show()
plt.scatter(X_test, Y_test, color = 'red')
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.title('SP00114585')
plt.xlabel('COP COR Quantity')
plt.ylabel('PAUS Quantity')
plt.show()
X2 = sm.add_constant(X_train)
est = sm.OLS(Y_train, X2)
est2 = est.fit()
print(est2.summary())
X3 = sm.add_constant(X_test)
est3 = sm.OLS(Y_test, X3)
est4 = est3.fit()
print(est4.summary())
最后,当尝试显示统计结果时,我总是在训练数据中找到好的结果,但在测试数据中却没有。我的代码可能有问题。 注意我是python的初学者
【问题讨论】:
-
举一个最小的例子,可以重现你的问题,让其他人轻松快速地帮助你
-
可能是因为你适应的测试集比训练集小很多...
-
但数据由 26 行组成,其中 19 行用于训练数据,其余用于测试数据
-
所以只有 7 行适合
est3?显然,结果将比为est拟合到 19 行时差得多。想象一下每个只适合 1 行(极端情况)。有什么奇怪的adj。 r 平方值等很糟糕? -
是的,所有的值都非常糟糕,调整后的 rsquare 的 p 值。我该怎么办 ?停止火车测试,或者将 50 个用于火车和 50 个用于测试
标签: python scikit-learn linear-regression statsmodels