Compute the d2_absolute_error_score metric — provided by scikit-learn. Use when the user has predictions and ground-truth and needs to compute d2_absolute_error_score, or asks how to score with d2_absolute_error_score.
Installs into .claude/skills of the current project.
Are you the author of D2 Absolute Error Score?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/qhjqhj00-d2-absolute-error-score)
---
name: d2-absolute-error-score
description: Compute the d2_absolute_error_score metric — provided by scikit-learn. Use when the user has predictions and ground-truth and needs to compute d2_absolute_error_score, or asks how to score with d2_absolute_error_score.
metadata:
skill_kind: metric
source_lib: scikit-learn
import_path: sklearn.metrics.d2_absolute_error_score
source: library_introspection
---
# d2-absolute-error-score
> Metric `d2_absolute_error_score` from `scikit-learn` (sklearn.metrics.d2_absolute_error_score)
## When to invoke this skill
The user has predictions + ground truth and asks to evaluate with d2_absolute_error_score, or
mentions `sklearn.metrics.d2_absolute_error_score` directly, or wants the standard scikit-learn implementation.
## Reference signature
```python
from sklearn.metrics import d2_absolute_error_score
# d2_absolute_error_score(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average')
```
## Library docstring
```
:math:`D^2` regression score function, fraction of absolute error 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 median of `y_true`
as constant prediction, disregarding the input features,
gets a :math:`D^2` score of 0.0.
Read more in the :ref:`User Guide <d2_score>`.
.. versionadded:: 1.1
Parameters
----------
y_true : array-like of shape (n_samples,) or (n_samples, n_outputs)
Ground truth (correct) target values.
y_pred : array-like of shape (n_samples,) or (n_samples, n_outputs)
Estimated target values.
sample_weight : array-like of shape (n_samples,), default=None
Sample weights.
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.
Returns
-------
score : float or ndarray of floats
The :math:`D^2` score with an absolute error deviance
or ndarray of scores if 'multioutput' is 'raw_values'.
Notes
-----
Like :math:`R^2`, :math:`D^2` score may be negative
(it need not actually be the square of a quantity D).
This metric is not well-defined for single samples and will return a NaN
value if n_samples is less than two.
References
----------
.. [1] Eq. (3.11) of Hastie, Trevor J., Robert Tibshirani and Martin J.
Wainwright. "Statistical Learning with Sparsity: The Lasso and
Generalizations." (2015). https://hastie.su.domains/StatLearnSparsity/
Examples
--------
>>> from sklearn.metrics import d2_absolute_error_score
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> d2_absolute_error_score(y_true, y_pred)
0.764...
>>> y_true = [[0.5, 1], [-1, 1], [7, -6]]
>>> y_pred = [[0, 2], [-1, 2], [8, -5]]
>>> d2_absolute_error_score(y_true, y_pred, multioutput='uniform_average')
0.691...
>>> d2_absolute_error_score(y_true, y_pred, multioutput='raw_values')
array([0.8125
```
## Quick recipe
```python
import sklearn.metrics as _m
score = _m.d2_absolute_error_score(y_true, y_pred)
```
## Don'ts
- Don't reimplement when the library version handles edge cases (NaN, ties, empty inputs) better than a hand-rolled formula.
- Always check the library version's argument order — sklearn is `(y_true, y_pred)` while torchmetrics is `(preds, target)`.