Understand the idea
Begin with the question: predict a quantity, predict a label, discover groups or reduce a representation. Then consider sample size, feature types, scale, flexibility, interpretability, probabilities, assumptions and cost. Core is essential within a relevant pathway, not a requirement to finish every model.
Python skill: Use a dictionary and comprehension to organise estimators that answer the same prediction question.
Meet the syntax
candidates.items()
type(model).__name__candidates.items()- Iterates over each candidate name and estimator together.
type(model).__name__- Reads the estimator class name for the comparison inventory.
Follow the code
Use the numbered comments to connect each Python block to the workflow above.
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
candidates = {'line': LinearRegression(), 'tree': DecisionTreeRegressor(random_state=42)}
answer = {name: type(model).__name__ for name, model in candidates.items()}
This practice: Read and run the Python. Next: Change · Choose the task before the family.
Given data · LINE24
24 observations. Deterministic teaching observations; values illustrate the concept rather than a real population claim. The dataframe df is supplied afresh for each Run.
| distance | duration |
|---|---|
| 1 | 5.30501 |
| 1.47826 | 11.2361 |
| 1.95652 | 11.8488 |
| 2.43478 | 14.809 |
| 2.91304 | 10.7589 |
| 3.3913 | 19.4972 |
| 3.86957 | 17.89 |
| 4.34783 | 15.531 |
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 |
|---|---|
| distance | float64 |
| duration | float64 |
Your task · Follow
Create candidate estimators for a numeric target: a line under key line and a regression tree under key tree. Store their class names in a dictionary named answer with those same keys.
Hint 1 — Think
Use a dictionary and comprehension to organise estimators that answer the same prediction question.
Hint 2 — Tools
Use candidates.items(), type(model).__name__. Read the visible syntax meanings before editing.
Hint 3 — Approach
Create candidate estimators for a numeric target: a line under key line and a regression tree under key tree. Store their class names in a dictionary named answer with those same keys. Keep the supplied row order and inspect the named output after running.
Explained solution
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
candidates = {'line': LinearRegression(), 'tree': DecisionTreeRegressor(random_state=42)}
answer = {name: type(model).__name__ for name, model in candidates.items()}
Both candidates predict a numeric outcome. The dictionary keeps their names attached to their estimator objects. Neither has been fitted or judged. Frame the task first, then compare plausible candidates using common folds and a suitable metric.
Helpful prior knowledge: Regression checkpoint · Classification checkpoint · Neural regression checkpoint · Neural classification checkpoint · Discovery checkpoint · PCA checkpoint 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.