plot_silhouette with examples#
An example showing the plot_silhouette
function
used by a scikit-learn clusterer.
9 # Authors: The scikit-plots developers
10 # SPDX-License-Identifier: BSD-3-Clause
Import scikit-plots#
16 from sklearn.cluster import KMeans
17 from sklearn.datasets import (
18 load_iris as data_3_classes,
19 )
20 from sklearn.model_selection import train_test_split
21
22 import numpy as np
23
24 np.random.seed(0) # reproducibility
25 # importing pylab or pyplot
26 import matplotlib.pyplot as plt
27
28 # Import scikit-plot
29 import scikitplot as sp
Loading the dataset#
35 # Load the data
36 X, y = data_3_classes(return_X_y=True, as_frame=False)
37 X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.5, random_state=0)
Model Training#
43 # Create an instance of the LogisticRegression
44 model = KMeans(n_clusters=4, random_state=0)
45
46 cluster_labels = model.fit_predict(X_train)
Plot!#
52 # Plot!
53 ax = sp.metrics.plot_silhouette(
54 X_train,
55 cluster_labels,
56 save_fig=True,
57 save_fig_filename="",
58 # overwrite=True,
59 add_timestamp=True,
60 # verbose=True,
61 )

Total running time of the script: (0 minutes 0.303 seconds)
Related examples