.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/stats/plot_residuals_distribution_script.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_stats_plot_residuals_distribution_script.py: plot_residuals_distribution with examples ========================================== An example showing the :py:func:`~scikitplot.api.metrics.plot_residuals_distribution` function used by a scikit-learn regressor. .. GENERATED FROM PYTHON SOURCE LINES 8-12 .. code-block:: Python :lineno-start: 9 # Authors: The scikit-plots developers # SPDX-License-Identifier: BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 13-15 Import scikit-plots ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 15-31 .. code-block:: Python :lineno-start: 16 from sklearn.datasets import ( load_diabetes as load_data, ) from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split import numpy as np np.random.seed(0) # reproducibility # importing pylab or pyplot import matplotlib.pyplot as plt # Import scikit-plots import scikitplot as sp .. GENERATED FROM PYTHON SOURCE LINES 32-34 Loading the dataset ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 34-39 .. code-block:: Python :lineno-start: 35 # Load the data X, y = load_data(return_X_y=True, as_frame=True) X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.5, random_state=0) .. GENERATED FROM PYTHON SOURCE LINES 40-42 Model Training -------------- .. GENERATED FROM PYTHON SOURCE LINES 42-49 .. code-block:: Python :lineno-start: 43 # Create an instance of the LogisticRegression model = LinearRegression().fit(X_train, y_train) # Perform predictions y_val_pred = model.predict(X_val) .. GENERATED FROM PYTHON SOURCE LINES 50-52 Plot! ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 52-65 .. code-block:: Python :lineno-start: 53 # Plot! ax = sp.metrics.plot_residuals_distribution( y_val, y_val_pred, dist_type="normal", save_fig=True, save_fig_filename="", # overwrite=True, add_timestamp=True, verbose=True, ) .. image-sg:: /auto_examples/stats/images/sphx_glr_plot_residuals_distribution_script_001.png :alt: Histogram of Residuals, Q-Q Plot: Fitted Normal μ=-4.45, σ=55.28, Q-Q Plot: Standard Normal mean=0, std=1 :srcset: /auto_examples/stats/images/sphx_glr_plot_residuals_distribution_script_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Fitted mean-mu (μ): -4.4509 Fitted std (σ) : 55.2768 [INFO] Saving path to: /home/circleci/repo/galleries/examples/stats/result_images/plot_residuals_distribution_20250629_134215Z.png [INFO] Plot saved to: /home/circleci/repo/galleries/examples/stats/result_images/plot_residuals_distribution_20250629_134215Z.png .. GENERATED FROM PYTHON SOURCE LINES 66-83 .. admonition:: References The use of the following functions, methods, classes and modules is shown in this example: - https://www.itl.nist.gov/div898/handbook/pri/section2/pri24.htm - https://online.stat.psu.edu/stat462/node/122/ .. tags:: model-type: regression model-workflow: model evaluation plot-type: histogram plot-type: qqplot domain: statistics level: intermediate purpose: showcase .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.722 seconds) .. _sphx_glr_download_auto_examples_stats_plot_residuals_distribution_script.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-plots/scikit-plots/main?urlpath=lab/tree/notebooks/auto_examples/stats/plot_residuals_distribution_script.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/stats/plot_residuals_distribution_script.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_residuals_distribution_script.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_residuals_distribution_script.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_residuals_distribution_script.zip ` .. include:: plot_residuals_distribution_script.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_