ValueError:当数组长度与索引长度不匹配时。如何调试呢?

问题描述 投票:0回答:1

所以我从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个回答
0
投票

您构建X_test数据框的方式不正确,一旦您考虑使用train_data而不是test_data。创建输出文件时,这会在test_data.PassengerIdpredictions的大小之间造成不匹配。

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

X_test = pd.get_dummies(test_data[features])
© www.soinside.com 2019 - 2024. All rights reserved.