TensorTonic
Problems
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
Binary Log Loss per Sample in PythonEasyCompute Accuracy, Precision, Recall, F1MediumCompute Confusion Matrix with NormalizationHardMean Squared Error (MSE)EasyImplement Huber LossEasyImplement Precision and Recall at K in PythonEasyCompute AUC (Area Under ROC)EasyImplement Micro-F1EasyCalculate P50, P95, and P99 Inference LatencyEasyTop-K Classification AccuracyEasyMean IoU for SegmentationEasyRegression MetricsEasy
Scratchpad

Accepts: array

Accepts: array

You must run your code first.
Implement R² Score (Coefficient of Determination)

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
Binary Log Loss per Sample in PythonEasyCompute Accuracy, Precision, Recall, F1MediumCompute Confusion Matrix with NormalizationHardMean Squared Error (MSE)EasyImplement Huber LossEasyImplement Precision and Recall at K in PythonEasyCompute AUC (Area Under ROC)EasyImplement Micro-F1EasyCalculate P50, P95, and P99 Inference LatencyEasyTop-K Classification AccuracyEasyMean IoU for SegmentationEasyRegression MetricsEasy
Scratchpad

Accepts: array

Accepts: array

You must run your code first.
Problems
Loading...
1 / 1