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.
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 withX_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 onX_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- Requested result described in the task and deliverables.
intercept- Requested result described in the task and deliverables.
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 |
- Store the fitted
distancecoefficient inslopeand theinterceptinintercept.
slopeinterceptHints
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.