我正在参加 Kaggle 竞赛,但我不断收到 ValueError:数组长度 2643 与索引长度 3281 不匹配

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

这是我的代码,有点混乱,因为我疯狂地试图解决这个问题。

# AnyNan values in the target column or in my dataset
training_data.dropna(inplace=True, axis=0)
testing_data.dropna(inplace=True, axis=0)

# Perform one hot encoding on HomePlanet, 

features = ['HomePlanet', 'Destination', 'CryoSleep', 'VIP' ]
X= pd.get_dummies(training_data[features]).astype(int)
y = pd.get_dummies(training_data.Transported).astype(int)
x_test = testing_data[features]

# Creating my model

X_train, X_test, y_train, y_test = train_test_split(X,y, train_size=0.6, test_size=0.4, random_state=42)
rt_model = RandomForestRegressor()
rt_model.fit(X_train,y_train)
predictions = rt_model.predict(X_test)

#save the csv

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

当我在训练测试分割后打印 X 、 y 和 X_train、y_train 的长度时,我得到:

6606 6606 3963 3963 2643 2643

我尝试重塑 X 和 y。 我尝试在我的 x_test 数据帧上执行一种热编码。 我在我的数组上执行了 iloc 方法。

问题仅来自于试图将其另存为 csv 的最后一部分。

比赛是太空泰坦尼克号kaggle 如果这篇文章太长,我很抱歉,我是一个新手,想提供尽可能多的信息。

python machine-learning debugging artificial-intelligence kaggle
1个回答
0
投票

从前两行来看,我假设您已经提供了测试数据。无需将训练数据拆分为额外的测试数据。

您的预测应该基于原始测试数据

x_test
而不是分割的
X_test
运行。请注意,Python 区分大小写,这样命名变量是有风险且令人困惑的。

当您使用

X_test
时,您会得到一个与
testing_data
长度不同的数组,因此当您从中创建 DataFrame 并尝试保存它时,会出现不匹配。

所以使用

predictions = rt_model.predict(x_test)

应该可以工作,但我会进一步更改代码并消除额外的数据分割。

就是我们

© www.soinside.com 2019 - 2024. All rights reserved.