CodeBlanks Logo Icon CodeBlanks

Summary - Decision trees

In this group you learned decision trees, splitting criteria, and overfitting.

Summary: decision trees

In this group:

  • A tree splits data with yes/no questions; leaves decide and the rules are read with export_text.
  • Splits are chosen with Gini (default) or entropy.
  • max_depth, min_samples_leaf, ccp_alpha limit the tree.
  • An unlimited tree overfits; the training-test gap is the sign.
  • No scaling is needed; feature_importances_ gives feature contributions.

The instability of single trees is fixed by random forest and boosting, which you'll see later.

Practice Tree regression

Train a DecisionTreeRegressor(max_depth=1). Because there is a single split there are only two distinct predictions; print the predictions for [[2], [8]].

Starter code

model.py
from sklearn.tree import DecisionTreeRegressor

X = [[1], [2], [3], [7], [8], [9]]
y = [10, 12, 11, 30, 32, 31]

# buraya yaz

Sample solution

model.py
from sklearn.tree import DecisionTreeRegressor

X = [[1], [2], [3], [7], [8], [9]]
y = [10, 12, 11, 30, 32, 31]

m = DecisionTreeRegressor(max_depth=1).fit(X, y)
print(m.predict([[2], [8]]))

Expected output

[11. 31.]

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which model doesn't need scaling?

  • Every distance-based model
  • Decision tree Correct answer
  • KNN
  • Ridge without scaling

Explanation: A tree compares each feature with its own threshold; units don't change the decision.

2 Require at least 5 examples in every leaf.

Question
m = DecisionTreeClassifier(____=5)
Answer
m = DecisionTreeClassifier(min_samples_leaf=5)

Explanation: min_samples_leaf prevents small, over-specialised leaves.

3 Match the concepts.

Term Meaning
Gini Impurity measure
export_text Prints the rules
ccp_alpha Pruning strength
DecisionTreeRegressor Mean at the leaf

All lessons in Beginner

13 Project: Classification

  1. Preparing data
  2. Training model
  3. Prediction
  4. Evaluation