.. 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-48 .. 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 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 49-52 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 52-59 .. code-block:: Python :lineno-start: 53 # 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 60-65 .. code-block:: Python :lineno-start: 60 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 66-67 Create an instance of the LogisticRegression .. GENERATED FROM PYTHON SOURCE LINES 67-94 .. code-block:: Python :lineno-start: 67 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 95-106 .. code-block:: Python :lineno-start: 95 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/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 107-115 .. code-block:: Python :lineno-start: 107 p = sp.evalplot( df, x="y_true", y="y_pred", 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 116-123 .. code-block:: Python :lineno-start: 116 p = sp.evalplot( df, x="y_true", y="y_pred", 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 124-125 fig, ax = plt.subplots(figsize=(8, 6)) .. GENERATED FROM PYTHON SOURCE LINES 125-135 .. code-block:: Python :lineno-start: 125 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/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 136-149 .. code-block:: Python :lineno-start: 136 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 150-153 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 153-160 .. code-block:: Python :lineno-start: 154 # 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.97, 0.03], random_state=0) .. GENERATED FROM PYTHON SOURCE LINES 161-166 .. code-block:: Python :lineno-start: 161 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 167-168 Initialize the Random Forest Classifier .. GENERATED FROM PYTHON SOURCE LINES 168-178 .. code-block:: Python :lineno-start: 168 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 179-180 Make predictions on the test set .. GENERATED FROM PYTHON SOURCE LINES 180-183 .. code-block:: Python :lineno-start: 180 y_val_pred = rf_model.predict(X_val) y_val_prob = rf_model.predict_proba(X_val)[:, 1] .. GENERATED FROM PYTHON SOURCE LINES 184-185 fig, ax = plt.subplots(figsize=(8, 8)) .. GENERATED FROM PYTHON SOURCE LINES 185-191 .. code-block:: Python :lineno-start: 185 p = sp.evalplot( x=y_val, y=y_val_pred, kind="all", ) .. 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 192-193 fig, ax = plt.subplots(figsize=(8, 8)) .. GENERATED FROM PYTHON SOURCE LINES 193-202 .. code-block:: Python :lineno-start: 193 p = sp.evalplot( x=y_val, # y=y_pred, y=y_val_prob, allow_probs=True, # if y_score provided threshold=0.5, kind="all", ) .. image-sg:: /auto_examples/snsx/images/sphx_glr_plot_evalplot_script_006.png :alt: Classification Report, Confusion Matrix :srcset: /auto_examples/snsx/images/sphx_glr_plot_evalplot_script_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 203-204 Generate a classification report .. GENERATED FROM PYTHON SOURCE LINES 204-210 .. code-block:: Python :lineno-start: 204 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.97 1.00 0.99 966 1 0.73 0.24 0.36 34 accuracy 0.97 1000 macro avg 0.85 0.62 0.67 1000 weighted avg 0.97 0.97 0.96 1000 [[963 3] [ 26 8]] .. GENERATED FROM PYTHON SOURCE LINES 211-212 import seaborn as sns .. GENERATED FROM PYTHON SOURCE LINES 212-222 .. code-block:: Python :lineno-start: 213 # 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 223-231 .. 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.437 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/maintenance/0.4.X?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 `_