plot_aucplot_script with examples#

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

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

Import scikit-plot

14 import scikitplot.snsx as sp
18 ax = sp.aucplot(
19     x=[0, 1, 1, 0, 1, 1, 0, 1, 1, 0],
20     y=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0],
21 )
ROC (Receiver Operating Characteristic) Curve
25 ax = sp.aucplot(
26     x=[0, 1, 1, 0, 1, 1, 0, 1, 1, 0],
27     y=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0],
28     kind="pr",
29 )
PR (Precision-Recall) Curve
33 import matplotlib.pyplot as plt
34 import numpy as np; np.random.seed(0)  # reproducibility
35 import pandas as pd
36
37 from sklearn.datasets import (
38     load_breast_cancer as data_2_classes,
39     load_iris as data_3_classes,
40     load_digits as data_10_classes,
41 )
42 from sklearn.linear_model import LogisticRegression
43 from sklearn.model_selection import train_test_split

Load the data

48 X, y = data_10_classes(return_X_y=True, as_frame=False)
49 # X, y = data_3_classes(return_X_y=True, as_frame=False)
50 X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=0)
51 np.unique(y)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Create an instance of the LogisticRegression

55 model = (
56     LogisticRegression(max_iter=int(1), random_state=0)
57     .fit(X_train, y_train)
58 )
59 # Perform predictions
60 y_val_prob = model.predict_proba(X_val)
61 # Create a DataFrame with predictions
62 df = pd.DataFrame({
63     "y_true": y_val==1,  # target class (0,1,2)
64     "y_score": y_val_prob[:, 1],  # target class (0,1,2)
65     # "y_true": np.random.normal(0.5, 0.1, 100).round(),
66     # "y_score": np.random.normal(0.5, 0.15, 100),
67     # "hue": np.random.normal(0.5, 0.4, 100).round(),
68 })
72 ax = sp.aucplot(x=df.y_true, y=df.y_score)
ROC (Receiver Operating Characteristic) Curve
76 ax = sp.aucplot(
77     df,
78     x="y_true",
79     y="y_score",
80     kind="pr",
81     label=f"class 1",
82 )
PR (Precision-Recall) Curve
86 for i in range(10):
87     ax = sp.aucplot(
88         x=y_val==i,
89         y=y_val_prob[:, i],
90         label=f"class {i}",
91     )
92
93     # --- Collect unique handles and labels ---
94     handles, labels = ax.get_legend_handles_labels()
95     by_label = dict(zip(labels, handles))  # deduplicate
96
97     # Override legend
98     ax.legend(by_label.values(), by_label.keys(), title="Legend")
ROC (Receiver Operating Characteristic) Curve
102 for i in range(10):
103     ax = sp.aucplot(
104         x=y_val==i,
105         y=y_val_prob[:, i],
106         kind="pr",
107         label=f"class {i}",
108     )
109
110     # # With raw arrays (no DataFrame)
111     # # Works because seaborn normalizes arrays internally
112     # np.random.seed(i)  # reproducibility
113     # ax = sp.aucplot(
114     #     x=np.random.normal(0.5, 0.1, 100).round(),
115     #     y=np.random.normal(0.5, 0.1, 100),
116     #     kind="pr",
117     #     label=f"{i}",
118     # )
119
120     # --- Collect unique handles and labels ---
121     handles, labels = ax.get_legend_handles_labels()
122     by_label = dict(zip(labels, handles))  # deduplicate
123
124     # Override legend
125     ax.legend(by_label.values(), by_label.keys(), title="Legend")
PR (Precision-Recall) Curve

Tags: model-type: classification model-workflow: model evaluation plot-type: line plot-type: auc curve level: beginner purpose: showcase

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

Related examples

plot_kdsplot_script with examples

plot_kdsplot_script with examples

plot_cumulative_gain with examples

plot_cumulative_gain with examples

plot_lift with examples

plot_lift with examples

plot_report with examples

plot_report with examples

Gallery generated by Sphinx-Gallery