.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/seaborn/plot_evalplot_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_evalplot_script.py: plot_evalplot_script with examples ========================================== .. currentmodule:: scikitplot.seaborn An example showing the :py:func:`~scikitplot.seaborn.evalplot` function with a scikit-learn classifier (e.g., :py:class:`~sklearn.linear_model.LogisticRegression`) instance. .. GENERATED FROM PYTHON SOURCE LINES 10-14 .. code-block:: Python # Authors: The scikit-plots developers # SPDX-License-Identifier: BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 15-16 Import scikit-plot .. GENERATED FROM PYTHON SOURCE LINES 16-19 .. code-block:: Python import scikitplot.seaborn as sp .. GENERATED FROM PYTHON SOURCE LINES 20-50 .. 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 def logistic_scale(scores): """Scale decision_function outputs to (0,1) using the logistic (sigmoid) function.""" scores = np.asarray(scores, dtype=float) # Clip to avoid overflow for large |x| before exp # scores = np.clip(scores, -500, 500) return 1.0 / (1.0 + np.exp(-scores)) def minmax_scale(scores): """Linearly scale an array to [0,1].""" scores = np.asarray(scores, dtype=float) min_, max_ = np.min(scores), np.max(scores) if np.isclose(min_, max_): # Avoid divide-by-zero when all values identical return np.zeros_like(scores) return (scores - min_) / (max_ - min_) .. GENERATED FROM PYTHON SOURCE LINES 51-54 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 54-61 .. 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 62-67 .. 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 68-69 Create an instance of the LogisticRegression .. GENERATED FROM PYTHON SOURCE LINES 69-96 .. 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 97-108 .. code-block:: Python p = sp.evalplot( df, x="y_true", y="y_pred", # y="y_score", # allow_probs=True, # if y_score provided # threshold=0.5, kind="all", ) .. image-sg:: /auto_examples/seaborn/images/sphx_glr_plot_evalplot_script_001.png :alt: Classification Report, Confusion Matrix :srcset: /auto_examples/seaborn/images/sphx_glr_plot_evalplot_script_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 109-117 .. code-block:: Python p = sp.evalplot( df, x="y_true", y="y_pred", kind="classification_report", text_kws={'fontsize': 16}, ) .. image-sg:: /auto_examples/seaborn/images/sphx_glr_plot_evalplot_script_002.png :alt: Classification Report :srcset: /auto_examples/seaborn/images/sphx_glr_plot_evalplot_script_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 118-125 .. code-block:: Python p = sp.evalplot( df, x="y_true", y="y_pred", kind="confusion_matrix", ) .. image-sg:: /auto_examples/seaborn/images/sphx_glr_plot_evalplot_script_003.png :alt: Confusion Matrix :srcset: /auto_examples/seaborn/images/sphx_glr_plot_evalplot_script_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 126-127 fig, ax = plt.subplots(figsize=(8, 6)) .. GENERATED FROM PYTHON SOURCE LINES 127-137 .. code-block:: Python p = sp.evalplot( df, x="y_true", # y="y_pred", y="y_score", allow_probs=True, # if y_score provided threshold=0.5, kind="all", ) .. image-sg:: /auto_examples/seaborn/images/sphx_glr_plot_evalplot_script_004.png :alt: Classification Report, Confusion Matrix :srcset: /auto_examples/seaborn/images/sphx_glr_plot_evalplot_script_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 138-151 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt 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.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split from sklearn.metrics import classification_report, confusion_matrix .. GENERATED FROM PYTHON SOURCE LINES 152-155 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 155-162 .. code-block:: Python # Generate a sample dataset X, y = make_classification(n_samples=5000, n_features=50, n_informative=45, n_redundant=2, n_classes=3, n_repeated=0, class_sep=1.5, flip_y=0.01, #weights=[0.97, 0.03], random_state=0) .. GENERATED FROM PYTHON SOURCE LINES 163-168 .. 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, 2]) .. GENERATED FROM PYTHON SOURCE LINES 169-170 Initialize the Random Forest Classifier .. GENERATED FROM PYTHON SOURCE LINES 170-180 .. code-block:: Python rf_model = RandomForestClassifier( class_weight='balanced', n_estimators=100, max_depth=6, random_state=0, ) # Train the model rf_model.fit(X_train, y_train) .. raw:: html
RandomForestClassifier(class_weight='balanced', max_depth=6, 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.


.. GENERATED FROM PYTHON SOURCE LINES 181-182 Make predictions on the test set .. GENERATED FROM PYTHON SOURCE LINES 182-185 .. code-block:: Python y_val_pred = rf_model.predict(X_val) y_val_prob = rf_model.predict_proba(X_val)[:, 1] .. GENERATED FROM PYTHON SOURCE LINES 186-187 fig, ax = plt.subplots(figsize=(8, 8)) .. GENERATED FROM PYTHON SOURCE LINES 187-193 .. code-block:: Python p = sp.evalplot( x=y_val, y=y_val_pred, kind="all", ) .. image-sg:: /auto_examples/seaborn/images/sphx_glr_plot_evalplot_script_005.png :alt: Classification Report, Confusion Matrix :srcset: /auto_examples/seaborn/images/sphx_glr_plot_evalplot_script_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 194-195 fig, ax = plt.subplots(figsize=(8, 8)) .. GENERATED FROM PYTHON SOURCE LINES 195-204 .. code-block:: Python p = sp.evalplot( x=y_val==1, # y=y_pred, y=y_val_prob, allow_probs=True, # if y_score provided threshold=0.5, kind="all", ) .. image-sg:: /auto_examples/seaborn/images/sphx_glr_plot_evalplot_script_006.png :alt: Classification Report, Confusion Matrix :srcset: /auto_examples/seaborn/images/sphx_glr_plot_evalplot_script_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 205-206 Generate a classification report .. GENERATED FROM PYTHON SOURCE LINES 206-212 .. code-block:: Python print(classification_report(y_val, y_val_pred)) # Generate a confusion matrix conf_matrix = confusion_matrix(y_val, y_val_pred) print(conf_matrix) .. rst-class:: sphx-glr-script-out .. code-block:: none precision recall f1-score support 0 0.89 0.86 0.87 334 1 0.87 0.89 0.88 333 2 0.86 0.86 0.86 333 accuracy 0.87 1000 macro avg 0.87 0.87 0.87 1000 weighted avg 0.87 0.87 0.87 1000 [[288 21 25] [ 14 296 23] [ 23 22 288]] .. GENERATED FROM PYTHON SOURCE LINES 213-214 import seaborn as sns .. GENERATED FROM PYTHON SOURCE LINES 214-224 .. code-block:: Python # plt.figure(figsize=(12, 7)) # sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', # xticklabels=np.arange(15), yticklabels=np.arange(15)) # plt.ylabel('Actual') # plt.xlabel('Predicted') # plt.title('Confusion Matrix') # plt.show() .. GENERATED FROM PYTHON SOURCE LINES 225-233 .. tags:: model-type: classification model-workflow: model evaluation plot-type: line plot-type: eval level: beginner purpose: showcase .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.777 seconds) .. _sphx_glr_download_auto_examples_seaborn_plot_evalplot_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/seaborn/plot_evalplot_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_evalplot_script.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_evalplot_script.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_evalplot_script.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_evalplot_script.zip ` .. include:: plot_evalplot_script.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_