Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Classification lessons
Margins and rules · ML-C14 · 25–40 MIN

Classification trees

Learn recursive splits, impurity, class leaves and validation within Classification.

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeCompute and inspect the evidence
  3. PractiseAdapt the Python
  4. TransferCompute and inspect the evidence
  5. ApplyBuild a guided full workflow

Understand the idea

A classification tree asks a sequence of threshold questions. A useful split separates class counts, reducing impurity. A leaf predicts from the classes reaching it. Scaling is unnecessary; depth and minimum leaf size constrain overfitting.

Illustrative data

Illustrative data
Class(width, length)
Class At1: (2, 3); t2: (3, 5); t3: (4, 4); t7: (7, 8)
Class Bt4: (7, 3); t5: (8, 6); t6: (6, 6)

Click the highlighted code to change the result.

example_model = DecisionTreeClassifier(max_depth=2, random_state=42)
example_model.fit(example[["width", "length"]], example["class"])
row = pd.DataFrame([{"width": , "length": }])
example_model.predict(row)  # A
t2 (3, 5): width 3 ≤ 5: yes → predict A. Observed training class: A. Highlighted branches trace this one row through the illustrative fitted tree; leaf counts refer to the seven training rows.width ≤ 5?yesnoA · 3 rowslength ≤ 7?yesnoB · 3 rowsA · 1 row

t2: width 3 ≤ 5: yes → predict A.

What the syntax does

DecisionTreeClassifier(max_depth=3, random_state=42)
  • Creates a decision tree classifier.
  • max_depth: 3, 5, None
  • random_state: 42, None
  • max_depth is a positive integer or None (the default, without this cap). min_samples_leaf is a positive count or a fitting-row fraction.
  • More depth or smaller leaves permits finer fits and can overfit; validate before opening final rows.
  • Scaling is unnecessary for these thresholds.
model.apply(row)
  • Finds the leaf reached by a row; predict returns the class selected at that leaf.

Supplied for this practice

These objects are prepared afresh before your editor runs.

X
Feature dataframe for the declared population, preserving row indices.
X = df[['length', 'width']]
y
Target series, aligned with X.
y = df.label
X_train / X_test / y_train / y_test
Training feature rows from the declared split. Final-test feature rows from the declared split. Training targets, aligned with X_train. Final-test targets, aligned with X_test.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)

Outputs you create

leaf
  1. Requested result described in the task and deliverables.
label
  1. Requested result described in the task and deliverables.
CLASS180 · first 8 rows
lengthwidthlabel
236.033-0.805568A
581.2970.728558A
-1511.27-1.00866A
99.0248-0.24496A
-13.0141-0.660765A
681.1790.602475A
51.14720.873157A
362.131-0.665605A
Your task
  1. Fit a tree with maximum depth 3.
  2. For the first test row, store its terminal node ID in leaf and its predicted class in label.
leaflabel
Hints

Think: A tree follows feature thresholds to a leaf with a learned class prediction.

Tools: DecisionTreeClassifier, apply and predict.

Approach: Fit the declared depth-limited tree, then obtain the first test row’s leaf and label.

Explained solution
from sklearn.tree import DecisionTreeClassifier
model=DecisionTreeClassifier(max_depth=3,random_state=42).fit(X_train,y_train)
leaf=int(model.apply(X_test.iloc[:1])[0])
label=model.predict(X_test.iloc[:1])[0]

The two outputs connect tree traversal with classification without requiring any regression-tree prerequisite.

Ctrl/⌘+Enter: Run · Tab: indent · Esc then Tab: leave editor

Python loads when you run. Code and results stay in this activity only.

Run your code to inspect its output. Check uses that same run.