Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

DecisionTreeClassifier

A supervised decision-tree classifier that learns feature-based rules for binary or multiclass prediction. Its split criterion, growth limits, weighting, randomness, and pruning behavior are configurable.

How it works

The classifier recursively chooses feature-based splits to form a tree, using a selectable impurity or information-gain criterion. Training stops according to depth, sample, leaf, impurity, or leaf-count constraints; the fitted tree then predicts classes and probabilities for new samples.

Syntax

class sklearn.tree.DecisionTreeClassifier(*, criterion='gini', splitter='best', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, random_state=None, max_leaf_nodes=None, min_impurity_decrease=0.0, class_weight=None, ccp_alpha=0.0, monotonic_cst=None)

Parameters

NameTypeDescription
criterion{"gini", "entropy", "log_loss"}Split-quality function: gini, entropy, or log_loss; default gini.
splitter{"best", "random"}Split-selection strategy: best or random; default best.
max_depthint or NoneMaximum tree depth; None means no explicit depth limit.
min_samples_splitint or floatMinimum samples required to split an internal node; integer count or fractional proportion.
min_samples_leafint or floatMinimum samples required in a leaf; integer count or fractional proportion.
min_weight_fraction_leaffloatMinimum weighted fraction of the total input weight required in a leaf.
max_featuresint, float, {"sqrt", "log2"} or NoneNumber of features considered for a split, or None for all features.
random_stateint, RandomState instance or NoneControls estimator randomness.
max_leaf_nodesint or NoneMaximum number of leaf nodes; None means unlimited.
min_impurity_decreasefloatMinimum weighted impurity decrease required for a split.
class_weightdict, list of dict or "balanced"Class weights, or None for uniform weighting.
ccp_alphanon-negative floatComplexity-pruning parameter; larger values prune more aggressively.

Returns

A fitted DecisionTreeClassifier instance from fit; predict returns class labels and predict_proba returns class probabilities.

Key points

  • The class implements supervised classification with a tree of feature-based decisions.
  • criterion supports gini, entropy, and log_loss for evaluating candidate splits.
  • splitter chooses the split strategy independently at each node; best is deterministic in strategy, while random samples candidate splits.
  • max_depth limits how many levels the learned tree can contain; None allows expansion until another stopping condition applies.
  • min_samples_split prevents an internal node from splitting unless enough samples reach it.
  • min_samples_leaf enforces a lower bound on samples represented by every leaf.

Common mistakes

  • Defaults can create fully grown, unpruned trees; constrain complexity or apply pruning when overfitting is a concern.

Example: Fit and predict with a decision tree

from sklearn.tree import DecisionTreeClassifier

X = [[0, 0], [1, 1], [0, 1], [1, 0]]
y = [0, 1, 1, 0]

model = DecisionTreeClassifier(max_depth=2, random_state=0)
model.fit(X, y)
print(model.predict([[0, 0], [1, 1]]))
print(model.predict_proba([[0, 1]]))

Creates a bounded-depth classifier, fits it to labeled samples, then predicts classes and class probabilities.

When to use it

Explainable classification rules

Need an interpretable baseline for labeled tabular data.

Multiclass classification

Class labels include more than two categories.

Class-imbalance handling

Training data contains unequal class frequencies.

Regularized tree modeling

Model complexity must be constrained or pruned.

Interview questions

QWhat does DecisionTreeClassifier do?

It is a supervised tree model that learns feature-based decision rules for classification. It supports binary and multiclass classification and can return both predicted classes and class probabilities.

Follow-up: How would you reduce overfitting? Limit max_depth, increase min_samples_leaf, restrict max_leaf_nodes, or use cost-complexity pruning with ccp_alpha.

QHow do criterion and splitter affect training?

criterion selects the split-quality measure: gini uses Gini impurity, while entropy and log_loss use Shannon information gain. splitter controls whether the best split or a random candidate strategy is used.

Follow-up: When might random splitting help? It can add variation among trees or models, but a fixed random_state is needed for repeatable results.

QHow do sample-size constraints control the tree?

min_samples_split sets the minimum samples needed to split an internal node, while min_samples_leaf sets the minimum samples permitted in a leaf. Integer values specify counts; float values are fractions converted using a ceiling operation.

Follow-up: Why use min_samples_leaf? It prevents leaves supported by too few observations and can make predictions less sensitive to individual samples.

  • DecisionTreeRegressor
  • ExtraTreeClassifier
  • ExtraTreesClassifier
  • RandomForestClassifier
  • HistGradientBoostingClassifier
  • sklearn.tree.plot_tree

Source: DecisionTreeClassifier — scikit-learn 1.9.1 documentation. Checked 2026-10-15.