Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Regression lessonsQUESTIONS · MODELS · EVIDENCE
Lines and evidence · ML-R01 · 12–18 MIN

Read a fitted line

Interpret slope and intercept in context.

Exercises within this concept

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

Understand the idea

A coefficient describes the predicted target change for one unit of its input. The intercept is the predicted value at input zero, which may be outside the useful data range.

Interpret slope and intercept in context.actual −predictedResiduals measure signed vertical differences.
Schematic · Interpret slope and intercept in context.Scroll the diagram horizontally if needed.

Python skill: Contains one learned slope for each feature, in feature order.

Meet the syntax

model.coef_
model.intercept_
model.coef_
Contains one learned slope for each feature, in feature order.
model.intercept_
Contains the fitted prediction when all numeric inputs are zero, which may lie outside the observed range.

Follow the code

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

slope=float(model.coef_[0])
intercept=float(model.intercept_)

This practice: Read and run the Python. Next: Change · Read a fitted line.

Simple linear regression · from idea to workflow

Question: How does one available measurement help predict a quantity?

Mechanism: A slope and intercept minimise squared training residuals.

Watch for: Curvature, unusual points and extrapolation can make a straight line misleading.

Interpret or debug · self-review: Read the residuals: does a curve remain? Explain why a small training error does not establish performance beyond the observed input range.

Independent full workflow: ML-X01 · ML-X02. First practise complete regression and classification in F11–F12; check readiness in W-K2–W-K3.

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.

LINE24 · first 8 prepared 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

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
distancefloat64
durationfloat64
Supplied setup · available if you need to inspect it

This code runs before your editor on every Run. These are the objects your exercise uses.

from sklearn.model_selection import train_test_split
X = df[['distance']]
y = df['duration']
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=.2,random_state=42)
from sklearn.linear_model import LinearRegression
model = LinearRegression().fit(X_train,y_train)
predictions = model.predict(X_test)

Your task · Follow

Store the fitted distance coefficient in slope and the intercept in intercept.

Hint 1 — Think

A fitted line stores its learned slope separately from its offset.

Hint 2 — Tools

coef_, intercept_ and scalar conversion.

Hint 3 — Approach

Read the single distance coefficient and intercept from the supplied fitted model.

Explained solution
slope=float(model.coef_[0])
intercept=float(model.intercept_)

The named values distinguish change per distance unit from the predicted value at zero distance.

Helpful prior knowledge: Supervised Workflow checkpoint · Readiness · unfamiliar regression 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

Store the fitted distance coefficient in slope and the intercept in intercept.

slopeintercept

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.