【问题标题】:XGBoostError: b'[19:12:58] src/metric/rank_metric.cc:89: Check failed: (preds.size()) == (info.labels.size()) label size predict size not match'XGBoostError: b'[19:12:58] src/metric/rank_metric.cc:89: Check failed: (preds.size()) == (info.labels.size()) label size predict size not match'
【发布时间】:2017-08-04 23:41:49
【问题描述】:

我正在为我的训练集训练一个 XGBoostClassifier。

我的训练特征是 (45001, 10338) 的形状,它是一个 numpy 数组,我的训练标签的形状是 (45001,) [我有 1161 个唯一标签,所以我对标签进行了标签编码] 这也是一个 numpy 数组。

从文档中,它清楚地表明我可以从 numpy 数组创建 DMatrix。所以我直接使用上面提到的训练特性和标签作为 numpy 数组。但是我收到以下错误

---------------------------------------------------------------------------
XGBoostError                              Traceback (most recent call last)
<ipython-input-30-3de36245534e> in <module>()
     13  scale_pos_weight=1,
     14  seed=27)
---> 15 modelfit(xgb1, train_x, train_y)

<ipython-input-27-9d215eac135e> in modelfit(alg, train_data_features, train_labels, useTrainCV, cv_folds, early_stopping_rounds)
      6         xgtrain = xgb.DMatrix(train_data_features, label=train_labels)
      7         cvresult = xgb.cv(xgb_param, xgtrain, num_boost_round=alg.get_params()['n_estimators'], nfold=cv_folds,
----> 8             metrics='auc',early_stopping_rounds=early_stopping_rounds)
      9         alg.set_params(n_estimators=cvresult.shape[0])
     10 

/home/carnd/anaconda3/envs/dl/lib/python3.5/site-packages/xgboost/training.py in cv(params, dtrain, num_boost_round, nfold, stratified, folds, metrics, obj, feval, maximize, early_stopping_rounds, fpreproc, as_pandas, verbose_eval, show_stdv, seed, callbacks)
    399         for fold in cvfolds:
    400             fold.update(i, obj)
--> 401         res = aggcv([f.eval(i, feval) for f in cvfolds])
    402 
    403         for key, mean, std in res:

/home/carnd/anaconda3/envs/dl/lib/python3.5/site-packages/xgboost/training.py in <listcomp>(.0)
    399         for fold in cvfolds:
    400             fold.update(i, obj)
--> 401         res = aggcv([f.eval(i, feval) for f in cvfolds])
    402 
    403         for key, mean, std in res:

/home/carnd/anaconda3/envs/dl/lib/python3.5/site-packages/xgboost/training.py in eval(self, iteration, feval)
    221     def eval(self, iteration, feval):
    222         """"Evaluate the CVPack for one iteration."""
--> 223         return self.bst.eval_set(self.watchlist, iteration, feval)
    224 
    225 

/home/carnd/anaconda3/envs/dl/lib/python3.5/site-packages/xgboost/core.py in eval_set(self, evals, iteration, feval)
    865             _check_call(_LIB.XGBoosterEvalOneIter(self.handle, iteration,
    866                                                   dmats, evnames, len(evals),
--> 867                                                   ctypes.byref(msg)))
    868             return msg.value
    869         else:

