.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/seaborn/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_seaborn_plot_aucplot_script.py: plot_aucplot_script with examples ========================================== An example showing the :py:func:`~scikitplot.seaborn.aucplot` function with a scikit-learn classifier (e.g., :py:class:`~sklearn.linear_model.LogisticRegression`) instance. .. GENERATED FROM PYTHON SOURCE LINES 8-12 .. code-block:: Python # 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 import scikitplot.seaborn as sp .. GENERATED FROM PYTHON SOURCE LINES 18-25 .. code-block:: Python 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], fmt='' ) .. image-sg:: /auto_examples/seaborn/images/sphx_glr_plot_aucplot_script_001.png :alt: ROC (Receiver Operating Characteristic) Curve :srcset: /auto_examples/seaborn/images/sphx_glr_plot_aucplot_script_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 26-34 .. code-block:: Python 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", fmt='' ) .. image-sg:: /auto_examples/seaborn/images/sphx_glr_plot_aucplot_script_002.png :alt: PR (Precision-Recall) Curve :srcset: /auto_examples/seaborn/images/sphx_glr_plot_aucplot_script_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 35-49 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np; np.random.seed(0) # reproducibility import pandas as pd from sklearn.datasets import make_classification 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 50-53 Load the data X, y = data_3_classes(return_X_y=True, as_frame=False) X, y = data_2_classes(return_X_y=True, as_frame=False) .. GENERATED FROM PYTHON SOURCE LINES 53-60 .. code-block:: Python # Generate a sample dataset X, y = make_classification(n_samples=5000, n_features=20, n_informative=15, n_redundant=2, n_classes=2, n_repeated=0, class_sep=1.5, flip_y=0.01, weights=[0.85, 0.15], random_state=0) .. GENERATED FROM PYTHON SOURCE LINES 61-66 .. code-block:: Python X_train, X_val, y_train, y_val = train_test_split( X, y, stratify=y, test_size=0.2, random_state=0 ) np.unique(y) .. rst-class:: sphx-glr-script-out .. code-block:: none array([0, 1]) .. GENERATED FROM PYTHON SOURCE LINES 67-68 Create an instance of the LogisticRegression .. GENERATED FROM PYTHON SOURCE LINES 68-94 .. code-block:: Python model = ( LogisticRegression( # max_iter=int(1e5), # C=10, # penalty='l1', # solver='liblinear', class_weight='balanced', 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) # np.argmax "y_pred": y_val_prob[:, 1] > 0.5, # 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(), }) df .. raw:: html
y_true y_score y_pred
0 False 0.033725 False
1 True 0.860583 True
2 False 0.423101 False
3 False 0.137295 False
4 False 0.788645 True
... ... ... ...
995 False 0.228034 False
996 False 0.017187 False
997 True 0.987892 True
998 False 0.931136 True
999 False 0.128248 False

1000 rows × 3 columns



.. GENERATED FROM PYTHON SOURCE LINES 95-98 .. code-block:: Python ax = sp.aucplot(x=df.y_true, y=df.y_score) .. image-sg:: /auto_examples/seaborn/images/sphx_glr_plot_aucplot_script_003.png :alt: ROC (Receiver Operating Characteristic) Curve :srcset: /auto_examples/seaborn/images/sphx_glr_plot_aucplot_script_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 99-109 .. code-block:: Python ax = sp.aucplot( df, x="y_true", y="y_score", kind="pr", label=f"class 1", # fmt='' ) .. image-sg:: /auto_examples/seaborn/images/sphx_glr_plot_aucplot_script_004.png :alt: PR (Precision-Recall) Curve :srcset: /auto_examples/seaborn/images/sphx_glr_plot_aucplot_script_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 110-127 .. code-block:: Python for i in range(2): ax = sp.aucplot( x=y_val==i, y=y_val_prob[:, i], # kind="roc", label=f"class {i}", # fmt='' ) # --- 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="Val Dataset") .. image-sg:: /auto_examples/seaborn/images/sphx_glr_plot_aucplot_script_005.png :alt: ROC (Receiver Operating Characteristic) Curve :srcset: /auto_examples/seaborn/images/sphx_glr_plot_aucplot_script_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 128-155 .. code-block:: Python for i in range(2): ax = sp.aucplot( x=y_val==i, y=y_val_prob[:, i], kind="pr", label=f"class {i}", # fmt='' ) # # 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="Val Dataset") .. image-sg:: /auto_examples/seaborn/images/sphx_glr_plot_aucplot_script_006.png :alt: PR (Precision-Recall) Curve :srcset: /auto_examples/seaborn/images/sphx_glr_plot_aucplot_script_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 156-164 .. tags:: model-type: classification model-workflow: model evaluation plot-type: line plot-type: auc level: beginner purpose: showcase .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.609 seconds) .. _sphx_glr_download_auto_examples_seaborn_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/main?urlpath=lab/tree/notebooks/auto_examples/seaborn/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/seaborn/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 `_