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.
Python skill: Creates a tree that splits feature space and predicts from each leaf’s class distribution.
Meet the syntax
DecisionTreeClassifier(random_state=42)
plot_tree(model)
model.apply(row)DecisionTreeClassifier- Creates a tree that splits feature space and predicts from each leaf’s class distribution.
random_state=42- Makes fitted split choices reproducible.
plot_tree(model)- Displays thresholds, class counts and leaf predictions of a fitted tree.
model.apply(row)- Finds the leaf reached by a row; predict returns the class selected at that leaf.
Follow the code
Use the numbered comments to connect each Python block to the workflow above.
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]
This practice: Read and run the Python. Next: Change · Classification trees.
Classification tree · from idea to workflow
Question: Can a short sequence of conditions predict a class?
Mechanism: Each split concentrates class labels; leaves predict class evidence.
Watch for: Tiny leaves memorise observations; class imbalance can hide rare-class failures.
Interpret or debug · self-review: Inspect minority-class validation errors and depth. Repair overfitting using training-only evidence.
Independent full workflow: ML-X06. First practise complete regression and classification in F11–F12; check readiness in W-K2–W-K3.
Given data · CLASS180
180 observations. Deterministic teaching observations; values illustrate the concept rather than a real population claim. The dataframe df is supplied afresh for each Run.
| length | width | label |
|---|---|---|
| 236.033 | -0.805568 | A |
| 581.297 | 0.728558 | A |
| -1511.27 | -1.00866 | A |
| 99.0248 | -0.24496 | A |
| -13.0141 | -0.660765 | A |
| 681.179 | 0.602475 | A |
| 51.1472 | 0.873157 | A |
| 362.131 | -0.665605 | A |
Column meanings and units
Column names describe the supplied features and target. Keep the stated units and row identities when making comparisons.
Synthetic data are deliberately small and reproducible. Their patterns illustrate an idea; they are not evidence about a real population.
| Column | Stored type |
|---|---|
| length | float64 |
| width | float64 |
| label | str |
Supplied setup · available if you need to inspect it
This code runs before your editor on every Run. These are the objects your exercise uses.
from sklearn.model_selection import train_test_split
X=df[['length','width']]
y=df.label
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=.2,random_state=42,stratify=y)
Your task · Follow
Fit a tree with maximum depth 3. For the first test row, store its terminal node ID in leaf and its predicted class in label.
Hint 1 — Think
A tree follows feature thresholds to a leaf with a learned class prediction.
Hint 2 — Tools
DecisionTreeClassifier, apply and predict.
Hint 3 — 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.
Helpful prior knowledge: Macro F1 and imbalance · How a search makes a choice These links are guidance, not locks.
Sources and API context
Examples run with this Playground’s scikit-learn 1.4.2 / Pyodide 0.26.4 runtime.