diff --git a/sklearn/metrics/_regression.py b/sklearn/metrics/_regression.py index 9be9f1d954fcc..31c2e0bddaa90 100644 --- a/sklearn/metrics/_regression.py +++ b/sklearn/metrics/_regression.py @@ -1750,7 +1750,12 @@ 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`. See Examples for details. + + References ---------- .. [1] Eq. (7) of `Koenker, Roger; Machado, José A. F. (1999). "Goodness of Fit and Related Inference Processes for Quantile Regression" @@ -1761,18 +1766,30 @@ 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`: + + >>> from sklearn.metrics import make_scorer, d2_pinball_score + >>> 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 )