Skip to contents

Formula:

sum_squares_residuals <- sum((y_true - y_pred) ** 2)
sum_squares <- sum((y_true - mean(y_true)) ** 2)
R2 <- 1 - sum_squares_residuals / sum_squares

This is also called the coefficient of determination.

It indicates how close the fitted regression line is to ground-truth data.

  • The highest score possible is 1.0. It indicates that the predictors perfectly accounts for variation in the target.

  • A score of 0.0 indicates that the predictors do not account for variation in the target.

  • It can also be negative if the model is worse than random.

This metric can also compute the "Adjusted R2" score.

Usage

metric_r2_score(
  ...,
  class_aggregation = "uniform_average",
  num_regressors = 0L,
  name = "r2_score",
  dtype = NULL
)

Arguments

...

For forward/backward compatability.

class_aggregation

Specifies how to aggregate scores corresponding to different output classes (or target dimensions), i.e. different dimensions on the last axis of the predictions. Equivalent to multioutput argument in Scikit-Learn. Should be one of NULL (no aggregation), "uniform_average", "variance_weighted_average".

num_regressors

Number of independent regressors used ("Adjusted R2" score). 0 is the standard R2 score. Defaults to 0.

name

Optional. string name of the metric instance.

dtype

Optional. data type of the metric result.

Value

a Metric instance is returned. The Metric instance can be passed directly to compile(metrics = ), or used as a standalone object. See ?Metric for example usage.

Examples

y_true <- rbind(1, 4, 3)
y_pred <- rbind(2, 4, 4)
metric <- metric_r2_score()
metric$update_state(y_true, y_pred)
metric$result()

## tf.Tensor(0.57142854, shape=(), dtype=float32)