Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← ML Foundations lessonsQUESTIONS · MODELS · EVIDENCE
Questions and tables · ML-F02 · 12–18 MIN

Features and target

Construct X and y without leaking outcomes into inputs.

Exercises within this concept

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeAdapt the Python
  3. TransferAdapt the Python

Understand the idea

Rows are observations. X is a two-dimensional feature table; y is the target for the same observations. Select columns by meaning, not simply by numeric dtype.

Construct X and y without leaking outcomes into inputs.ObservationFeature X₁Feature X₂Target yrow 12standardknownrow 25expressknownrow 38economyunknownKeep row identities aligned; new rows provide X.
Schematic · Construct X and y without leaking outcomes into inputs.Scroll the diagram horizontally if needed.

Python skill: Selects a list of feature columns and keeps a two-dimensional dataframe.

Meet the syntax

X = df[['distance']]
y = df['duration']
df[['distance']]
Selects a list of feature columns and keeps a two-dimensional dataframe.
df['duration']
Selects one aligned target column as a series.
X
Names the feature table.
y
Names the target values aligned to X.

Follow the code

Use the numbered comments to connect each Python block to the workflow above.

X=df[['distance']]
y=df['duration']

This practice: Read and run the Python. Next: Change · Features and target.

Given data · LINE12

12 observations. Deterministic teaching observations; values illustrate the concept rather than a real population claim. The dataframe df is supplied afresh for each Run.

LINE12 · all rows
distanceduration
18
211
213
314
420
522
524
625
731
833
935
1041

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.

Input schema
ColumnStored type
distanceint64
durationint64

Your task · Follow

Create X with distance and y with duration.

Hint 1 — Think

The estimator needs inputs and outcomes in separate objects without changing their row order.

Hint 2 — Tools

Dataframe column selection; X as a table and y as a series.

Hint 3 — Approach

Select the distance column as a feature table and the duration column as its aligned target.

Explained solution
X=df[['distance']]
y=df['duration']

Keeping X two-dimensional preserves the estimator interface even with one feature; the separate y supplies the outcomes to learn.

Helpful prior knowledge: What question are we answering? 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.

Your task · Follow

Create X with distance and y with duration.

Xy

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.