K与RandomFores的交叉验证

2021-05-16 08:51:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在尝试使用RandomForest来预测一些东西,同时也使用k-fold交叉验证来最小化我对minu-samples-leaf的交叉验证错误。我目前在设置代码时遇到了问题,因为当我到达train_x = x[train_index]时,我总是遇到错误。我得到的错误显示在下面。你知道吗

from sklearn import model_selection
kf = model_selection.KFold(n_splits=5)

x = train
y = test

for m in range(0, 10): # vary min_samples_leaf

    dtr = ensemble.RandomForestRegressor(n_estimators = 15, min_samples_leaf = m, max_features = 10, criterion = 'mse')

    for train_index, test_index in kf.split(x):
        print("TRAIN:", train_index, "TEST:", test_index)
        train_x = x[train_index]
        train_y = y[test_index]
        regr = dtr.fit(train_x, train_y)

密钥错误:

None of [Int64Index([15546, 15547, 15548, 15549, 15550, 15551, 15552, 15553, 15554,\n            15555,\n            ...\n            77718, 77719, 77720, 77721, 77722, 77723, 77724, 77725, 77726,\n            77727],\n           dtype='int64', length=62182)] are in the [columns]