Implementation:Rapidsai Cuml Regression Metrics
| Knowledge Sources | |
|---|---|
| Domains | Machine_Learning, Regression, Model_Evaluation |
| Last Updated | 2026-02-08 12:00 GMT |
Overview
Provides GPU-accelerated regression evaluation metrics including R-squared, mean squared error, mean absolute error, median absolute error, and mean squared log error.
Description
The regression.py module implements standard regression scoring functions on the GPU using CuPy. It contains five public functions -- r2_score, mean_squared_error, mean_absolute_error, median_absolute_error, and mean_squared_log_error -- each of which measures how well predicted values match ground-truth target values. All functions share a common internal normalization helper (_normalize_regression_metric_args) that validates and reshapes inputs, coerces arrays to CuPy, and handles the multioutput parameter for multi-output regression. The module also provides an internal weighted-median helper for the median absolute error metric with sample weights.
Each metric supports optional sample_weight arrays and a multioutput parameter that controls aggregation across multiple output columns. The multioutput parameter accepts "raw_values" (no aggregation), "uniform_average" (equal weighting), "variance_weighted" (for R-squared), or a custom weight array.
Usage
Use these functions to evaluate regression model performance on GPU data. They are drop-in replacements for their scikit-learn counterparts, accepting device arrays (CuPy, Numba device arrays, cuDF) and host arrays (NumPy) alike. Typical usage is after model training and prediction to compute a quality score.
Code Reference
Source Location
- Repository: Rapidsai_Cuml
- File:
python/cuml/cuml/metrics/regression.py
Signature
def r2_score(
y_true,
y_pred,
*,
sample_weight=None,
multioutput="uniform_average",
force_finite=True,
)
def mean_squared_error(
y_true,
y_pred,
sample_weight=None,
multioutput="uniform_average",
squared=True,
)
def mean_absolute_error(
y_true, y_pred, sample_weight=None, multioutput="uniform_average"
)
def median_absolute_error(
y_true,
y_pred,
*,
sample_weight=None,
multioutput="uniform_average",
)
def mean_squared_log_error(
y_true,
y_pred,
sample_weight=None,
multioutput="uniform_average",
squared=True,
)
Import
from cuml.metrics import r2_score
from cuml.metrics import mean_squared_error
from cuml.metrics import mean_absolute_error
from cuml.metrics import median_absolute_error
from cuml.metrics import mean_squared_log_error
I/O Contract
Inputs (common to all functions)
| Name | Type | Required | Description |
|---|---|---|---|
| y_true | array-like of shape (n_samples,) or (n_samples, n_outputs) | Yes | Ground truth target values. |
| y_pred | array-like of shape (n_samples,) or (n_samples, n_outputs) | Yes | Estimated / predicted target values. |
| sample_weight | array-like of shape (n_samples,) | No | Per-sample weights. Defaults to uniform weighting when None.
|
| multioutput | str or array-like of shape (n_outputs,) | No | Aggregation strategy for multi-output: "raw_values", "uniform_average", "variance_weighted" (r2 only), or a custom weight array. Default is "uniform_average".
|
| force_finite | bool | No | (r2_score only) Replace NaN / -Inf with 1.0 or 0.0. Default True.
|
| squared | bool | No | (MSE / MSLE only) If True return MSE; if False return RMSE. Default True.
|
Outputs
| Name | Type | Description |
|---|---|---|
| score / loss | float or ndarray of floats | A scalar score when aggregation is applied, or an array of per-output scores when multioutput="raw_values".
|
Usage Examples
import cupy as cp
from cuml.metrics import r2_score, mean_squared_error, mean_absolute_error
y_true = cp.array([3.0, -0.5, 2.0, 7.0])
y_pred = cp.array([2.5, 0.0, 2.0, 8.0])
# R-squared score
r2 = r2_score(y_true, y_pred)
print("R2:", r2) # close to 0.948...
# Mean Squared Error
mse = mean_squared_error(y_true, y_pred)
print("MSE:", mse) # 0.375
# Root Mean Squared Error
rmse = mean_squared_error(y_true, y_pred, squared=False)
print("RMSE:", rmse)
# Mean Absolute Error
mae = mean_absolute_error(y_true, y_pred)
print("MAE:", mae) # 0.5