/home/carnd/anaconda3/envs/dl/lib/python3.5/site-packages/xgboost/core.py in _check_call(ret)
    125     """
    126     if ret != 0:
--> 127         raise XGBoostError(_LIB.XGBGetLastError())
    128 
    129 

XGBoostError: b'[19:12:58] src/metric/rank_metric.cc:89: Check failed: (preds.size()) == (info.labels.size()) label size predict size not match'

请在下面找到我的型号代码:

def modelfit(alg, train_data_features, train_labels,useTrainCV=True, cv_folds=5, early_stopping_rounds=50):

    if useTrainCV:
        xgb_param = alg.get_xgb_params()
        xgb_param['num_class'] = 1161   
        xgtrain = xgb.DMatrix(train_data_features, label=train_labels)
        cvresult = xgb.cv(xgb_param, xgtrain, num_boost_round=alg.get_params()['n_estimators'], nfold=cv_folds,
            metrics='auc',early_stopping_rounds=early_stopping_rounds)
        alg.set_params(n_estimators=cvresult.shape[0])

    #Fit the algorithm on the data
    alg.fit(train_data_features, train_labels, eval_metric='auc')

    #Predict training set:
    dtrain_predictions = alg.predict(train_data_features)
    dtrain_predprob = alg.predict_proba(train_data_features)[:,1]

    #Print model report:
    print("\nModel Report")
    print("Accuracy : %.4g" % metrics.accuracy_score(train_labels, dtrain_predictions))

我在上面的地方哪里出错了?

我的分类如下:

xgb1 = xgb.XGBClassifier(
 learning_rate =0.1,
 n_estimators=50,
 max_depth=5,
 min_child_weight=1,
 gamma=0,
 subsample=0.8,
 colsample_bytree=0.8,
 objective='multi:softmax',
 nthread=4,
 scale_pos_weight=1,
 seed=27)

编辑 - 2 改变评价指标后,

---------------------------------------------------------------------------
XGBoostError                              Traceback (most recent call last)
<ipython-input-9-30c62a886c2e> in <module>()
     13  scale_pos_weight=1,
     14  seed=27)
---> 15 modelfit(xgb1, train_x_trail, train_y_trail)

<ipython-input-8-9d215eac135e> in modelfit(alg, train_data_features, train_labels, useTrainCV, cv_folds, early_stopping_rounds)
      6         xgtrain = xgb.DMatrix(train_data_features, label=train_labels)
      7         cvresult = xgb.cv(xgb_param, xgtrain, num_boost_round=alg.get_params()['n_estimators'], nfold=cv_folds,
----> 8             metrics='auc',early_stopping_rounds=early_stopping_rounds)
      9         alg.set_params(n_estimators=cvresult.shape[0])
     10 

/home/carnd/anaconda3/envs/dl/lib/python3.5/site-packages/xgboost/training.py in cv(params, dtrain, num_boost_round, nfold, stratified, folds, metrics, obj, feval, maximize, early_stopping_rounds, fpreproc, as_pandas, verbose_eval, show_stdv, seed, callbacks)
    398                            evaluation_result_list=None))
    399         for fold in cvfolds:
--> 400             fold.update(i, obj)
    401         res = aggcv([f.eval(i, feval) for f in cvfolds])
    402 

/home/carnd/anaconda3/envs/dl/lib/python3.5/site-packages/xgboost/training.py in update(self, iteration, fobj)
    217     def update(self, iteration, fobj):
    218         """"Update the boosters for one iteration"""
--> 219         self.bst.update(self.dtrain, iteration, fobj)
    220 
    221     def eval(self, iteration, feval):

/home/carnd/anaconda3/envs/dl/lib/python3.5/site-packages/xgboost/core.py in update(self, dtrain, iteration, fobj)
    804 
    805         if fobj is None:
--> 806             _check_call(_LIB.XGBoosterUpdateOneIter(self.handle, iteration, dtrain.handle))
    807         else:
    808             pred = self.predict(dtrain)

