plot_residuals_distribution with examples#

An example showing the plot_residuals_distribution function used by a scikit-learn regressor.

 9 # Authors: The scikit-plots developers
10 # SPDX-License-Identifier: BSD-3-Clause

Import scikit-plots#

16 from sklearn.datasets import (
17     load_diabetes as load_data,
18 )
19 from sklearn.linear_model import LinearRegression
20 from sklearn.model_selection import train_test_split
21
22 import numpy as np
23
24 np.random.seed(0)  # reproducibility
25 # importing pylab or pyplot
26 import matplotlib.pyplot as plt
27
28 # Import scikit-plots
29 import scikitplot as sp

Loading the dataset#

35 # Load the data
36 X, y = load_data(return_X_y=True, as_frame=True)
37 X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.5, random_state=0)

Model Training#

43 # Create an instance of the LogisticRegression
44 model = LinearRegression().fit(X_train, y_train)
45
46 # Perform predictions
47 y_val_pred = model.predict(X_val)

Plot!#

53 # Plot!
54 ax = sp.metrics.plot_residuals_distribution(
55     y_val,
56     y_val_pred,
57     dist_type="normal",
58     save_fig=True,
59     save_fig_filename="",
60     # overwrite=True,
61     add_timestamp=True,
62     verbose=True,
63 )
Histogram of Residuals, Q-Q Plot: Fitted Normal μ=-4.45, σ=55.28, Q-Q Plot: Standard Normal mean=0, std=1
Fitted mean-mu (μ): -4.4509
Fitted std (σ)    : 55.2768
[INFO] Saving path to: /home/circleci/repo/galleries/examples/regression/result_images/plot_residuals_distribution_20250627_090925Z.png
[INFO] Plot saved to: /home/circleci/repo/galleries/examples/regression/result_images/plot_residuals_distribution_20250627_090925Z.png

References

The use of the following functions, methods, classes and modules is shown in this example:

Tags: model-type: regression model-workflow: model evaluation plot-type: histogram plot-type: qqplot domain: statistics level: intermediate purpose: showcase

Total running time of the script: (0 minutes 0.721 seconds)

Related examples

plot_confusion_matrix with examples

plot_confusion_matrix with examples

plot_precision_recall with examples

plot_precision_recall with examples

plot_roc_curve with examples

plot_roc_curve with examples

plot_feature_importances with examples

plot_feature_importances with examples

Gallery generated by Sphinx-Gallery