From 76cca9cb98a5ef837b41c928b4bb238e0ecebfbc Mon Sep 17 00:00:00 2001 From: Syed Rizvi Date: Tue, 22 Apr 2025 14:00:41 +0100 Subject: [PATCH 1/3] DOC Clarify usage of d2_pinball_score with model selection tools --- sklearn/metrics/_regression.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/sklearn/metrics/_regression.py b/sklearn/metrics/_regression.py index 9be9f1d954fcc..43357c2bb9aa5 100644 --- a/sklearn/metrics/_regression.py +++ b/sklearn/metrics/_regression.py @@ -1750,7 +1750,17 @@ def d2_pinball_score( This metric is not well-defined for a single point and will return a NaN value if n_samples is less than two. - References + This metric is not a built-in scoring string for use in model selection + tools such as `GridSearchCV` or `RandomizedSearchCV`. + + To use it as a custom scoring function, wrap it using + :func:`~sklearn.metrics.make_scorer`: + + >>> from sklearn.metrics import make_scorer, d2_pinball_score + >>> scorer = make_scorer(d2_pinball_score, alpha=0.95) + >>> # Then use it as `scoring=scorer` in RandomizedSearchCV or GridSearchCV + + References ---------- .. [1] Eq. (7) of `Koenker, Roger; Machado, José A. F. (1999). "Goodness of Fit and Related Inference Processes for Quantile Regression" @@ -1772,7 +1782,12 @@ def d2_pinball_score( -1.045... >>> d2_pinball_score(y_true, y_true, alpha=0.1) 1.0 + + >>> # Using with make_scorer + >>> from sklearn.metrics import make_scorer + >>> scorer = make_scorer(d2_pinball_score, alpha=0.95) """ + y_type, y_true, y_pred, multioutput = _check_reg_targets( y_true, y_pred, multioutput ) From afd30712d3bb7a637194bd12d6c2c1f3b66ec70d Mon Sep 17 00:00:00 2001 From: Syed Rizvi Date: Sat, 26 Apr 2025 15:45:47 +0100 Subject: [PATCH 2/3] DOC Update d2_pinball_score docstring: move scorer example to Examples section --- sklearn/metrics/_regression.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/sklearn/metrics/_regression.py b/sklearn/metrics/_regression.py index 43357c2bb9aa5..2f0c6bab9dd7d 100644 --- a/sklearn/metrics/_regression.py +++ b/sklearn/metrics/_regression.py @@ -1756,10 +1756,6 @@ def d2_pinball_score( To use it as a custom scoring function, wrap it using :func:`~sklearn.metrics.make_scorer`: - >>> from sklearn.metrics import make_scorer, d2_pinball_score - >>> scorer = make_scorer(d2_pinball_score, alpha=0.95) - >>> # Then use it as `scoring=scorer` in RandomizedSearchCV or GridSearchCV - References ---------- .. [1] Eq. (7) of `Koenker, Roger; Machado, José A. F. (1999). @@ -1783,10 +1779,11 @@ def d2_pinball_score( >>> d2_pinball_score(y_true, y_true, alpha=0.1) 1.0 - >>> # Using with make_scorer - >>> from sklearn.metrics import make_scorer + >>> # Using d2_pinball_score with model selection tools + >>> from sklearn.metrics import make_scorer, d2_pinball_score >>> scorer = make_scorer(d2_pinball_score, alpha=0.95) - """ + >>> # Use `scoring=scorer` in RandomizedSearchCV or GridSearchCV +""" y_type, y_true, y_pred, multioutput = _check_reg_targets( y_true, y_pred, multioutput From fe85a06d2ef4f08d9c8fd064bd8193edd7b19124 Mon Sep 17 00:00:00 2001 From: Syed Rizvi Date: Sat, 26 Apr 2025 15:58:48 +0100 Subject: [PATCH 3/3] Expand examples section to show how to use d2_pinball_score with make_scorer and GridSearchCV --- sklearn/metrics/_regression.py | 37 +++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/sklearn/metrics/_regression.py b/sklearn/metrics/_regression.py index 2f0c6bab9dd7d..31c2e0bddaa90 100644 --- a/sklearn/metrics/_regression.py +++ b/sklearn/metrics/_regression.py @@ -1752,9 +1752,8 @@ def d2_pinball_score( This metric is not a built-in scoring string for use in model selection tools such as `GridSearchCV` or `RandomizedSearchCV`. - To use it as a custom scoring function, wrap it using - :func:`~sklearn.metrics.make_scorer`: + :func:`~sklearn.metrics.make_scorer`. See Examples for details. References ---------- @@ -1767,23 +1766,29 @@ def d2_pinball_score( Examples -------- + >>> import numpy as np >>> from sklearn.metrics import d2_pinball_score - >>> y_true = [1, 2, 3] - >>> y_pred = [1, 3, 3] - >>> d2_pinball_score(y_true, y_pred) - 0.5 - >>> d2_pinball_score(y_true, y_pred, alpha=0.9) - 0.772... - >>> d2_pinball_score(y_true, y_pred, alpha=0.1) - -1.045... - >>> d2_pinball_score(y_true, y_true, alpha=0.1) - 1.0 + >>> y_true = [3, -0.5, 2, 7] + >>> y_pred = [2.5, 0.0, 2, 8] + >>> d2_pinball_score(y_true, y_pred, alpha=0.95) + 0.968... + + Using with :func:`~sklearn.metrics.make_scorer`: - >>> # Using d2_pinball_score with model selection tools >>> from sklearn.metrics import make_scorer, d2_pinball_score - >>> scorer = make_scorer(d2_pinball_score, alpha=0.95) - >>> # Use `scoring=scorer` in RandomizedSearchCV or GridSearchCV -""" + >>> pinball_95_scorer = make_scorer(d2_pinball_score, alpha=0.95) + >>> from sklearn.model_selection import GridSearchCV + >>> from sklearn.linear_model import LinearRegression + >>> X = np.array([[1], [2], [3], [4]]) + >>> y = np.array([2.5, 0.0, 2, 8]) + >>> grid = GridSearchCV( + ... LinearRegression(), + ... param_grid={"fit_intercept": [True, False]}, + ... scoring=pinball_95_scorer, + ... cv=2, + ... ) + >>> _ = grid.fit(X, y) + """ y_type, y_true, y_pred, multioutput = _check_reg_targets( y_true, y_pred, multioutput