@@ -101,6 +101,9 @@ Scoring Function
101
101
'neg_mean_poisson_deviance' :func: `metrics.mean_poisson_deviance `
102
102
'neg_mean_gamma_deviance' :func: `metrics.mean_gamma_deviance `
103
103
'neg_mean_absolute_percentage_error' :func: `metrics.mean_absolute_percentage_error `
104
+ 'd2_absolute_error_score' :func: `metrics.d2_absolute_error_score `
105
+ 'd2_pinball_score' :func: `metrics.d2_pinball_score `
106
+ 'd2_tweedie_score' :func: `metrics.d2_tweedie_score `
104
107
==================================== ============================================== ==================================
105
108
106
109
@@ -1969,7 +1972,8 @@ The :mod:`sklearn.metrics` module implements several loss, score, and utility
1969
1972
functions to measure regression performance. Some of those have been enhanced
1970
1973
to handle the multioutput case: :func: `mean_squared_error `,
1971
1974
:func: `mean_absolute_error `, :func: `r2_score `,
1972
- :func: `explained_variance_score ` and :func: `mean_pinball_loss `.
1975
+ :func: `explained_variance_score `, :func: `mean_pinball_loss `, :func: `d2_pinball_score `
1976
+ and :func: `d2_absolute_error_score `.
1973
1977
1974
1978
1975
1979
These functions have an ``multioutput `` keyword argument which specifies the
@@ -2371,8 +2375,8 @@ is defined as
2371
2375
\sum _{i=0 }^{n_\text {samples} - 1 }
2372
2376
\begin {cases}
2373
2377
(y_i-\hat {y}_i)^2 , & \text {for }p=0 \text { (Normal)}\\
2374
- 2 (y_i \log (y/\hat {y}_i) + \hat {y}_i - y_i), & \text {for}p=1 \text { (Poisson)}\\
2375
- 2 (\log (\hat {y}_i/y_i) + y_i/\hat {y}_i - 1 ), & \text {for}p=2 \text { (Gamma)}\\
2378
+ 2 (y_i \log (y/\hat {y}_i) + \hat {y}_i - y_i), & \text {for }p=1 \text { (Poisson)}\\
2379
+ 2 (\log (\hat {y}_i/y_i) + y_i/\hat {y}_i - 1 ), & \text {for }p=2 \text { (Gamma)}\\
2376
2380
2 \left (\frac {\max (y_i,0 )^{2 -p}}{(1 -p)(2 -p)}-
2377
2381
\frac {y\,\hat {y}^{1 -p}_i}{1 -p}+\frac {\hat {y}^{2 -p}_i}{2 -p}\right ),
2378
2382
& \text {otherwise}
@@ -2415,34 +2419,6 @@ the difference in errors decreases. Finally, by setting, ``power=2``::
2415
2419
we would get identical errors. The deviance when ``power=2 `` is thus only
2416
2420
sensitive to relative errors.
2417
2421
2418
- .. _d2_tweedie_score :
2419
-
2420
- D² score, the coefficient of determination
2421
- -------------------------------------------
2422
-
2423
- The :func: `d2_tweedie_score ` function computes the percentage of deviance
2424
- explained. It is a generalization of R², where the squared error is replaced by
2425
- the Tweedie deviance. D², also known as McFadden's likelihood ratio index, is
2426
- calculated as
2427
-
2428
- .. math ::
2429
-
2430
- D^2 (y, \hat {y}) = 1 - \frac {\text {D}(y, \hat {y})}{\text {D}(y, \bar {y})} \,.
2431
-
2432
- The argument ``power `` defines the Tweedie power as for
2433
- :func: `mean_tweedie_deviance `. Note that for `power=0 `,
2434
- :func: `d2_tweedie_score ` equals :func: `r2_score ` (for single targets).
2435
-
2436
- Like R², the best possible score is 1.0 and it can be negative (because the
2437
- model can be arbitrarily worse). A constant model that always predicts the
2438
- expected value of y, disregarding the input features, would get a D² score
2439
- of 0.0.
2440
-
2441
- A scorer object with a specific choice of ``power `` can be built by::
2442
-
2443
- >>> from sklearn.metrics import d2_tweedie_score, make_scorer
2444
- >>> d2_tweedie_score_15 = make_scorer(d2_tweedie_score, power=1.5)
2445
-
2446
2422
.. _pinball_loss :
2447
2423
2448
2424
Pinball loss
@@ -2507,6 +2483,93 @@ explained in the example linked below.
2507
2483
hyper-parameters of quantile regression models on data with non-symmetric
2508
2484
noise and outliers.
2509
2485
2486
+ .. _d2_score :
2487
+
2488
+ D² score
2489
+ --------
2490
+
2491
+ The D² score computes the fraction of deviance explained.
2492
+ It is a generalization of R², where the squared error is generalized and replaced
2493
+ by a deviance of choice :math: `\text {dev}(y, \hat {y})`
2494
+ (e.g., Tweedie, pinball or mean absolute error). D² is a form of a *skill score *.
2495
+ It is calculated as
2496
+
2497
+ .. math ::
2498
+
2499
+ D^2 (y, \hat {y}) = 1 - \frac {\text {dev}(y, \hat {y})}{\text {dev}(y, y_{\text {null}})} \,.
2500
+
2501
+ Where :math: `y_{\text {null}}` is the optimal prediction of an intercept-only model
2502
+ (e.g., the mean of `y_true ` for the Tweedie case, the median for absolute
2503
+ error and the alpha-quantile for pinball loss).
2504
+
2505
+ Like R², the best possible score is 1.0 and it can be negative (because the
2506
+ model can be arbitrarily worse). A constant model that always predicts
2507
+ :math: `y_{\text {null}}`, disregarding the input features, would get a D² score
2508
+ of 0.0.
2509
+
2510
+ D² Tweedie score
2511
+ ^^^^^^^^^^^^^^^^
2512
+
2513
+ The :func: `d2_tweedie_score ` function implements the special case of D²
2514
+ where :math: `\text {dev}(y, \hat {y})` is the Tweedie deviance, see :ref: `mean_tweedie_deviance `.
2515
+ It is also known as D² Tweedie and is related to McFadden's likelihood ratio index.
2516
+
2517
+ The argument ``power `` defines the Tweedie power as for
2518
+ :func: `mean_tweedie_deviance `. Note that for `power=0 `,
2519
+ :func: `d2_tweedie_score ` equals :func: `r2_score ` (for single targets).
2520
+
2521
+ A scorer object with a specific choice of ``power `` can be built by::
2522
+
2523
+ >>> from sklearn.metrics import d2_tweedie_score, make_scorer
2524
+ >>> d2_tweedie_score_15 = make_scorer(d2_tweedie_score, power=1.5)
2525
+
2526
+ D² pinball score
2527
+ ^^^^^^^^^^^^^^^^^^^^^
2528
+
2529
+ The :func: `d2_pinball_score ` function implements the special case
2530
+ of D² with the pinball loss, see :ref: `pinball_loss `, i.e.:
2531
+
2532
+ .. math ::
2533
+
2534
+ \text {dev}(y, \hat {y}) = \text {pinball}(y, \hat {y}).
2535
+
2536
+ The argument ``alpha `` defines the slope of the pinball loss as for
2537
+ :func: `mean_pinball_loss ` (:ref: `pinball_loss `). It determines the
2538
+ quantile level ``alpha `` for which the pinball loss and also D²
2539
+ are optimal. Note that for `alpha=0.5 ` (the default) :func: `d2_pinball_score `
2540
+ equals :func: `d2_absolute_error_score `.
2541
+
2542
+ A scorer object with a specific choice of ``alpha `` can be built by::
2543
+
2544
+ >>> from sklearn.metrics import d2_pinball_score, make_scorer
2545
+ >>> d2_pinball_score_08 = make_scorer(d2_pinball_score, alpha=0.8)
2546
+
2547
+ D² absolute error score
2548
+ ^^^^^^^^^^^^^^^^^^^^^^^
2549
+
2550
+ The :func: `d2_absolute_error_score ` function implements the special case of
2551
+ the :ref: `mean_absolute_error `:
2552
+
2553
+ .. math ::
2554
+
2555
+ \text {dev}(y, \hat {y}) = \text {MAE}(y, \hat {y}).
2556
+
2557
+ Here are some usage examples of the :func: `d2_absolute_error_score ` function::
2558
+
2559
+ >>> from sklearn.metrics import d2_absolute_error_score
2560
+ >>> y_true = [3, -0.5, 2, 7]
2561
+ >>> y_pred = [2.5, 0.0, 2, 8]
2562
+ >>> d2_absolute_error_score(y_true, y_pred)
2563
+ 0.764...
2564
+ >>> y_true = [1, 2, 3]
2565
+ >>> y_pred = [1, 2, 3]
2566
+ >>> d2_absolute_error_score(y_true, y_pred)
2567
+ 1.0
2568
+ >>> y_true = [1, 2, 3]
2569
+ >>> y_pred = [2, 2, 2]
2570
+ >>> d2_absolute_error_score(y_true, y_pred)
2571
+ 0.0
2572
+
2510
2573
2511
2574
.. _clustering_metrics :
2512
2575
0 commit comments