plot_precision_recall with examples#

An example showing the plot_precision_recall function used by a scikit-learn classifier.

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

Import scikit-plots#

16 from sklearn.datasets import (
17     load_digits as data_10_classes,
18 )
19 from sklearn.linear_model import LogisticRegression
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-plot
29 import scikitplot as sp

Loading the dataset#

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

Model Training#

43 # Create an instance of the LogisticRegression
44 model = LogisticRegression(max_iter=1, random_state=0).fit(X_train, y_train)
45
46 # Perform predictions
47 y_val_prob = model.predict_proba(X_val)
/home/circleci/.pyenv/versions/3.11.13/lib/python3.11/site-packages/sklearn/linear_model/_logistic.py:470: ConvergenceWarning:

lbfgs failed to converge after 1 iteration(s) (status=1):
STOP: TOTAL NO. OF ITERATIONS REACHED LIMIT

Increase the number of iterations to improve the convergence (max_iter=1).
You might also want to scale the data as shown in:
    https://scikit-learn.org/stable/modules/preprocessing.html
Please also refer to the documentation for alternative solver options:
    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression

Plot!#

53 # Plot!
54 ax = sp.metrics.plot_precision_recall(
55     y_val,
56     y_val_prob,
57     save_fig=True,
58     save_fig_filename="",
59     # overwrite=True,
60     add_timestamp=True,
61     # verbose=True,
62 )
Precision-Recall AUC Curves

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

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

Related examples

plot_roc_curve with examples

plot_roc_curve with examples

plot_confusion_matrix with examples

plot_confusion_matrix with examples

plot_classifier_eval with examples

plot_classifier_eval with examples

plot_silhouette with examples

plot_silhouette with examples

Gallery generated by Sphinx-Gallery