【问题标题】:ValueError : when array length doesn't match index length. How to debug this?ValueError :当数组长度与索引长度不匹配时。如何调试这个?
【发布时间】:2020-10-03 08:32:49
【问题描述】:

所以我开始使用 Kaggle 并且我正在执行预测谁在泰坦尼克号坠毁事件中幸存和谁没有幸存的指导任务。

我按照要求做了所有事情。

所以我的最后一个代码单元看起来像这样

from sklearn.ensemble import RandomForestClassifier

y = train_data['Survived']
features = ["Pclass","Sex","SibSp","Parch"]
X = pd.get_dummies(train_data[features])
X_test = pd.get_dummies(train_data[features])
model = RandomForestClassifier(n_estimators=1,max_depth=5,random_state=1)
model.fit(X,y)
predictions = model.predict(X_test)



output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
output.to_csv('my_submission.csv', index=False)
print("Your submission was successfully saved!")

编译后出现如下错误:

ValueError                                Traceback (most recent call last)
<ipython-input-24-7d2fc2ea2973> in <module>
     11 
     12 
---> 13 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
     14 output.to_csv('my_submission.csv', index=False)
     15 print("Your submission was successfully saved!")

/opt/conda/lib/python3.7/site-packages/pandas/core/frame.py in __init__(self, data, index, columns, dtype, copy)
    433             )
    434         elif isinstance(data, dict):
--> 435             mgr = init_dict(data, index, columns, dtype=dtype)
    436         elif isinstance(data, ma.MaskedArray):
    437             import numpy.ma.mrecords as mrecords

/opt/conda/lib/python3.7/site-packages/pandas/core/internals/construction.py in init_dict(data, index, columns, dtype)
    252             arr if not is_datetime64tz_dtype(arr) else arr.copy() for arr in arrays
    253         ]
--> 254     return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
    255 
    256 

/opt/conda/lib/python3.7/site-packages/pandas/core/internals/construction.py in arrays_to_mgr(arrays, arr_names, index, columns, dtype)
     62     # figure out the index, if necessary
     63     if index is None:
---> 64         index = extract_index(arrays)
     65     else:
     66         index = ensure_index(index)

/opt/conda/lib/python3.7/site-packages/pandas/core/internals/construction.py in extract_index(data)
    376                         f"length {len(index)}"
    377                     )
--> 378                     raise ValueError(msg)
    379             else:
    380                 index = ibase.default_index(lengths[0])

ValueError: array length 891 does not match index length 418

但是,我无法调试我的错误到底是什么,有人可以帮忙吗?谢谢。

【问题讨论】:

    标签: python pandas machine-learning data-science kaggle


    【解决方案1】:

    通过将 x_train 替换为 x_test 来纠正 x_test 分配。

    【讨论】:

      【解决方案2】:

      在考虑 train_data 而不是 test_data 时,您构建 X_test 数据框的方式不正确。在创建输出文件时,这会导致 test_data.PassengerIdpredictions 的大小不匹配。

      更正以下行,它将起作用:

      X_test = pd.get_dummies(test_data[features])
      

      【讨论】:

        猜你喜欢
        • 2016-08-31
        • 1970-01-01
        • 2018-02-22
        • 2021-06-30
        • 1970-01-01
        • 1970-01-01
        • 2021-10-23
        • 2018-12-19
        • 2022-01-13
        相关资源
        最近更新 更多