Skip to content

Commit

Permalink
FEA add PredictionErrorDisplay (#18020)
Browse files Browse the repository at this point in the history
Co-authored-by: jeremie du boisberranger <jeremiedbb@yahoo.fr>
Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
Co-authored-by: Christian Lorentzen <lorentzen.ch@gmail.com>
  • Loading branch information
4 people committed Nov 25, 2022
1 parent f493dd3 commit 40d7d88
Show file tree
Hide file tree
Showing 11 changed files with 929 additions and 259 deletions.
1 change: 1 addition & 0 deletions doc/modules/classes.rst
Expand Up @@ -1126,6 +1126,7 @@ See the :ref:`visualizations` section of the user guide for further details.
metrics.ConfusionMatrixDisplay
metrics.DetCurveDisplay
metrics.PrecisionRecallDisplay
metrics.PredictionErrorDisplay
metrics.RocCurveDisplay
calibration.CalibrationDisplay

Expand Down
74 changes: 74 additions & 0 deletions doc/modules/model_evaluation.rst
Expand Up @@ -2711,6 +2711,80 @@ Here are some usage examples of the :func:`d2_absolute_error_score` function::
>>> d2_absolute_error_score(y_true, y_pred)
0.0

.. _visualization_regression_evaluation:

Visual evaluation of regression models
--------------------------------------

Among methods to assess the quality of regression models, scikit-learn provides
the :class:`~sklearn.metrics.PredictionErrorDisplay` class. It allows to
visually inspect the prediction errors of a model in two different manners.

.. image:: ../auto_examples/model_selection/images/sphx_glr_plot_cv_predict_001.png
:target: ../auto_examples/model_selection/plot_cv_predict.html
:scale: 75
:align: center

The plot on the left shows the actual values vs predicted values. For a
noise-free regression task aiming to predict the (conditional) expectation of
`y`, a perfect regression model would display data points on the diagonal
defined by predicted equal to actual values. The further away from this optimal
line, the larger the error of the model. In a more realistic setting with
irreducible noise, that is, when not all the variations of `y` can be explained
by features in `X`, then the best model would lead to a cloud of points densely
arranged around the diagonal.

Note that the above only holds when the predicted values is the expected value
of `y` given `X`. This is typically the case for regression models that
minimize the mean squared error objective function or more generally the
:ref:`mean Tweedie deviance <mean_tweedie_deviance>` for any value of its
"power" parameter.

When plotting the predictions of an estimator that predicts a quantile
of `y` given `X`, e.g. :class:`~sklearn.linear_model.QuantileRegressor`
or any other model minimizing the :ref:`pinball loss <pinball_loss>`, a
fraction of the points are either expected to lie above or below the diagonal
depending on the estimated quantile level.

All in all, while intuitive to read, this plot does not really inform us on
what to do to obtain a better model.

The right-hand side plot shows the residuals (i.e. the difference between the
actual and the predicted values) vs. the predicted values.

This plot makes it easier to visualize if the residuals follow and
`homoscedastic or heteroschedastic
<https://en.wikipedia.org/wiki/Homoscedasticity_and_heteroscedasticity>`_
distribution.

In particular, if the true distribution of `y|X` is Poisson or Gamma
distributed, it is expected that the variance of the residuals of the optimal
model would grow with the predicted value of `E[y|X]` (either linearly for
Poisson or quadratically for Gamma).

When fitting a linear least squares regression model (see
:class:`~sklearn.linear_mnodel.LinearRegression` and
:class:`~sklearn.linear_mnodel.Ridge`), we can use this plot to check
if some of the `model assumptions
<https://en.wikipedia.org/wiki/Ordinary_least_squares#Assumptions>`_
are met, in particular that the residuals should be uncorrelated, their
expected value should be null and that their variance should be constant
(homoschedasticity).

If this is not the case, and in particular if the residuals plot show some
banana-shaped structure, this is a hint that the model is likely mis-specified
and that non-linear feature engineering or switching to a non-linear regression
model might be useful.

Refer to the example below to see a model evaluation that makes use of this
display.

.. topic:: Example:

* See :ref:`sphx_glr_auto_examples_compose_plot_transformed_target.py` for
an example on how to use :class:`~sklearn.metrics.PredictionErrorDisplay`
to visualize the prediction quality improvement of a regression model
obtained by transforming the target before learning.

.. _clustering_metrics:

Expand Down
1 change: 1 addition & 0 deletions doc/visualizations.rst
Expand Up @@ -86,5 +86,6 @@ Display Objects
metrics.ConfusionMatrixDisplay
metrics.DetCurveDisplay
metrics.PrecisionRecallDisplay
metrics.PredictionErrorDisplay
metrics.RocCurveDisplay
model_selection.LearningCurveDisplay
7 changes: 7 additions & 0 deletions doc/whats_new/v1.2.rst
Expand Up @@ -504,6 +504,13 @@ Changelog
of a binary classification problem. :pr:`22518` by
:user:`Arturo Amor <ArturoAmorQ>`.

- |Feature| Add :class:`metrics.PredictionErrorDisplay` to plot residuals vs
predicted and actual vs predicted to qualitatively assess the behavior of a
regressor. The display can be created with the class methods
:func:`metrics.PredictionErrorDisplay.from_estimator` and
:func:`metrics.PredictionErrorDisplay.from_predictions`. :pr:`18020` by
:user:`Guillaume Lemaitre <glemaitre>`.

- |Feature| :func:`metrics.roc_auc_score` now supports micro-averaging
(`average="micro"`) for the One-vs-Rest multiclass case (`multi_class="ovr"`).
:pr:`24338` by :user:`Arturo Amor <ArturoAmorQ>`.
Expand Down

0 comments on commit 40d7d88

Please sign in to comment.