首页 > 解决方案 > 训练测试数据集回归结果

问题描述

我的问题是我正在对我的数据应用简单的线性回归:当我将数据拆分为训练和测试数据时,当 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的初学者

标签: pythonscikit-learnlinear-regressionstatsmodels

解决方案


尝试运行此模型几次,无需指定random_statetrain_test_split更改test_size参数。

IE

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size =0.2)

到目前为止,每次运行模型时,都会对数据进行相同的拆分,因此您可能会因为拆分而过度拟合模型。


推荐阅读