【问题标题】:xgboost CV with custom folds pythonxgboost CV 与自定义折叠 python
【发布时间】:2017-08-17 18:25:14
【问题描述】:

我正在处理数据,每个患者都可以有不同数量的训练示例。在运行 Xgboost CV 时,我想确保来自同一患者的数据被限制为仅存在于同一折叠中,因此我需要使用折叠,其中可能有不同数量的索引。

在 xgb.cv 函数中使用 'fold' 参数传递包含索引的 numpy 数组列表时,我得到:

dtrain = dall.slice(np.concatenate([idset[i] for i in range(nfold) if k != i])) ValueError: 无法连接零维数组

我已经在 R 中实现了相同的过程,通过将我的自定义折叠作为列表传递,其中每个元素都是测试折叠索引的向量。

您能否建议将自定义索引传递给 Python XGBoost CV 函数的正确方法。谢谢!

【问题讨论】:

    标签: python cross-validation xgboost


    【解决方案1】:

    这是旧的,但当我遇到类似问题时,我在谷歌搜索上找到了一个答案。

    我想将 TimeSeriesSplit 与 xgboost cv 一起使用,但由于 folds 参数需要 KFold 或 StratifiedKFold,因此无法直接使用,但是,您可以将自己的索引列表作为元组列表提供,如下所示

    train1 =  [0, 1, 2, 3, 4] 
    test1  =  [4, 5, 6, 7, 8]
    
    train2 =  [9 ,10 ,11 ,12 ,13]
    test2 =   [14, 15, 16, 17, 18]
    
    train3=  [19, 20, 21, 22, 23, 24]
    test3 =  [25, 26, 27, 28, 29, 30]
    
    tsFolds = [(train1, test1), (train2, test2), (train3, test3)]
    
    xgbCV = xgb.cv(
        params = parameters, 
        dtrain = trainDMat, 
        num_boost_round = num_boost_round,
        nfold = len(tsFolds),
        folds = tsFolds,
        metrics = {'rmse'},
        early_stopping_rounds = early_stopping_rounds,
        verbose_eval = True,
        seed = seed     
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-31
      • 1970-01-01
      • 1970-01-01
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多