Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know the number of tree created in XGBoost

I have a question about XGBoost.

Do you know how to know the number of tree created in XGBoost? Unlike RandomForest, which model maker decides how many trees are made, XGBoost basically continues to create the trees till the loss function reaches certain figure. Therefore I want to know this.

Thank you.

like image 745
kanam Avatar asked May 19 '18 14:05

kanam


People also ask

How many trees are in gradient boosting?

In practice, you'll typically see Gradient Boost being used with a maximum number of leaves of between 8 and 32.

What is the role of number of trees in XGBoost?

Xgboost is a gradient boosting method, as such it adds trees in every iteration to improve the prediction accuracy.

How trees are built in XGBoost?

In boosting, the trees are built sequentially such that each subsequent tree aims to reduce the errors of the previous tree. Each tree learns from its predecessors and updates the residual errors. Hence, the tree that grows next in the sequence will learn from an updated version of the residuals.


1 Answers

It's a bit crooked, but what I'm currently doing is dump-ing the model (XGBoost produces a list where each element is a string representation of a single tree), and then counting how many elements are in the list:

# clf is a XGBoost model fitted using the sklearn API
dump_list = clf.get_booster().get_dump()
num_trees = len(dump_list)
like image 80
OmerB Avatar answered Sep 19 '22 04:09

OmerB