拟合时的简单模型错误:发现样本数不一致的输入变量



我知道这个问题以各种形式存在,但在网上搜索了几天/几个小时后,我仍然没有找到任何东西,这解决了我的问题。

这是我的笔记本:

import numpy as np
import pandas as pd
X = pd.read_csv('../input/web-traffic-time-series-forecasting/train_1.csv.zip')
X = X.drop('Page', axis=1)
X.fillna(0, inplace=True, axis=0)
X_sliced = X.iloc[:, 0:367]
y_sliced = X.iloc[:, 367:-1]
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
linreg = LinearRegression()
X_sliced.drop(X_sliced.iloc[:, 182:367], inplace=True, axis=1) #Here, I make sure that my X_sliced has the same shape as y_sliced
X_sliced.shape

输出:(145063182(

y_sliced.shape

输出:(145063182(

X_train, y_train, X_test, y_test = train_test_split(X_sliced, y_sliced)
linreg.fit(X_train, y_train)

ValueError:发现样本数量不一致的输入变量:[108779736266]

当我的数据帧的形状完全相同时,为什么我会收到这个错误?

kaggle上原始作业的链接:https://www.kaggle.com/c/web-traffic-time-series-forecasting/overview

您以错误的顺序分配了train_test_split的输出,它应该是:

X_train, X_test, y_train, y_test = train_test_split(X_sliced, y_sliced) # x, x, y, y not x, y, x, y

最新更新