How to limit the Tree Size of a Decision Tree in Sklearn

03.03.2021

Intro

When building a decision tree model, yoeu will often want to limit the tree size so that building the time buidling the model is reduced or to prevent over fitting. In this article, we will learn to limit the size of a random tree model using, Sklearn.

Limiting the Tree Size

To limit the size of a tree, we have several options. The main option max_depth depth. Setting this paramter will limit how deep the tree branches its decisions.

from sklearn.tree import DecisionTreeClassifier
from sklearn import datasets

iris = datasets.load_iris()
features = iris.data
target = iris.target

decisiontree = DecisionTreeClassifier(max_depth = 5,
                                      min_samples_split = 2,
                                      min_samples_leaf = 1,
                                      min_weight_fraction_leaf = 0,
                                      max_leaf_nodes = None,
                                      min_impurity_decrease = 0)

model = decisiontree.fit(features, target)
print(model.score())