bigframes.ml.metrics.r2_score#

bigframes.ml.metrics.r2_score(y_true: DataFrame | Series, y_pred: DataFrame | Series, *, force_finite=True) float[source]#

\(R^2\) (coefficient of determination) regression score function.

Best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). In the general case when the true y is non-constant, a constant model that always predicts the average y disregarding the input features would get a \(R^2\) score of 0.0.

In the particular case when y_true is constant, the \(R^2\) score is not finite: it is either NaN (perfect predictions) or -Inf (imperfect predictions). To prevent such non-finite numbers to pollute higher-level experiments such as a grid search cross-validation, by default these cases are replaced with 1.0 (perfect predictions) or 0.0 (imperfect predictions) respectively.

Examples:

>>> import bigframes.pandas as bpd
>>> import bigframes.ml.metrics
>>> y_true = bpd.DataFrame([3, -0.5, 2, 7])
>>> y_pred = bpd.DataFrame([2.5, 0.0, 2, 8])
>>> r2_score = bigframes.ml.metrics.r2_score(y_true, y_pred)
>>> r2_score
np.float64(0.9486081370449679)
Parameters:
  • y_true (Series or DataFrame of shape (n_samples,)) – Ground truth (correct) target values.

  • y_pred (Series or DataFrame of shape (n_samples,)) – Estimated target values.

Returns:

The \(R^2\) score.

Return type:

float