sklearn.metrics.d2_pinball_score(y_true, y_pred, *, sample_weight=None, alpha=0.5, multioutput='uniform_average')[source]

\(D^2\) regression score function, fraction of pinball loss explained.

Best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A model that always uses the empirical alpha-quantile of y_true as constant prediction, disregarding the input features, gets a \(D^2\) score of 0.0.

Read more in the User Guide.

New in version 1.1.

y_truearray-like of shape (n_samples,) or (n_samples, n_outputs)

Ground truth (correct) target values.

y_predarray-like of shape (n_samples,) or (n_samples, n_outputs)

Estimated target values.

sample_weightarray-like of shape (n_samples,), optional

Sample weights.

alphafloat, default=0.5

Slope of the pinball deviance. It determines the quantile level alpha for which the pinball deviance and also D2 are optimal. The default alpha=0.5 is equivalent to d2_absolute_error_score.

multioutput{‘raw_values’, ‘uniform_average’} or array-like of shape (n_outputs,), default=’uniform_average’

Defines aggregating of multiple output values. Array-like value defines weights used to average scores.

‘raw_values’ :

Returns a full set of errors in case of multioutput input.

‘uniform_average’ :

Scores of all outputs are averaged with uniform weight.

scorefloat or ndarray of floats

The \(D^2\) score with a pinball deviance or ndarray of scores if multioutput='raw_values'.


Like \(R^2\), \(D^2\) score may be negative (it need not actually be the square of a quantity D).

This metric is not well-defined for a single point and will return a NaN value if n_samples is less than two.



Eq. (7) of Koenker, Roger; Machado, José A. F. (1999). “Goodness of Fit and Related Inference Processes for Quantile Regression”


Eq. (3.11) of Hastie, Trevor J., Robert Tibshirani and Martin J. Wainwright. “Statistical Learning with Sparsity: The Lasso and Generalizations.” (2015).


>>> from sklearn.metrics import d2_pinball_score
>>> y_true = [1, 2, 3]
>>> y_pred = [1, 3, 3]
>>> d2_pinball_score(y_true, y_pred)
>>> d2_pinball_score(y_true, y_pred, alpha=0.9)
>>> d2_pinball_score(y_true, y_pred, alpha=0.1)
>>> d2_pinball_score(y_true, y_true, alpha=0.1)