Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Regression lessons
Lines and evidence · ML-R01 · 25–40 MIN

Read a fitted line

Interpret slope and intercept in context.

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeAdapt the Python
  3. TransferCompute and inspect the evidence
  4. ApplyBuild a guided full workflow

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.
Interpret slope and intercept in context.Scroll the diagram horizontally if needed.

What the syntax does

model.coef_
  • Contains one learned slope for each feature, in feature order.

Supplied for this practice

These objects are prepared afresh before your editor runs.

X
Feature dataframe for the declared population, preserving row indices.
X = df[['distance']]
y
Target series, aligned with X.
y = df['duration']
X_train / X_test / y_train / y_test
Training feature rows from the declared split. Final-test feature rows from the declared split. Training targets, aligned with X_train. Final-test targets, aligned with X_test.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model
Already fitted on X_train, y_train. Fitted estimator or pipeline for the requested model family. Fitted on X_train, y_train; reuse without refitting.
predictions
Supplied predictions; its definition below specifies the values and role.
predictions = model.predict(X_test)

Outputs you create

slope
  1. Requested result described in the task and deliverables.
intercept
  1. Requested result described in the task and deliverables.
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. Store the fitted distance coefficient in slope and the intercept in intercept.
slopeintercept
Hints

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

Tools: coef_, intercept_ and scalar conversion.

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.

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.