Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← ML Foundations lessonsQUESTIONS · MODELS · EVIDENCE
Learning from examples · ML-F03 · 12–18 MIN

What fitting does

Recognise learned state from examples.

Exercises within this concept

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

Understand the idea

fit estimates model parameters from supplied examples. A fitted line has learned an intercept and coefficient; creating an estimator alone has not learned anything.

Recognise learned state from examples.Observations X, yUnfitted estimatorLinearRegression()No learned line yetFitted estimatorLearned linefit(X, y) learns the line from these observations.
Schematic · Recognise learned state from examples.Scroll the diagram horizontally if needed.

Python skill: Creates an unfitted linear regression estimator.

Meet the syntax

model = LinearRegression()
model.fit(X, y)
model.coef_
LinearRegression()
Creates an unfitted linear regression estimator.
model.fit(X, y)
Learns coefficients and an intercept from aligned feature rows and targets.
model.coef_
Reads the coefficients learned during fitting; the trailing underscore marks a fitted attribute.

Follow the code

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

from sklearn.linear_model import LinearRegression
model=LinearRegression().fit(df[['distance']],df.duration)

This practice: Read and run the Python. Next: Change · What fitting does.

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

Fit a LinearRegression model using distance to predict duration.

Hint 1 — Think

Creating an estimator and learning its parameters are separate steps.

Hint 2 — Tools

LinearRegression and fit with aligned X/y.

Hint 3 — Approach

Import the estimator, create it, and fit the distance table to duration values.

Explained solution
from sklearn.linear_model import LinearRegression
model=LinearRegression().fit(df[['distance']],df.duration)

The constructor only specifies the estimator; fit learns the line from this population and preserves it in model.

Helpful prior knowledge: Features and target 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

Fit a LinearRegression model using distance to predict duration.

model

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.