33
$\begingroup$

XGBoost have been doing a great job, when it comes to dealing with both categorical and continuous dependant variables. But, how do I select the optimized parameters for an XGBoost problem?

This is how I applied the parameters for a recent Kaggle problem:

param <- list(  objective           = "reg:linear", 
                booster = "gbtree",
                eta                 = 0.02, # 0.06, #0.01,
                max_depth           = 10, #changed from default of 8
                subsample           = 0.5, # 0.7
                colsample_bytree    = 0.7, # 0.7
                num_parallel_tree   = 5
                # alpha = 0.0001, 
                # lambda = 1
)


clf <- xgb.train(   params              = param, 
                    data                = dtrain, 
                    nrounds             = 3000, #300, #280, #125, #250, # changed from 300
                    verbose             = 0,
                    early.stop.round    = 100,
                    watchlist           = watchlist,
                    maximize            = FALSE,
                    feval=RMPSE
)

All I do to experiment is randomly select (with intuition) another set of parameters for improving on the result.

Is there anyway I automate the selection of optimized(best) set of parameters?

(Answers can be in any language. I'm just looking for the technique)

$\endgroup$

3 Answers 3

42
$\begingroup$

Whenever I work with xgboost I often make my own homebrew parameter search but you can do it with the caret package as well like KrisP just mentioned.

  1. Caret

See this answer on Cross Validated for a thorough explanation on how to use the caret package for hyperparameter search on xgboost. How to tune hyperparameters of xgboost trees?

  1. Custom Grid Search

I often begin with a few assumptions based on Owen Zhang's slides on tips for data science P. 14

enter image description here

Here you can see that you'll mostly need to tune row sampling, column sampling and maybe maximum tree depth. This is how I do a custom row sampling and column sampling search for a problem I am working on at the moment:

searchGridSubCol <- expand.grid(subsample = c(0.5, 0.75, 1), 
                                colsample_bytree = c(0.6, 0.8, 1))
ntrees <- 100

#Build a xgb.DMatrix object
DMMatrixTrain <- xgb.DMatrix(data = yourMatrix, label = yourTarget)

rmseErrorsHyperparameters <- apply(searchGridSubCol, 1, function(parameterList){

    #Extract Parameters to test
    currentSubsampleRate <- parameterList[["subsample"]]
    currentColsampleRate <- parameterList[["colsample_bytree"]]

    xgboostModelCV <- xgb.cv(data =  DMMatrixTrain, nrounds = ntrees, nfold = 5, showsd = TRUE, 
                           metrics = "rmse", verbose = TRUE, "eval_metric" = "rmse",
                           "objective" = "reg:linear", "max.depth" = 15, "eta" = 2/ntrees,                               
                           "subsample" = currentSubsampleRate, "colsample_bytree" = currentColsampleRate)

    xvalidationScores <- as.data.frame(xgboostModelCV)
    #Save rmse of the last iteration
    rmse <- tail(xvalidationScores$test.rmse.mean, 1)

    return(c(rmse, currentSubsampleRate, currentColsampleRate))

})

And combined with some ggplot2 magic using the results of that apply function you can plot a graphical representation of the search.My xgboost hyperparameter search

In this plot lighter colors represent lower error and each block represents a unique combination of column sampling and row sampling. So if you want to perform an additional search of say eta (or tree depth) you will end up with one of these plots for each eta parameters tested.

I see you have a different evaluation metric (RMPSE), just plug that in the cross validation function and you'll get the desired result. Besides that I wouldn't worry too much about fine tuning the other parameters because doing so won't improve performance too much, at least not so much compared to spending more time engineering features or cleaning the data.

  1. Others

Random search and Bayesian parameter selection are also possible but I haven't made/found an implementation of them yet.

Here is a good primer on bayesian Optimization of hyperparameters by Max Kuhn creator of caret.

http://blog.revolutionanalytics.com/2016/06/bayesian-optimization-of-machine-learning-models.html

$\endgroup$
3
  • $\begingroup$ I have a question regarding min_child_weight parameter. it says 3/(% of rare events) Suppose I have 3 classes 0 (3.4%), 1 (96.4%) and 2 (0.2%). so should it be 3/3.6 or 3/0.2 ? I am guessing the former $\endgroup$
    – user14204
    Sep 22, 2016 at 5:24
  • $\begingroup$ In most cases, xgboost treats anything below 5% as rare events in most problems, I guess it's one of the quirks of the algorithm. I like to think of it as anything below a click in a CTR problem is a "rare event". So in this case it would be 3/3.6. I sometimes is lower but not that often. $\endgroup$
    – wacax
    Sep 29, 2016 at 23:37
  • 1
    $\begingroup$ rBayesianOptimization package makes implementation of BO pretty straightforward $\endgroup$ Dec 13, 2017 at 3:12
5
$\begingroup$

You could use the caret package to do hyperparameter space search, either through a grid search , or through random search.

$\endgroup$
0
$\begingroup$

Grid, Random, Bayesian and PSO ... etc..

When you work with XGBoost all of the above doesn't matter, because XGB is really fast so you can use Grid with many hyperparametrs until you find you solution.

One thing that may help you: use approx method, it always give me the lowest mse error.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.