【问题标题】:Simple model error on fit: Found input variables with inconsistent numbers of samples拟合时的简单模型错误:发现样本数量不一致的输入变量
【发布时间】:2020-07-10 15:03:03
【问题描述】:

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

这是我的笔记本:

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

输出:(145063, 182)

y_sliced.shape

输出:(145063, 182)

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

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

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

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

【问题讨论】:

    标签: pandas machine-learning data-science


    【解决方案1】:

    您以错误的顺序分配了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
    

    【讨论】:

      猜你喜欢
      • 2017-12-11
      • 2021-01-04
      • 2019-12-31
      • 2020-07-24
      • 2020-02-24
      • 2021-06-20
      • 2018-06-25
      • 1970-01-01
      相关资源
      最近更新 更多