我的k-fold交叉验证技术在删除行的数据框上给出错误



祝你一切安好。我一直在使用数据框,我不得不删除包含任何空值的行。我使用以下命令删除这些行。我使用了以下命令:

df.dropna(axis=0,how="any",inplace=True)

然后当我像这样应用k-fold交叉验证时:

#Using kfold cross validation
from sklearn.model_selection import KFold, cross_val_predict
kf = KFold(shuffle=True, random_state=42, n_splits=5)
for train_index, test_index in kf.split(X):
X_train, X_test, y_train, y_test = (X.iloc[train_index, :], 
X.iloc[test_index, :], 
y[train_index], 
y[test_index])

出现以下错误:

KeyError: "Passing list-likes to .loc or [] with any missing labels is no longer supported. The following labels were missing: Int64Index([    0,   149,   151,   156,   157,n            ...n            26474, 26987, 27075, 27157, 27345],n           dtype='int64', length=1764). See https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike"

我不知道如何解决这个问题。它可能会给我一个错误,因为这些行不存在,我可能需要重新索引它们,从0开始,有合适的索引。我不知道该怎么做。谁有什么好的建议?由于

我认为你想要的是:

for train_index, test_index in kf.split(X):

X_train, X_test, y_train, y_test = (X.iloc[train_index], 
X.iloc[test_index], 
y.iloc[train_index], 
y.iloc[test_index])

我认为你的问题来自于你使用kf.split(X)生成的相对索引数作为y[train_index]y[test_index]的索引值。如果在XyDF的索引中的索引,您的原始可能-偶然-工作。

最新更新