Understand the idea
RMSE measures errors in target units. R² compares squared error with variation around the evaluation-set mean. It can be negative. A trained mean dummy uses the training mean, so it is a distinct reference.
What the syntax does
r2_score- Compares squared prediction error with variation around the evaluation-target mean; the result can be negative.
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
rmse- Requested result described in the task and deliverables.
r2- 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 |
- Compute RMSE and R² for the supplied predictions; store them in
rmseandr2.
rmser2Hints
Think: One measure reports error in outcome units; the other compares squared error with a reference.
Tools: root_mean_squared_error and r2_score.
Approach: Apply both metrics to the same aligned outcomes and predictions, then store each named result.
Explained solution
from sklearn.metrics import root_mean_squared_error,r2_score
rmse=root_mean_squared_error(y_test,predictions)
r2=r2_score(y_test,predictions)
Using identical rows makes the two summaries complementary descriptions of the same prediction evidence.