Compute In-Sample and Out-of-Sample R² for an OLS Fit

Quick Overview

Implement a Python function that fits ordinary least squares on a training split and reports R² on both the training and test data. It tests whether you avoid refitting on test data, choose the right baseline mean for each split, and understand why test R² can be negative.

Compute In-Sample and Out-of-Sample R² for an OLS Fit

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `train_test_r2(X_train, y_train, X_test, y_test)` that fits OLS with an intercept on the training data only, then evaluates that single model on both splits. Return `(r2_train, r2_test)`: the R² of predictions on `X_train` against `y_train`, and on `X_test` against `y_test`. Requirements: - Never re-fit on the test data. - R² is `1 - RSS / TSS`. - For `r2_test`, center TSS on `y_test.mean()`, not the training mean. - Return two floats in the order `(r2_train, r2_test)`. `r2_test` can be negative. ```hint Two baselines Each R² compares the model with a constant prediction. Work out which constant each split is compared against, and keep the coefficients from the training fit for both. ``` ### What a Strong Answer Covers - Fitting one OLS model with an intercept on the training split and reusing its coefficients for both sets of predictions. - Computing each R² against its own split's mean, and why the test baseline is `y_test.mean()`. - Why `r2_test` can be negative while in-sample OLS R² with an intercept cannot. - Returning plain floats in the order `(r2_train, r2_test)`, and what happens when the test targets have zero variance. ### Follow-up Questions - Why can `r2_test` be negative? - Would centering the test TSS on the training mean change the metric? - How would you get a more stable estimate of out-of-sample R² than a single train/test split gives?

Overview: Implement a Python function that fits ordinary least squares on a training split and reports R² on both the training and test data. It tests whether you avoid refitting on test data, choose the right baseline mean for each split, and understand why test R² can be negative.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement train_test_r2(X_train, y_train, X_test, y_test) that fits OLS with an intercept on the training data only, then evaluates that single model on both splits. Return (r2_train, r2_test): the R² of predictions on X_train against y_train, and on X_test against y_test.

Requirements:

  • Never re-fit on the test data.
  • R² is 1 - RSS / TSS .
  • For r2_test , center TSS on y_test.mean() , not the training mean.
  • Return two floats in the order (r2_train, r2_test) . r2_test can be negative.

What a Strong Answer Covers Guidance

  • Fitting one OLS model with an intercept on the training split and reusing its coefficients for both sets of predictions.
  • Computing each R² against its own split's mean, and why the test baseline is y_test.mean() .
  • Why r2_test can be negative while in-sample OLS R² with an intercept cannot.
  • Returning plain floats in the order (r2_train, r2_test) , and what happens when the test targets have zero variance.

Follow-up Questions Guidance

  • Why can r2_test be negative?
  • Would centering the test TSS on the training mean change the metric?
  • How would you get a more stable estimate of out-of-sample R² than a single train/test split gives?