XGBoost, you know this name if you're familiar with machine learning competitions. It's the algorithm you want to try: it's very fast, effective, easy to use, and comes with very cool features. I'll show you in this blog post how you can use it in DSS without coding and some more advanced things you can do with Python code.
Since DSS 3.0, XGBoost is natively integrated into DSS virtual machine learning, meaning you can train XGBoost models without writing any code.
You'll find more information about how to use XGBoost in visual machine learning in the reference documentation.
The rest of this Howto covers how to use XGBoost manually, for DSS 2.X
XGBoost is a gradient boosting tree method. Gradient because it use gradient descent: it's a way to find a local minimum of a function: the algorithm follow the path of the descent. Boosting is a technique which consist of the fact that a set of weak learners is stronger than a single strong learner. And tree because it's a decision tree algorithm. Explore Tianqi Chen presentation to grasp the theoretical aspect.
XGBoost isn't in DSS by default, you have to get it on github, compile it and install the python module on the python of DSS.
In your server console:
To check if everything works fine you can try to import xgboost in an ipython notebook.
Take a dataset and in your analysis, go to Models / Settings / Algorithms and add a custom model. Here you'll have to write 3 lines of code to specify the model name, import xgboost and create your classifier:
Let's compare it to scikit learn Gradient Boosting with both default parameter:
Same R2 score but XGBoost was trained in 20 seconds against 5 minutes for the scikit learn GBT!
You can now deploy it like another model in DSS but maybe you'll want to change the default parameters to optimize your score!
You can set some parameters to your XGBoost:
max_depth : int
Maximum tree depth for base learners.
learning_rate : float
Boosting learning rate (xgb's "eta")
n_estimators : int
Number of boosted trees to fit.
silent : boolean
Whether to print messages while running boosting.
objective : string
Specify the learning task and the corresponding learning objective.
nthread : int
Number of parallel threads used to run xgboost.
gamma : float
Minimum loss reduction required to make a further partition
on a leaf node of the tree.
min_child_weight : int
Minimum sum of instance weight(hessian) needed in a child.
max_delta_step : int
Maximum delta step we allow each tree's weight estimation to be.
subsample : float
Subsample ratio of the training instance.
colsample_bytree : float
Subsample ratio of columns when constructing each tree.
base_score:
The initial prediction score of all instances, global bias.
seed : int
Random number seed.
missing : float, optional
Value in the data which needs to be present as a missing value.
If None, defaults to np.nan.
You have 2 ways to control overfitting in xgboost:
Xgboost can take in input sparse matrix. That's very useful because when you have categorical variables with high cardinality, you can convert them into dummies matrix without being out of memory!
For this I use a python function:
This return a sparse matrix of 3 columns, one by value of VAR_0001:
You can concatenate this matrix with other dummies matrix with the scipy hstack function:
A really cool feature is early stopping. As you are going to learn more and more trees, you will overfit your training dataset. Early stopping able you to specify a validation dataset and the number of iteration the algorithm should stop if the score on your validation dataset didn't increase.
To use it, you can specify in the fit method of the classifier an evaluation set, an evaluation method and the early stopping round number:
I set explicitly the n_estimators to a very large number. In your job log you'll see the score increasing on the dataset you put in the eval_set list:
Note that you can define your own evaluation metric instead.
You can get the features importance easily in clf.booster().get_fscore()where clf is your trained classifier.
In a iPython notebook, I use this code to see it:
Fine-tuning your XGBoost can be done by exploring the space of parameters possibilities. For this task I use the package Hyperopt. Hyperopt is a Python library for optimizing over awkward search spaces with real-valued, discrete, and conditional dimensions.
Here an exemple of python recipe to use it:
After loading your datasets of training and validation, I define my objective function.
This function train a model, evaluate it and return the error on the validation set. I define the space I want to explore, here I want to try values from 5 to 30 for max_depth, from 1 to 10 for min_child_weight and from 0.8 to 1 for subsample.
Hyperopt will minimise this error in a maximum of 100 experiments.
Here more documentation: hyperopt
That's it! If you want to talk more about xgboost or anything related to data science, send me an email!
Please fill out the form below to receive the success story by email:
How can we come back to you ?