PredictionErrorDisplay#

classsklearn.metrics.PredictionErrorDisplay(*,y_true,y_pred)[source]#

Visualization of the prediction error of a regression model.

This tool can display “residuals vs predicted” or “actual vs predicted”using scatter plots to qualitatively assess the behavior of a regressor,preferably on held-out data points.

See the details in the docstrings offrom_estimator orfrom_predictions tocreate a visualizer. All parameters are stored as attributes.

For general information regardingscikit-learn visualization tools, readmore in theVisualization Guide.For details regarding interpreting these plots, refer to theModel Evaluation Guide.

Added in version 1.2.

Parameters:
y_truendarray of shape (n_samples,)

True values.

y_predndarray of shape (n_samples,)

Prediction values.

Attributes:
line_matplotlib Artist

Optimal line representingy_true==y_pred. Therefore, it is adiagonal line forkind="predictions" and a horizontal line forkind="residuals".

errors_lines_matplotlib Artist or None

Residual lines. Ifwith_errors=False, then it is set toNone.

scatter_matplotlib Artist

Scatter data points.

ax_matplotlib Axes

Axes with the different matplotlib axis.

figure_matplotlib Figure

Figure containing the scatter and lines.

See also

PredictionErrorDisplay.from_estimator

Prediction error visualization given an estimator and some data.

PredictionErrorDisplay.from_predictions

Prediction error visualization given the true and predicted targets.

Examples

>>>importmatplotlib.pyplotasplt>>>fromsklearn.datasetsimportload_diabetes>>>fromsklearn.linear_modelimportRidge>>>fromsklearn.metricsimportPredictionErrorDisplay>>>X,y=load_diabetes(return_X_y=True)>>>ridge=Ridge().fit(X,y)>>>y_pred=ridge.predict(X)>>>display=PredictionErrorDisplay(y_true=y,y_pred=y_pred)>>>display.plot()<...>>>>plt.show()
../../_images/sklearn-metrics-PredictionErrorDisplay-1.png
classmethodfrom_estimator(estimator,X,y,*,kind='residual_vs_predicted',subsample=1000,random_state=None,ax=None,scatter_kwargs=None,line_kwargs=None)[source]#

Plot the prediction error given a regressor and some data.

For general information regardingscikit-learn visualization tools,read more in theVisualization Guide.For details regarding interpreting these plots, refer to theModel Evaluation Guide.

Added in version 1.2.

Parameters:
estimatorestimator instance

Fitted regressor or a fittedPipelinein which the last estimator is a regressor.

X{array-like, sparse matrix} of shape (n_samples, n_features)

Input values.

yarray-like of shape (n_samples,)

Target values.

kind{“actual_vs_predicted”, “residual_vs_predicted”}, default=”residual_vs_predicted”

The type of plot to draw:

  • “actual_vs_predicted” draws the observed values (y-axis) vs.the predicted values (x-axis).

  • “residual_vs_predicted” draws the residuals, i.e. differencebetween observed and predicted values, (y-axis) vs. the predictedvalues (x-axis).

subsamplefloat, int or None, default=1_000

Sampling the samples to be shown on the scatter plot. Iffloat,it should be between 0 and 1 and represents the proportion of theoriginal dataset. Ifint, it represents the number of samplesdisplay on the scatter plot. IfNone, no subsampling will beapplied. by default, 1000 samples or less will be displayed.

random_stateint or RandomState, default=None

Controls the randomness whensubsample is notNone.SeeGlossary for details.

axmatplotlib axes, default=None

Axes object to plot on. IfNone, a new figure and axes iscreated.

scatter_kwargsdict, default=None

Dictionary with keywords passed to thematplotlib.pyplot.scattercall.

line_kwargsdict, default=None

Dictionary with keyword passed to thematplotlib.pyplot.plotcall to draw the optimal line.

Returns:
displayPredictionErrorDisplay

Object that stores the computed values.

See also

PredictionErrorDisplay

Prediction error visualization for regression.

PredictionErrorDisplay.from_predictions

Prediction error visualization given the true and predicted targets.

Examples

