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.
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.
| distance | duration |
|---|---|
| 1 | 8 |
| 2 | 11 |
| 2 | 13 |
| 3 | 14 |
| 4 | 20 |
| 5 | 22 |
| 5 | 24 |
| 6 | 25 |
| 7 | 31 |
| 8 | 33 |
| 9 | 35 |
| 10 | 41 |
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 | int64 |
| duration | int64 |
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.