+4 votes
in Machine Learning by (75.1k points)
I have used the default parameters to train the CatBoost model. How can I find the number of trees in the model?

1 Answer

+4 votes
by (353k points)
selected by
Best answer

Attribute "tree_count_" of the CatBoost classifier can be used to find the number of trees in the model. The number of trees depends on the parameter "iterations" value. Usually, the number of trees is the same as the parameter "iterations" value. However, this number can differ from the value specified in the --iterations in the following cases:

  • The overfitting detector stops the training.
  • The --use-best-model training parameter is set to True.
Here is a sample code to find the number of trees in the trained model:
from catboost import CatBoostClassifier
import numpy as np

def generate_train_test_data():
    Randomly generate train test data and labels

    # train data
    feature_count = 10
    train_data_count = 500
    train_data = np.reshape(np.random.random(train_data_count * feature_count), (train_data_count, feature_count))
    train_labels = np.round(np.random.random(train_data_count))

    # test data
    test_data_count = 100
    test_data = np.reshape(np.random.random(test_data_count * feature_count), (test_data_count, feature_count))
    test_labels = np.round(np.random.random(test_data_count))

    return train_data, train_labels, test_data, test_labels

if __name__ == "__main__":
    X_train, y_train, X_test, y_test = generate_train_test_data()

    # train the model
    model = CatBoostClassifier(verbose=False)
    model.fit(X_train, y_train)

    # get the tree count in the model
    print("Tree count: ", model.tree_count_)