Compute the In-Sample R² of an OLS Linear Regression Fit

Quick Overview

Write a Python function that fits ordinary least squares with an intercept on a small dataset and returns the in-sample R², matching the scikit-learn score. It tests how R² is defined, which baseline the total sum of squares measures, and what an in-sample fit statistic can and cannot tell you.

Compute the In-Sample R² of an OLS Linear Regression Fit

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `in_sample_r2(X, y)` that fits OLS with an intercept on the full `X` (shape `(20, 3)`) and `y` (shape `(20,)`), predicts on the same `X`, and returns the in-sample R² as a single `float`. The value must match `LinearRegression().fit(X, y).score(X, y)`. Requirements: - R² is `1 - SS_res / SS_tot`, where `SS_tot` uses the mean of `y`. - No train/test split: fit and score on the full data. `numpy` is available. scikit-learn, statsmodels (OLS on `add_constant(X)`), or the normal equation are all accepted. ```hint Name the baseline Both sums of squares are computed on the training rows. Ask which simple prediction `SS_tot` measures the error of, and what your model must contain to always do at least as well as it. ``` ### Constraints & Assumptions - `X` has 20 rows and 3 predictor columns; `y` has 20 values. - All 20 rows are used for both fitting and scoring. - The return value is one Python `float` that agrees with scikit-learn's `score` on the same data. ### What a Strong Answer Covers - Fitting OLS with an intercept on all 20 rows, by any of the accepted routes. - Predicting on the same `X` and computing `SS_res` and `SS_tot`, with the mean of `y` as the baseline. - Why the accepted routes all produce the same number as `LinearRegression().score`. - What in-sample R² does not tell you, and the constant-`y` edge case. ### Follow-up Questions - Why can in-sample R² never decrease when you add a feature? - What happens to the value if you fit without an intercept but keep the same formula? - When can R² be negative, and why can that not happen in this setup?

Overview: Write a Python function that fits ordinary least squares with an intercept on a small dataset and returns the in-sample R², matching the scikit-learn score. It tests how R² is defined, which baseline the total sum of squares measures, and what an in-sample fit statistic can and cannot tell you.

Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement in_sample_r2(X, y) that fits OLS with an intercept on the full X (shape (20, 3)) and y (shape (20,)), predicts on the same X, and returns the in-sample R² as a single float. The value must match LinearRegression().fit(X, y).score(X, y).

Requirements:

  • R² is 1 - SS_res / SS_tot , where SS_tot uses the mean of y .
  • No train/test split: fit and score on the full data.

numpy is available. scikit-learn, statsmodels (OLS on add_constant(X)), or the normal equation are all accepted.

Constraints & Assumptions

  • X has 20 rows and 3 predictor columns; y has 20 values.
  • All 20 rows are used for both fitting and scoring.
  • The return value is one Python float that agrees with scikit-learn's score on the same data.

What a Strong Answer Covers Guidance

  • Fitting OLS with an intercept on all 20 rows, by any of the accepted routes.
  • Predicting on the same X and computing SS_res and SS_tot , with the mean of y as the baseline.
  • Why the accepted routes all produce the same number as LinearRegression().score .
  • What in-sample R² does not tell you, and the constant- y edge case.

Follow-up Questions Guidance

  • Why can in-sample R² never decrease when you add a feature?
  • What happens to the value if you fit without an intercept but keep the same formula?
  • When can R² be negative, and why can that not happen in this setup?