Note
Go to the end to download the full example code. or to run this example in your browser via JupyterLite or Binder
Visualizations with Display Objects#
In this example, we will construct display objects,
ConfusionMatrixDisplay
, RocCurveDisplay
, and
PrecisionRecallDisplay
directly from their respective metrics. This
is an alternative to using their corresponding plot functions when
a model’s predictions are already computed or expensive to compute. Note that
this is advanced usage, and in general we recommend using their respective
plot functions.
# Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause
Load Data and train model#
For this example, we load a blood transfusion service center data set from OpenML. This is a binary classification problem where the target is whether an individual donated blood. Then the data is split into a train and test dataset and a logistic regression is fitted with the train dataset.
fromsklearn.datasetsimport fetch_openml fromsklearn.linear_modelimport LogisticRegression fromsklearn.model_selectionimport train_test_split fromsklearn.pipelineimport make_pipeline fromsklearn.preprocessingimport StandardScaler X, y = fetch_openml (data_id=1464, return_X_y=True) X_train, X_test, y_train, y_test = train_test_split (X, y, stratify=y) clf = make_pipeline (StandardScaler (), LogisticRegression (random_state=0)) clf.fit(X_train, y_train)
Pipeline(steps=[('standardscaler', StandardScaler()), ('logisticregression', LogisticRegression(random_state=0))])In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.