.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/snsx/plot_aucplot_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_snsx_plot_aucplot_script.py: plot_aucplot_script with examples ========================================== An example showing the :py:func:`~scikitplot.snsx.aucplot` 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-14 Import scikit-plot .. GENERATED FROM PYTHON SOURCE LINES 14-17 .. code-block:: Python :lineno-start: 14 import scikitplot.snsx as sp .. GENERATED FROM PYTHON SOURCE LINES 18-24 .. code-block:: Python :lineno-start: 18 ax = sp.aucplot( x=[0, 1, 1, 0, 1, 1, 0, 1, 1, 0], y=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0], ) .. image-sg:: /auto_examples/snsx/images/sphx_glr_plot_aucplot_script_001.png :alt: ROC (Receiver Operating Characteristic) Curve :srcset: /auto_examples/snsx/images/sphx_glr_plot_aucplot_script_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 25-32 .. code-block:: Python :lineno-start: 25 ax = sp.aucplot( x=[0, 1, 1, 0, 1, 1, 0, 1, 1, 0], y=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0], kind="pr", ) .. image-sg:: /auto_examples/snsx/images/sphx_glr_plot_aucplot_script_002.png :alt: PR (Precision-Recall) Curve :srcset: /auto_examples/snsx/images/sphx_glr_plot_aucplot_script_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 33-46 .. code-block:: Python :lineno-start: 33 import matplotlib.pyplot as plt import numpy as np; np.random.seed(0) # reproducibility import pandas as pd from sklearn.datasets import ( load_breast_cancer as data_2_classes, load_iris as data_3_classes, load_digits as data_10_classes, ) from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split .. GENERATED FROM PYTHON SOURCE LINES 47-48 Load the data .. GENERATED FROM PYTHON SOURCE LINES 48-53 .. code-block:: Python :lineno-start: 48 X, y = data_10_classes(return_X_y=True, as_frame=False) # X, y = data_3_classes(return_X_y=True, as_frame=False) X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=0) np.unique(y) .. rst-class:: sphx-glr-script-out .. code-block:: none array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) .. GENERATED FROM PYTHON SOURCE LINES 54-55 Create an instance of the LogisticRegression .. GENERATED FROM PYTHON SOURCE LINES 55-71 .. code-block:: Python :lineno-start: 55 model = ( LogisticRegression(max_iter=int(1), random_state=0) .fit(X_train, y_train) ) # Perform predictions y_val_prob = model.predict_proba(X_val) # Create a DataFrame with predictions df = pd.DataFrame({ "y_true": y_val==1, # target class (0,1,2) "y_score": y_val_prob[:, 1], # target class (0,1,2) # "y_true": np.random.normal(0.5, 0.1, 100).round(), # "y_score": np.random.normal(0.5, 0.15, 100), # "hue": np.random.normal(0.5, 0.4, 100).round(), }) .. GENERATED FROM PYTHON SOURCE LINES 72-75 .. code-block:: Python :lineno-start: 72 ax = sp.aucplot(x=df.y_true, y=df.y_score) .. image-sg:: /auto_examples/snsx/images/sphx_glr_plot_aucplot_script_003.png :alt: ROC (Receiver Operating Characteristic) Curve :srcset: /auto_examples/snsx/images/sphx_glr_plot_aucplot_script_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 76-85 .. code-block:: Python :lineno-start: 76 ax = sp.aucplot( df, x="y_true", y="y_score", kind="pr", label=f"class 1", ) .. image-sg:: /auto_examples/snsx/images/sphx_glr_plot_aucplot_script_004.png :alt: PR (Precision-Recall) Curve :srcset: /auto_examples/snsx/images/sphx_glr_plot_aucplot_script_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 86-101 .. code-block:: Python :lineno-start: 86 for i in range(10): ax = sp.aucplot( x=y_val==i, y=y_val_prob[:, i], label=f"class {i}", ) # --- Collect unique handles and labels --- handles, labels = ax.get_legend_handles_labels() by_label = dict(zip(labels, handles)) # deduplicate # Override legend ax.legend(by_label.values(), by_label.keys(), title="Legend") .. image-sg:: /auto_examples/snsx/images/sphx_glr_plot_aucplot_script_005.png :alt: ROC (Receiver Operating Characteristic) Curve :srcset: /auto_examples/snsx/images/sphx_glr_plot_aucplot_script_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 102-128 .. code-block:: Python :lineno-start: 102 for i in range(10): ax = sp.aucplot( x=y_val==i, y=y_val_prob[:, i], kind="pr", label=f"class {i}", ) # # With raw arrays (no DataFrame) # # Works because seaborn normalizes arrays internally # np.random.seed(i) # reproducibility # ax = sp.aucplot( # x=np.random.normal(0.5, 0.1, 100).round(), # y=np.random.normal(0.5, 0.1, 100), # kind="pr", # label=f"{i}", # ) # --- Collect unique handles and labels --- handles, labels = ax.get_legend_handles_labels() by_label = dict(zip(labels, handles)) # deduplicate # Override legend ax.legend(by_label.values(), by_label.keys(), title="Legend") .. image-sg:: /auto_examples/snsx/images/sphx_glr_plot_aucplot_script_006.png :alt: PR (Precision-Recall) Curve :srcset: /auto_examples/snsx/images/sphx_glr_plot_aucplot_script_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 129-137 .. tags:: model-type: classification model-workflow: model evaluation plot-type: line plot-type: auc curve level: beginner purpose: showcase .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.710 seconds) .. _sphx_glr_download_auto_examples_snsx_plot_aucplot_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/maintenance/0.4.X?urlpath=lab/tree/notebooks/auto_examples/snsx/plot_aucplot_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/snsx/plot_aucplot_script.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_aucplot_script.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_aucplot_script.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_aucplot_script.zip ` .. include:: plot_aucplot_script.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_