TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Implement R² Score (Coefficient of Determination)

Metrics & Evaluation
Easy

Compute the coefficient of determination for one-dimensional regression targets and predictions:

R2=1−∑i(yi−y^i)2∑i(yi−yˉ)2R^2 = 1 - \frac{\sum_i (y_i - \hat{y}_i)^2}{\sum_i (y_i - \bar{y})^2}R2=1−∑i​(yi​−yˉ​)2∑i​(yi​−y^​i​)2​

Here, yiy_iyi​ is a target, y^i\hat{y}_iy^​i​ is its prediction, and yˉ\bar{y}yˉ​ is the mean target. When every target is equal, return 1.0 if every prediction matches its target and 0.0 otherwise. Return the score as a Python float.

Loading visualization...

Examples

Input: y_true = [3, 4, 5], y_pred = [2.9, 4.1, 5.0]

Output: 0.99

Explanation: The residual sum of squares is 0.02 and the total sum of squares is 2, so the score is 1 - 0.02 / 2.

Input: y_true = [1, 1, 1], y_pred = [1, 1, 1]

Output: 1.0

Input: y_true = [1, 1, 1], y_pred = [0, 2, 1]

Output: 0.0

Hint 1

Use np.sum((y_true - y_pred) ** 2) for the residual sum of squares.

Hint 2

Check whether the total sum of squares is zero before applying the fraction.

Requirements

  • Compute the residual and total sums of squares with NumPy
  • Apply the stated convention when all targets are equal
  • Return a Python float

Constraints

  • y_true and y_pred are equal-length, nonempty one-dimensional numeric lists
  • Each list contains at most 10610^6106 values
  • Use NumPy only
Try Similar Problems
Mean Squared ErrorClassification MetricsLog Loss Per SampleHuber LossConfusion Matrix Norm

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

You must run your code first.
PrevNext

Implement R² Score (Coefficient of Determination)

Metrics & Evaluation
Easy

Compute the coefficient of determination for one-dimensional regression targets and predictions:

R2=1−∑i(yi−y^i)2∑i(yi−yˉ)2R^2 = 1 - \frac{\sum_i (y_i - \hat{y}_i)^2}{\sum_i (y_i - \bar{y})^2}R2=1−∑i​(yi​−yˉ​)2∑i​(yi​−y^​i​)2​

Here, yiy_iyi​ is a target, y^i\hat{y}_iy^​i​ is its prediction, and yˉ\bar{y}yˉ​ is the mean target. When every target is equal, return 1.0 if every prediction matches its target and 0.0 otherwise. Return the score as a Python float.

Loading visualization...

Examples

Input: y_true = [3, 4, 5], y_pred = [2.9, 4.1, 5.0]

Output: 0.99

Explanation: The residual sum of squares is 0.02 and the total sum of squares is 2, so the score is 1 - 0.02 / 2.

Input: y_true = [1, 1, 1], y_pred = [1, 1, 1]

Output: 1.0

Input: y_true = [1, 1, 1], y_pred = [0, 2, 1]

Output: 0.0

Hint 1

Use np.sum((y_true - y_pred) ** 2) for the residual sum of squares.

Hint 2

Check whether the total sum of squares is zero before applying the fraction.

Requirements

  • Compute the residual and total sums of squares with NumPy
  • Apply the stated convention when all targets are equal
  • Return a Python float

Constraints

  • y_true and y_pred are equal-length, nonempty one-dimensional numeric lists
  • Each list contains at most 10610^6106 values
  • Use NumPy only
Try Similar Problems
Mean Squared ErrorClassification MetricsLog Loss Per SampleHuber LossConfusion Matrix Norm

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

You must run your code first.