.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/clustering/plot_silhouette_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_clustering_plot_silhouette_script.py: plot_silhouette with examples ============================= An example showing the :py:func:`~scikitplot.api.metrics.plot_silhouette` function used by a scikit-learn clusterer. .. GENERATED FROM PYTHON SOURCE LINES 8-12 .. code-block:: Python # Authors: The scikit-plots developers # SPDX-License-Identifier: BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 13-15 Import scikit-plots ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 15-31 .. code-block:: Python from sklearn.cluster import KMeans from sklearn.datasets import ( load_iris as data_3_classes, ) from sklearn.model_selection import train_test_split import numpy as np np.random.seed(0) # reproducibility # importing pylab or pyplot import matplotlib.pyplot as plt # Import scikit-plot import scikitplot as sp .. GENERATED FROM PYTHON SOURCE LINES 32-34 Loading the dataset ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 34-39 .. code-block:: Python # Load the data X, y = data_3_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) .. GENERATED FROM PYTHON SOURCE LINES 40-42 Model Training -------------- .. GENERATED FROM PYTHON SOURCE LINES 42-48 .. code-block:: Python # Create an instance of the LogisticRegression model = KMeans(n_clusters=4, random_state=0) cluster_labels = model.fit_predict(X_train) .. GENERATED FROM PYTHON SOURCE LINES 49-51 Plot! ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 51-63 .. code-block:: Python # Plot! ax = sp.metrics.plot_silhouette( X_train, cluster_labels, save_fig=True, save_fig_filename="", # overwrite=True, add_timestamp=True, # verbose=True, ) .. image-sg:: /auto_examples/clustering/images/sphx_glr_plot_silhouette_script_001.png :alt: Silhouette Analysis :srcset: /auto_examples/clustering/images/sphx_glr_plot_silhouette_script_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 64-72 .. tags:: model-type: clustering model-workflow: model evaluation plot-type: bar plot-type: silhouette level: beginner purpose: showcase .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.318 seconds) .. _sphx_glr_download_auto_examples_clustering_plot_silhouette_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/clustering/plot_silhouette_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/clustering/plot_silhouette_script.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_silhouette_script.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_silhouette_script.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_silhouette_script.zip ` .. include:: plot_silhouette_script.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_