从 xgb.train() 获取概率

jea*_*ean 5 python probability xgboost

我是 Python 和机器学习的新手。我在网上搜索了我的问题,并尝试了人们建议的解决方案,但仍然没有得到它。如果有人能帮助我,我将非常感激。

我正在开发我的第一个 XGboost 模型。我已经使用 xgb.XGBClassifier 调整了参数,然后想强制模型变量的单调性。看来我必须使用 xgb.train() 来强制单调性,如下面的代码所示。

xgb.train()可以执行predict()函数,但不能执行predict_proba()函数。那么如何从 xgb.train() 获得概率?

我尝试使用 'objective':'multi:softprob' 而不是 'objective':'binary:logistic'。然后得分 = bst_constr.predict(dtrain)。但分数对我来说似乎不太合适。

太感谢了。

params_constr={
    'base_score':0.5, 
    'learning_rate':0.1, 
    'max_depth':5,
    'min_child_weight':100, 
    'n_estimators':200, 
    'nthread':-1,
    'objective':'binary:logistic', 
    'seed':2018, 
    'eval_metric':'auc' 
}

params_constr['monotone_constraints'] = "(1,1,0,1,-1,-1,0,0,1,-1,1,0,1,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,)" 

dtrain = xgb.DMatrix(X_train, label = y_train)

bst_constr = xgb.train(params_constr, dtrain)


X_test['score']=bst_constr.predict_proba(X_test)[:,1]

AttributeError: 'Booster' object has no attribute 'predict_proba'
Run Code Online (Sandbox Code Playgroud)

小智 7

因此,根据我的理解,您试图在预测阶段获得每个类别的概率。两个选择。

  1. 您似乎正在使用 XGBoost 本机 api。然后只需选择'objective':'multi:softprob'作为参数,并使用bst_constr.predict代替bst_constr.predict_proba

  2. XGBoost 还提供 scikit-learn api。但是,您应该使用 启动模型bst_constr = xgb.XGBClassifier(**params_constr),并用于bst_constr.fit()训练。然后你就可以调用bst_constr.predict_proba来获取你想要的东西。您可以参考此处了解XGBoost 中的 Scikit-Learn API 的更多详细信息。