>>>importmatplotlib.pyplotasplt>>>fromsklearn.datasetsimportload_diabetes>>>fromsklearn.linear_modelimportRidge>>>fromsklearn.metricsimportPredictionErrorDisplay>>>X,y=load_diabetes(return_X_y=True)>>>ridge=Ridge().fit(X,y)>>>disp=PredictionErrorDisplay.from_estimator(ridge,X,y)>>>plt.show()
../../_images/sklearn-metrics-PredictionErrorDisplay-2.png
classmethodfrom_predictions(y_true,y_pred,*,kind='residual_vs_predicted',subsample=1000,random_state=None,ax=None,scatter_kwargs=None,line_kwargs=None)[source]#

Plot the prediction error given the true and predicted targets.

For general information regardingscikit-learn visualization tools,read more in theVisualization Guide.For details regarding interpreting these plots, refer to theModel Evaluation Guide.

Added in version 1.2.

Parameters:
y_truearray-like of shape (n_samples,)

True target values.

y_predarray-like of shape (n_samples,)

Predicted target values.

kind{“actual_vs_predicted”, “residual_vs_predicted”}, default=”residual_vs_predicted”

The type of plot to draw:

  • “actual_vs_predicted” draws the observed values (y-axis) vs.the predicted values (x-axis).

  • “residual_vs_predicted” draws the residuals, i.e. differencebetween observed and predicted values, (y-axis) vs. the predictedvalues (x-axis).

subsamplefloat, int or None, default=1_000

Sampling the samples to be shown on the scatter plot. Iffloat,it should be between 0 and 1 and represents the proportion of theoriginal dataset. Ifint, it represents the number of samplesdisplay on the scatter plot. IfNone, no subsampling will beapplied. by default, 1000 samples or less will be displayed.

random_stateint or RandomState, default=None

Controls the randomness whensubsample is notNone.SeeGlossary for details.

axmatplotlib axes, default=None

Axes object to plot on. IfNone, a new figure and axes iscreated.

scatter_kwargsdict, default=None

Dictionary with keywords passed to thematplotlib.pyplot.scattercall.

line_kwargsdict, default=None

Dictionary with keyword passed to thematplotlib.pyplot.plotcall to draw the optimal line.

Returns:
displayPredictionErrorDisplay

Object that stores the computed values.

See also

PredictionErrorDisplay

Prediction error visualization for regression.

PredictionErrorDisplay.from_estimator

Prediction error visualization given an estimator and some data.

Examples

>>>importmatplotlib.pyplotasplt>>>fromsklearn.datasetsimportload_diabetes>>>fromsklearn.linear_modelimportRidge>>>fromsklearn.metricsimportPredictionErrorDisplay>>>X,y=load_diabetes(return_X_y=True)>>>ridge=Ridge().fit(X,y)>>>y_pred=ridge.predict(X)>>>disp=PredictionErrorDisplay.from_predictions(y_true=y,y_pred=y_pred)>>>plt.show()
../../_images/sklearn-metrics-PredictionErrorDisplay-3.png
plot(ax=None,*,kind='residual_vs_predicted',scatter_kwargs=None,line_kwargs=None)[source]#

Plot visualization.

Extra keyword arguments will be passed to matplotlib’splot.

Parameters:
axmatplotlib axes, default=None

Axes object to plot on. IfNone, a new figure and axes iscreated.

kind{“actual_vs_predicted”, “residual_vs_predicted”}, default=”residual_vs_predicted”

The type of plot to draw:

  • “actual_vs_predicted” draws the observed values (y-axis) vs.the predicted values (x-axis).

  • “residual_vs_predicted” draws the residuals, i.e. differencebetween observed and predicted values, (y-axis) vs. the predictedvalues (x-axis).

scatter_kwargsdict, default=None

Dictionary with keywords passed to thematplotlib.pyplot.scattercall.

line_kwargsdict, default=None

Dictionary with keyword passed to thematplotlib.pyplot.plotcall to draw the optimal line.

Returns:
displayPredictionErrorDisplay

Object that stores computed values.

Gallery examples#

Time-related feature engineering

Time-related feature engineering

Lagged features for time series forecasting

Lagged features for time series forecasting

Effect of transforming the targets in regression model

Effect of transforming the targets in regression model

Combine predictors using stacking

Combine predictors using stacking

Common pitfalls in the interpretation of coefficients of linear models

Common pitfalls in the interpretation of coefficients of linear models

Plotting Cross-Validated Predictions

Plotting Cross-Validated Predictions

Release Highlights for scikit-learn 1.2

Release Highlights for scikit-learn 1.2