.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/snsx/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_snsx_plot_evalplot_script.py: plot_evalplot_script with examples ========================================== An example showing the :py:func:`~scikitplot.snsx.evalplot` function used by a scikit-learn regressor. .. GENERATED FROM PYTHON SOURCE LINES 8-12 .. code-block:: Python :lineno-start: 9 # 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 :lineno-start: 14 import scikitplot.snsx as sp .. GENERATED FROM PYTHON SOURCE LINES 18-30 .. code-block:: Python :lineno-start: 18 import matplotlib.pyplot as plt import numpy as np; np.random.seed(0) # reproducibility import pandas as pd from sklearn.datasets import ( load_breast_cancer as data_2_classes, # load_iris as data_3_classes, ) from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split .. GENERATED FROM PYTHON SOURCE LINES 31-33 Load the data X, y = data_3_classes(return_X_y=True, as_frame=False) .. GENERATED FROM PYTHON SOURCE LINES 33-37 .. code-block:: Python :lineno-start: 33 X, y = data_2_classes(return_X_y=True, as_frame=False) X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.5, random_state=0) np.unique(y) .. rst-class:: sphx-glr-script-out .. code-block:: none array([0, 1]) .. GENERATED FROM PYTHON SOURCE LINES 38-39 Create an instance of the LogisticRegression .. GENERATED FROM PYTHON SOURCE LINES 39-56 .. code-block:: Python :lineno-start: 39 model = ( LogisticRegression(max_iter=int(1e5), 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) # "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(), }) .. GENERATED FROM PYTHON SOURCE LINES 57-65 .. code-block:: Python :lineno-start: 57 p = sp.evalplot( df, x="y_true", y="y_score", kind="all", ) .. image-sg:: /auto_examples/snsx/images/sphx_glr_plot_evalplot_script_001.png :alt: Classification Report, Confusion Matrix :srcset: /auto_examples/snsx/images/sphx_glr_plot_evalplot_script_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 66-74 .. code-block:: Python :lineno-start: 66 p = sp.evalplot( df, x="y_true", y="y_score", kind="classification_report", text_kws={'fontsize': 16}, ) .. image-sg:: /auto_examples/snsx/images/sphx_glr_plot_evalplot_script_002.png :alt: Classification Report :srcset: /auto_examples/snsx/images/sphx_glr_plot_evalplot_script_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 75-82 .. code-block:: Python :lineno-start: 75 p = sp.evalplot( df, x="y_true", y="y_score", kind="confusion_matrix", ) .. image-sg:: /auto_examples/snsx/images/sphx_glr_plot_evalplot_script_003.png :alt: Confusion Matrix :srcset: /auto_examples/snsx/images/sphx_glr_plot_evalplot_script_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 83-92 .. code-block:: Python :lineno-start: 83 fig, ax = plt.subplots(figsize=(8, 6)) p = sp.evalplot( df, x="y_true", y="y_score", kind="all", # legend=True, ) .. image-sg:: /auto_examples/snsx/images/sphx_glr_plot_evalplot_script_004.png :alt: Classification Report, Confusion Matrix :srcset: /auto_examples/snsx/images/sphx_glr_plot_evalplot_script_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 93-101 .. code-block:: Python :lineno-start: 93 import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import make_classification 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 102-103 Create a synthetic dataset with 15 classes .. GENERATED FROM PYTHON SOURCE LINES 103-113 .. code-block:: Python :lineno-start: 103 X, y = make_classification(n_samples=5000, n_features=20, n_classes=15, n_informative=15, n_redundant=5, random_state=0) # Convert to DataFrame for easier visualization (optional) data = pd.DataFrame(X) data['target'] = y print(data.head()) .. rst-class:: sphx-glr-script-out .. code-block:: none 0 1 2 ... 18 19 target 0 -3.249907 -7.609039 3.904290 ... 0.613949 -3.092426 7 1 -1.653727 3.553595 -0.281940 ... -2.332832 -4.067393 3 2 -1.057158 7.627822 0.181669 ... 0.713138 6.743130 11 3 0.734427 -2.012334 -1.798327 ... -4.232499 1.357491 11 4 6.516446 -8.325342 -1.350152 ... 1.410434 13.547467 1 [5 rows x 21 columns] .. GENERATED FROM PYTHON SOURCE LINES 114-115 Split the dataset into training and testing sets .. GENERATED FROM PYTHON SOURCE LINES 115-117 .. code-block:: Python :lineno-start: 115 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0) .. GENERATED FROM PYTHON SOURCE LINES 118-119 Initialize the Random Forest Classifier .. GENERATED FROM PYTHON SOURCE LINES 119-124 .. code-block:: Python :lineno-start: 119 rf_model = RandomForestClassifier(n_estimators=100, random_state=0) # Train the model rf_model.fit(X_train, y_train) .. raw:: html
RandomForestClassifier(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 125-126 Make predictions on the test set .. GENERATED FROM PYTHON SOURCE LINES 126-128 .. code-block:: Python :lineno-start: 126 y_pred = rf_model.predict(X_test) .. GENERATED FROM PYTHON SOURCE LINES 129-137 .. code-block:: Python :lineno-start: 129 fig, ax = plt.subplots(figsize=(8, 8)) p = sp.evalplot( x=y_test, y=y_pred, kind="all", # legend=True, ) .. image-sg:: /auto_examples/snsx/images/sphx_glr_plot_evalplot_script_005.png :alt: Classification Report, Confusion Matrix :srcset: /auto_examples/snsx/images/sphx_glr_plot_evalplot_script_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 138-148 .. code-block:: Python :lineno-start: 138 import scikitplot as sp # Save the combined figure as an image file figs = sp.stack( # experimental p.figure, p.figure, orient='x', **{'figsize': (12, 8)} ) .. image-sg:: /auto_examples/snsx/images/sphx_glr_plot_evalplot_script_006.png :alt: plot evalplot script :srcset: /auto_examples/snsx/images/sphx_glr_plot_evalplot_script_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 149-150 Generate a classification report .. GENERATED FROM PYTHON SOURCE LINES 150-156 .. code-block:: Python :lineno-start: 150 print(classification_report(y_test, y_pred)) # Generate a confusion matrix conf_matrix = confusion_matrix(y_test, y_pred) print(conf_matrix) .. rst-class:: sphx-glr-script-out .. code-block:: none precision recall f1-score support 0 0.48 0.48 0.48 64 1 0.44 0.48 0.46 61 2 0.56 0.52 0.54 65 3 0.55 0.67 0.60 61 4 0.53 0.62 0.57 76 5 0.64 0.45 0.53 80 6 0.64 0.62 0.63 72 7 0.57 0.66 0.61 56 8 0.62 0.62 0.62 65 9 0.54 0.57 0.56 68 10 0.67 0.60 0.63 70 11 0.54 0.55 0.54 69 12 0.63 0.53 0.58 77 13 0.56 0.45 0.50 67 14 0.41 0.53 0.46 49 accuracy 0.56 1000 macro avg 0.56 0.56 0.55 1000 weighted avg 0.56 0.56 0.56 1000 [[31 2 3 3 2 3 3 2 2 4 1 3 0 1 4] [ 5 29 3 3 1 2 2 2 2 2 1 3 2 3 1] [ 0 2 34 3 3 1 0 3 2 1 3 1 5 2 5] [ 3 1 1 41 1 1 0 1 2 1 0 2 1 2 4] [ 5 4 1 3 47 1 1 2 2 2 1 2 3 2 0] [ 3 5 1 3 7 36 0 6 2 5 1 9 0 1 1] [ 0 1 2 3 7 1 45 2 0 1 0 4 2 1 3] [ 1 0 1 2 3 3 3 37 1 1 0 0 1 3 0] [ 6 3 0 3 1 1 2 1 40 4 0 2 1 0 1] [ 1 1 1 4 3 2 3 0 1 39 4 1 0 1 7] [ 1 1 7 1 4 0 3 0 1 2 42 1 3 2 2] [ 1 4 3 0 2 2 4 0 2 5 1 38 3 1 3] [ 1 5 1 1 3 2 1 2 2 2 6 5 41 3 2] [ 5 4 3 0 3 1 2 5 4 3 2 0 0 30 5] [ 2 4 0 5 2 0 1 2 1 0 1 0 3 2 26]] .. GENERATED FROM PYTHON SOURCE LINES 157-158 import seaborn as sns .. GENERATED FROM PYTHON SOURCE LINES 158-168 .. code-block:: Python :lineno-start: 159 # 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 169-177 .. 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 3.563 seconds) .. _sphx_glr_download_auto_examples_snsx_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/main?urlpath=lab/tree/notebooks/auto_examples/snsx/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/snsx/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 `_