Scikit-Learn 交叉验证功能在索引不连续时不允许自定义折叠

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

尝试将自定义交叉验证折叠传递给 sklearn 的交叉验证函数。

交叉验证功能似乎会触发错误,因为它坚持使用基于位置的索引,而不是基于标签的索引。我在 cv_folds 参数中传递的索引与原始数据框的索引一致。这是相关的原因是因为我想使用哈希函数值来为我的训练测试拆分以及我的 cv 折叠选择子集。我收到以下错误:IndexError: indices are out-of-bounds

df2 = pd.DataFrame(np.random.rand(8, 3), columns=['feature_1', 'feature_2', 'feature_3'])
train_index_list = [0,1,2,5,6,7]
test_index_list = [3,4]
X_train = df2.loc[train_index_list].drop(columns='feature_3').copy()
y_train = df2.loc[train_index_list]['feature_3'].copy()
# 2-fold cross validation
cv_folds = [ ([0,1,2,],[5,6,7]), ([5,6,7], [0,1,2])]
cv_output = cross_validate(model, X_train, y_train,  scoring=['neg_mean_squared_error'], cv=cv_folds) 

这会触发一个错误。但令我困惑的是,以下几行运行得很好

X_train.loc[train_index_list]
y_train.loc[train_index_list]

如何解决这个问题,以便我可以将自定义的 cv 折叠传递到 Scikit-Learn 中?

pandas dataframe scikit-learn cross-validation
© www.soinside.com 2019 - 2024. All rights reserved.