/home/carnd/anaconda3/envs/dl/lib/python3.5/site-packages/xgboost/core.py in _check_call(ret)
    125     """
    126     if ret != 0:
--> 127         raise XGBoostError(_LIB.XGBGetLastError())
    128 
    129 

XGBoostError: b'[03:43:03] src/objective/multiclass_obj.cc:42: Check failed: (info.labels.size()) != (0) label set cannot be empty'

【问题讨论】:

    标签: python numpy xgboost


    【解决方案1】:

    您得到的原始错误是因为该指标不是为多类分类设计的(请参阅here)。

    您可以使用 xgboost 的scikit learn wrapper 来解决这个问题。我用这个包装器修改了你的代码,以产生类似的功能。我不确定您为什么要进行网格搜索,因为您没有枚举参数。相反,您使用的是在xgb1 中指定的参数。这是修改后的代码:

    import xgboost as xgb
    import sklearn
    import numpy as np
    from sklearn.model_selection import GridSearchCV
    
    def modelfit(alg, train_data_features, train_labels,useTrainCV=True, cv_folds=5):
    
        if useTrainCV:
            params=alg.get_xgb_params()
            xgb_param=dict([(key,[params[key]]) for key in params])
    
            boost = xgb.sklearn.XGBClassifier()
            cvresult = GridSearchCV(boost,xgb_param,cv=cv_folds)
            cvresult.fit(X,y)
            alg=cvresult.best_estimator_
    
    
        #Fit the algorithm on the data
        alg.fit(train_data_features, train_labels)
    
        #Predict training set:
        dtrain_predictions = alg.predict(train_data_features)
        dtrain_predprob = alg.predict_proba(train_data_features)[:,1]
    
        #Print model report:
        print("\nModel Report")
        print("Accuracy : %.4g" % sklearn.metrics.accuracy_score(train_labels, dtrain_predictions))
    
    xgb1 = xgb.sklearn.XGBClassifier(
     learning_rate =0.1,
     n_estimators=50,
     max_depth=5,
     min_child_weight=1,
     gamma=0,
     subsample=0.8,
     colsample_bytree=0.8,
     objective='multi:softmax',
     nthread=4,
     scale_pos_weight=1,
     seed=27)    
    
    
    X=np.random.normal(size=(200,30))
    y=np.random.randint(0,5,200)
    
    modelfit(xgb1, X, y)
    

    我得到的输出是

    Model Report
    Accuracy : 1
    

    请注意,我为数据使用了小得多的大小。以你提到的大小,算法可能会很慢。

    【讨论】:

    • 在 tensorflow 中,我们创建批处理并运行它们。我可以批量运行这个算法吗?比如说100条记录?我怎样才能保存这个模型并再次训练它?我会接受你的回答
    • 当你在 tensorflow 上训练神经网络时,你会使用批量梯度下降。因此,您可以分块进行。但是,xgboost 的操作方式不同,因此您不能只将其分成块。但是,我查看了 xgboost 常见问题页面:xgboost.readthedocs.io/en/latest/faq.html,在关于大型数据集的部分中,他们写道:XGBoost 旨在提高内存效率。通常只要数据适合您的内存,它就可以处理问题(这通常意味着数百万个实例)。如果内存不足,请检查外部内存版本或 xgboost 的分布式版本
    • 因此,根据上面的引用,您似乎可以尝试在您的计算机上按原样运行代码。您还可以将 verbose=2 放在 GridSearchCV 中,以便它在运行时打印更多详细信息。如果它不起作用,您可以尝试分布式版本。他们从常见问题页面(我在上一条评论中链接到的那个)给出了一个链接。您也可以设置 useTrainCV=False。由于您有一组参数,因此您实际上并不需要 gridsearch,因此您可以跳过代码的那部分(这是当前代码中最重的部分)。
    【解决方案2】:

    错误是 b/c 您尝试使用 AUC 评估指标进行多类分类,但 AUC 仅适用于二类问题。在 xgboost 实现中,“auc”期望预测大小与标签大小相同,而您的多类预测大小将为 45001*1161。使用“mlogloss”或“merror”多类指标。

    P.S.:目前,xgboost 在有这么多类的情况下会相当慢,因为在训练期间预测缓存效率会有些低。

    【讨论】:

    • 更改评价指标后请检查上面的新错误
    猜你喜欢
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    • 2016-12-03
    • 2013-07-28
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多