【问题标题】:K-fold Cross Validation with RandomForest使用 RandomForest 进行 K 折交叉验证
【发布时间】:2020-02-06 07:00:17
【问题描述】:

我目前正在尝试使用 RandomForest 进行预测,同时还使用 k 折交叉验证来最小化我对 min_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]

【问题讨论】:

    标签: python machine-learning scikit-learn random-forest cross-validation


    【解决方案1】:

    你有一大堆从 kf.split() 中提取的值,你必须调用 x[train_index] 的 train_index 不只是在数组 x 中。

    代码看起来是对的,所以我怀疑“train”(当然还有“x”)中的数据格式有问题?

    错误表明您的 Int64Index 类型(索引 IIRC 的 pandas 类型)的值大于 x 的值(最大长度 62182),因此您的原始数据肯定有问题。

    【讨论】:

      猜你喜欢
      • 2017-05-04
      • 1970-01-01
      • 1970-01-01
      • 2017-02-06
      • 1970-01-01
      • 2016-01-15
      • 2020-07-08
      • 1970-01-01
      • 2020-08-29
      相关资源
      最近更新 更多