Compute R-squared From Predictions

Quick Overview

Implement a NumPy function that computes the coefficient of determination from true values and model predictions. It tests whether you center the total sum of squares on the target mean, leave negative scores unclipped, return a plain float, and handle a constant target.

Compute R-squared From Predictions

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `compute_r2(y_true, y_pred)` so that it returns the coefficient of determination $$R^2 = 1 - \frac{\text{RSS}}{\text{TSS}}$$ where `RSS = sum((y_true - y_pred)**2)` is the residual sum of squares and `TSS = sum((y_true - y_true.mean())**2)` is the total sum of squares. Requirements: - Center TSS on the mean of `y_true`, never on `y_pred`. - Do not clip the result into `[0, 1]`. A bad `y_pred` must produce a negative value. - Return a scalar Python `float`, not a 0-d array. - A manual NumPy implementation and `sklearn.metrics.r2_score` are both accepted. Example: ```python y_true = np.array([3.0, -0.5, 2.0, 7.0]) y_pred = np.array([2.5, 0.0, 2.0, 8.0]) compute_r2(y_true, y_pred) # ~0.9486 # a deliberately bad y_pred gives a negative result compute_r2(np.array([1.0, 2.0, 3.0]), np.array([3.0, 2.0, 1.0])) # -3.0 ``` ```hint Check the bad example by hand Compute RSS and TSS separately for the second example before you write any code, and compare their ratio with 1. ``` ### Constraints & Assumptions - `y_true` and `y_pred` are 1-D float arrays of equal length. The test inputs have `n = 8`. - One test pairs `y_true` with predictions from an OLS fit. The other uses a deliberately bad `y_pred` whose correct answer is negative. ### What a Strong Answer Covers - RSS measured against the predictions and TSS centered on the mean of `y_true` - Why the value is left unclipped, and what a negative score says about the model - Conversion of the inputs to float arrays and of the result to a plain `float` - What happens when `y_true` is constant and TSS is 0 - The linear time cost ### Follow-up Questions - Why can $R^2$ be negative, and what does a negative value mean? - How should the function handle a constant `y_true`, where TSS is 0? - When does $R^2$ equal the squared correlation between `y_true` and `y_pred`, and why does that shortcut fail on the bad example?

Overview: Implement a NumPy function that computes the coefficient of determination from true values and model predictions. It tests whether you center the total sum of squares on the target mean, leave negative scores unclipped, return a plain float, and handle a constant target.

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

Implement compute_r2(y_true, y_pred) so that it returns the coefficient of determination

R2=1−RSSTSSR^2 = 1 - \frac{\text{RSS}}{\text{TSS}}

where RSS = sum((y_true - y_pred)**2) is the residual sum of squares and TSS = sum((y_true - y_true.mean())**2) is the total sum of squares.

Requirements:

  • Center TSS on the mean of y_true , never on y_pred .
  • Do not clip the result into [0, 1] . A bad y_pred must produce a negative value.
  • Return a scalar Python float , not a 0-d array.
  • A manual NumPy implementation and sklearn.metrics.r2_score are both accepted.

Example:

y_true = np.array([3.0, -0.5, 2.0, 7.0])
y_pred = np.array([2.5,  0.0, 2.0, 8.0])
compute_r2(y_true, y_pred)  # ~0.9486

# a deliberately bad y_pred gives a negative result
compute_r2(np.array([1.0, 2.0, 3.0]), np.array([3.0, 2.0, 1.0]))  # -3.0

Constraints & Assumptions

  • y_true and y_pred are 1-D float arrays of equal length. The test inputs have n = 8 .
  • One test pairs y_true with predictions from an OLS fit. The other uses a deliberately bad y_pred whose correct answer is negative.

What a Strong Answer Covers Guidance

  • RSS measured against the predictions and TSS centered on the mean of y_true
  • Why the value is left unclipped, and what a negative score says about the model
  • Conversion of the inputs to float arrays and of the result to a plain float
  • What happens when y_true is constant and TSS is 0
  • The linear time cost

Follow-up Questions Guidance

  • Why can R2R^2 be negative, and what does a negative value mean?
  • How should the function handle a constant y_true , where TSS is 0?
  • When does R2R^2 equal the squared correlation between y_true and y_pred , and why does that shortcut fail on the bad example?