Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Choose and Explain Models lessons
Frame and shortlist · ML-M01 · 18–25 MIN

Choose the task before the family

Build a complete model-choice mental model.

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeCompute and inspect the evidence
  3. PractiseCompute and inspect the evidence
  4. TransferCompute and inspect the evidence

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.

Build a complete model-choice mental model.QuestionPredict quantityPredict labelDiscover groupsReduce dimensions
Build a complete model-choice mental model.Scroll the diagram horizontally if needed.

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
  1. The requested result, with the values, labels and row order described by this task.
LINE24 · first 8 rows
distanceduration
15.30501
1.4782611.2361
1.9565211.8488
2.4347814.809
2.9130410.7589
3.391319.4972
3.8695717.89
4.3478315.531
Your task
  1. Create candidate estimators for a numeric target: a line under key line and a regression tree under key tree.
  2. Store their class names in a dictionary named answer with those same keys.
answer
Hints

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.

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.