Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to enforce Monotonic Constraints in XGBoost with ScikitLearn?

I build up a XGBoost model using scikit-learn and I am pretty happy with it. As fine tuning to avoid overfitting, I'd like to ensure monotonicity of some features but there I start facing some difficulties...

As far as I understood, there is no documentation in scikit-learn about xgboost (which I confess I am really surprised about - knowing that this situation is lasting for several months). The only documentation I found is directly on http://xgboost.readthedocs.io

On this website, I found out that monotonicity can be enforced using "monotone_constraints" option. I tried to use it in Scikit-Learn but I got an error message "TypeError: init() got an unexpected keyword argument 'monotone_constraints'"

Do you know a way to do it ?

Here is the code I wrote in python (using spyder):

grid = {'learning_rate' : 0.01, 'subsample' : 0.5, 'colsample_bytree' : 0.5,
    'max_depth' : 6, 'min_child_weight' : 10, 'gamma' : 1, 
    'monotone_constraints' : monotonic_indexes}
#'monotone_constraints' ~ = "(1,-1)"
m07_xgm06 = xgb.XGBClassifier(n_estimators=2000, **grid)
m07_xgm06.fit(X_train_v01_oe, Label_train, early_stopping_rounds=10, eval_metric="logloss", 
    eval_set=[(X_test1_v01_oe, Label_test1)])
like image 668
chapelon Avatar asked Mar 28 '17 17:03

chapelon


People also ask

How does XGBoost enforce monotonicity?

The way to enforce monotonicity constraint in XGBoost is by passing the monotone_constraints parameter. It will be a tuple-like string where 1 indicates an increasing constraint, 0 indicates no constraint and -1 indicates a decreasing constraint.

How do you enforce monotonicity?

For tree based methods (decision trees, random forests, gradient boosted trees), monotonicity can be forced during the model learning phase by not creating splits on monotonic features that would break the monotonicity constraint.

What is monotonicity constraints?

It is often the case in a modeling problem or project that the functional form of an acceptable model is constrained in some way. This may happen due to business considerations, or because of the type of scientific question being investigated.


1 Answers

In order to do this using the xgboost sklearn API, you need to upgrade to xgboost 0.81. They fixed the ability to set parameters controlled via kwargs as part of this PR: https://github.com/dmlc/xgboost/pull/3791

like image 87
jwan Avatar answered Nov 09 '22 23:11

jwan