R Xgboost错误:标签必须位于[0,num\u类),num\u类=2

R Xgboost错误:标签必须位于[0,num\u类),num\u类=2,r,xgboost,R,Xgboost,在为泰坦尼克号问题实现XGboost时出现此错误 xgb.iter.update(bst$handle,dtrain,迭代-1,obj)中出错: [03:26:03]合并/。/src/objective/multi class_obj.cc:75:检查 失败:label\u error>=0&&label\u error

在为泰坦尼克号问题实现XGboost时出现此错误

xgb.iter.update(bst$handle,dtrain,迭代-1,obj)中出错:
[03:26:03]合并/。/src/objective/multi class_obj.cc:75:检查 失败:label\u error>=0&&label\u error 以下是我的代码:

#Parameter  ie no of class
nc <- length(unique(train_label))
nc
xgb_params <- list("objective"="multi:softprob",
                       "eval_metric"="mlogloss",
                       "num_class"=nc)
watchlist <- list(train=train_matix,test=test_matix)


#XGB Model
bst_model <- xgb.train(params = xgb_params,data = train_matix, nrounds = 100,watchlist = watchlist)
#参数即类的编号

nc我猜您有多标签问题。 由于没有示例,我只能猜测您应该通过
nc+1
而不是nc。
祝你好运。

我这样解决了它。我的类标签是-1、0和1。因此我的num_class=3。为了与[0,3]范围兼容,我必须将类标签增加1。请注意,在这个范围中,3被排除,有效标签是0、1、2。因此,我转换的类标签是0,1,2

此外,我将代码更改为用于多类分类

目标已更改为“multi:softmax”,并添加了“num_class”参数

xgb1 = XGBClassifier(
    learning_rate=0.1,
    n_estimators=1000,
    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,
    num_class=3,
    )
在modelfit()中,函数“auc”替换为“merror”

def modelfit(alg, dtrain, predictors, useTrainCV=True, cv_folds=5, early_stopping_rounds=50):
if useTrainCV:
    xgb_param = alg.get_xgb_params()
    #change the class labels
    dtrain[target] = dtrain[target] + 1
    xgtrain = xgb.DMatrix(dtrain[predictors].values, label=dtrain[target].values)

    cvresult = xgb.cv(xgb_param, xgtrain, num_boost_round=xgb_param['n_estimators'], nfold=cv_folds,
                      metrics='merror', early_stopping_rounds=early_stopping_rounds)
    alg.set_params(n_estimators=cvresult.shape[0])
    print(cvresult.shape[0])

# Fit the algorithm on the data
alg.fit(dtrain[predictors], dtrain[target], eval_metric='merror')

# Predict training set:
dtrain_predictions = alg.predict(dtrain[predictors])

# Print model report:
print("\nModel Report")
print("Accuracy : %.4g" % metrics.accuracy_score(dtrain[target].values, dtrain_predictions))

你能提供一个例子吗?请提供
列车标签的样本。
。我正在尝试使用XGBoost列车来实现泰坦尼克号卡格尔问题。首先,你应该使用目标函数的
二进制:logistic
。可能重复