Trains a Extreme Gradient Boosting Model. XGBoost belongs to a family of boosting algorithms that creates an ensemble of weak learner to learn about data.
XGBTrainer
R6Class
object.
For usage details see Methods, Arguments and Examples sections.
bst = XGBTrainer$new(booster, objective, nthread, silent=0, n_estimators=100, learning_rate=0.3, gamma=0, max_depth=6, min_child_weight=1, subsample=1, colsample_bytree=1, lambda=1, alpha = 0, eval_metric, print_every = 50, feval, early_stopping, maximize, custom_objective, save_period, save_name, xgb_model, callbacks, verbose, num_class, weight, na_missing) bst$fit(X_train, "target", valid=NULL) tfidf$predict(X_test)
$new()
Initialises an instance of xgboost model
$fit()
fits model to an input train data using given parametes.
$cross_val()
performs cross validation on train data
$predict()
returns predictions by fitting the trained model on test data.
the detailed version of these arguments can found in xgboost documentation http://xgboost.readthedocs.io/en/latest/parameter.html
the trainer type, the values are gbtree(default)
, gblinear
, dart:gbtree
specify the learning task. Check the link above for all possible values.
the number of features to consider when looking for the best split
Possible values are auto
,sqrt
,log
,None
number of parallel threads used to run, default is to run using all threads available
0 means printing running messages, 1 means silent mode
number of trees to grow, default = 100
Step size shrinkage used in update to prevents overfitting. Lower the learning rate, more time it takes in training, value lies between between 0 and 1. Default = 0.3
the maximum depth of each tree, default = 6
set number of folds for cross validation
if stratified sampling to be done or not, default is TRUE
the list of CV folds' indices - either those passed through the folds parameter or randomly generated.
Minimum loss reduction required to make a further partition on a leaf node of the tree. The larger gamma is, the more conservative the algorithm will be. Value lies between 0 and infinity, Default = 0
Minimum sum of instance weight (hessian) needed in a child. If the tree partition step results in a leaf node with the sum of instance weight less than min_child_weight, then the building process will give up further partitioning. In linear regression task, this simply corresponds to minimum number of instances needed to be in each node. The larger min_child_weight is, the more conservative the algorithm will be. Value lies between 0 and infinity. Default = 1
Subsample ratio of the training instances. Setting it to 0.5 means that XGBoost would randomly sample half of the training data prior to growing trees. and this will prevent overfitting. Subsampling will occur once in every boosting iteration. Value lies between 0 and 1. Default = 1
Subsample ratio of columns when constructing each tree. Subsampling will occur once in every boosting iteration. Value lies between 0 and 1. Default = 1
L2 regularization term on weights. Increasing this value will make model more conservative. Default = 1
L1 regularization term on weights. Increasing this value will make model more conservative. Default = 0
Evaluation metrics for validation data, a default metric will be assigned according to objective
print training log after n iterations. Default = 50
custom evaluation function
Used to prevent overfitting, stops model training after this number of iterations if there is no improvement seen
If feval and early_stopping_rounds are set, then this parameter must be set as well. When it is TRUE, it means the larger the evaluation score the better.
custom objective function
when it is non-NULL, model is saved to disk after every save_period rounds, 0 means save at the end.
the name or path for periodically saved model file.
a previously built model to continue the training from. Could be either an object of class xgb.Booster, or its raw data, or the name of a file with a previously saved model.
a list of callback functions to perform various task during boosting. See callbacks. Some of the callbacks are automatically created depending on the parameters' values. User can provide either existing or their own callback methods in order to customize the training process.
If 0, xgboost will stay silent. If 1, xgboost will print information of performance. If 2, xgboost will print some additional information. Setting verbose > 0 automatically engages the cb.evaluation.log and cb.print.evaluation callback functions.
what information should be printed when verbose=1 or verbose=2. Watchlist is used to specify validation set monitoring during training. For example user can specify watchlist=list(validation1=mat1, validation2=mat2) to watch the performance of each round's model on mat1 and mat2
set number of classes in case of multiclassification problem
a vector indicating the weight for each row of the input.
by default is set to NA, which means that NA values should be considered as 'missing' by the algorithm. Sometimes, 0 or other extreme value might be used to represent missing values. This parameter is only used when input is a dense matrix.
# NOT RUN {
library(data.table)
df <- copy(iris)
# convert characters/factors to numeric
df$Species <- as.numeric(as.factor(df$Species))-1
# initialise model
xgb <- XGBTrainer$new(objective = 'multi:softmax',
maximize = FALSE,
eval_metric = 'merror',
num_class=3,
n_estimators = 2)
xgb$fit(df, 'Species')
# do cross validation to find optimal value for n_estimators
xgb$cross_val(X = df, y = 'Species',nfolds = 3, stratified = TRUE,
early_stopping = 1)
best_iter <- xgb$cv_model$best_iteration
xgb$show_importance()
# make predictions
preds <- xgb$predict(as.matrix(iris[,1:4]))
preds
# }
Run the code above in your browser using DataLab