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.
What the syntax does
candidates.items()- Iterates over each candidate name and estimator together.
type(model).__name__- Reads the estimator class name for the comparison inventory.
Outputs you create
answer- The requested result, with the values, labels and row order described by this task.
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 |
- Create candidate estimators for a numeric target: a line under key
lineand a regression tree under keytree. - Store their class names in a dictionary named
answerwith those same keys.
answerHints
Think: Use a dictionary and comprehension to organise estimators that answer the same prediction question.
Tools: Use candidates.items(), type(model).__name__. Read the visible syntax meanings before editing.
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.