从 xgb.train() 获取概率 [英] get probability from xgb.train()
问题描述
我是 Python 和机器学习的新手.我在互联网上搜索了我的问题并尝试了人们建议的解决方案,但仍然没有得到它.如果有人可以帮助我,我将不胜感激.
I am new to Python and Machine learning. I have searched internet regarding my question and tried the solution people have suggested, but still not get it. Would really appreciate it if anyone can help me out.
我正在开发我的第一个 XGboost 模型.我已经使用 xgb.XGBClassifier 调整了参数,然后想对模型变量强制执行单调性.似乎我必须使用 xgb.train() 来强制执行单调性,如下面的代码所示.
I am working on my first XGboost model. I have tuned the parameters by using xgb.XGBClassifier, and then would like to enforce monotonicity on model variables. Seemingly I have to use xgb.train() to enforce monotonicity as shown in my code below.
xgb.train() 可以执行 predict(),但不能执行 predict_proba() 函数.那么如何从 xgb.train() 获得概率?
xgb.train() can do predict(), but NOT predict_proba() function. So how can I get probability from xgb.train() ?
我尝试使用 'objective':'multi:softprob' 而不是 'objective':'binary:logistic'.然后 score = bst_constr.predict(dtrain).但分数对我来说似乎不正确.
I have tried to use 'objective':'multi:softprob' instead of 'objective':'binary:logistic'. then score = bst_constr.predict(dtrain). But the score does not seem right to me.
非常感谢.
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'
推荐答案
所以根据我的理解,您正在尝试获取预测阶段每个类的概率.两种选择.
So based on my understanding, you are trying to obtain the probability for each class in the prediction phase. Two options.
您似乎正在使用 XGBoost 原生 API.然后只需选择
'objective':'multi:softprob'
作为参数,并使用bst_constr.predict
而不是bst_constr.predict_proba
.
It seems that you are using the XGBoost native api. Then just select the
'objective':'multi:softprob'
as the parameter, and use thebst_constr.predict
instead ofbst_constr.predict_proba
.
XGBoost 还提供了 scikit-learn api.但是,您应该使用 bst_constr = xgb.XGBClassifier(**params_constr)
启动模型,并使用 bst_constr.fit()
进行训练.然后你可以调用 bst_constr.predict_proba
来获取你想要的.您可以在此处参考 Scikit-Learn API 中的更多详细信息XGBoost.
XGBoost also provides the scikit-learn api. But then you should initiate the model with bst_constr = xgb.XGBClassifier(**params_constr)
, and use bst_constr.fit()
for training. Then you can call the bst_constr.predict_proba
to obtain what you want. You can refer here for more details Scikit-Learn API in XGBoost.
这篇关于从 xgb.train() 获取概率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!