Understand the idea
A classifier returns labels. The supplied tree recipe is only an example of the familiar fit/predict interface here; tree mechanics and depth choices come later.
Python skill: Learns the supplied classification recipe from feature rows and class labels.
Meet the syntax
classifier.fit(X, y)
classifier.predict(new_rows)classifier.fit(X, y)- Learns the supplied classification recipe from feature rows and class labels.
classifier.predict(new_rows)- Returns one predicted class label for each new feature row.
Follow the code
Use the numbered comments to connect each Python block to the workflow above.
from sklearn.tree import DecisionTreeClassifier
classifier=DecisionTreeClassifier(max_depth=2,random_state=42)
classifier.fit(df[['length','width']],df.label)
answer=classifier.predict(df[['length','width']])
This practice: Read and run the Python. Next: Change · Predicting classes.
Given data · CLASS18
18 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 |
| 2051.15 | 1.87316 | B |
| 2362.13 | 0.334395 | B |
| 2285.63 | 0.257253 | B |
| 2680.44 | 0.961328 | B |
| 1856.81 | 0.472554 | B |
| 2946.98 | 0.880302 | B |
| -331.781 | 2.72724 | C |
| 412.325 | 3.28307 | C |
| 319.701 | 3.33371 | C |
| 1658.91 | 2.68519 | C |
| -396.782 | 2.36965 | C |
| 477.136 | 3.8745 | C |
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 |
Your task · Follow
Use the supplied classifier recipe to predict these observations’ class labels.
Hint 1 — Think
The output is a class label, even though the recipe uses numeric measurements.
Hint 2 — Tools
The supplied DecisionTreeClassifier recipe, fit and predict.
Hint 3 — Approach
Use the given classifier settings with the two measurement columns and label target, then obtain one label per row.
Explained solution
from sklearn.tree import DecisionTreeClassifier
classifier=DecisionTreeClassifier(max_depth=2,random_state=42)
classifier.fit(df[['length','width']],df.label)
answer=classifier.predict(df[['length','width']])
This exercise isolates the classifier interface; the supplied tree recipe is not a request to choose or tune a tree.
Helpful prior knowledge: Making a reproducible split 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.