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.
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.
| distance | duration |
|---|---|
| 1 | 5.30501 |
| 1.47826 | 11.2361 |
| 1.95652 | 11.8488 |
| 2.43478 | 14.809 |
| 2.91304 | 10.7589 |
| 3.3913 | 19.4972 |
| 3.86957 | 17.89 |
| 4.34783 | 15.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.
| Column | Stored type |
|---|---|
| distance | float64 |
| duration | float64 |
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.