plot_elbow with examples#
An example showing the plot_elbow
function
used by a scikit-learn clusterer.
9 # Authors: The scikit-plots developers
10 # SPDX-License-Identifier: BSD-3-Clause
Load the dataset#
We will start by loading the iris
dataset.
18 from sklearn.cluster import KMeans
19 from sklearn.datasets import (
20 load_iris as data_3_classes,
21 )
22 from sklearn.model_selection import train_test_split
23
24 import numpy as np
25
26 np.random.seed(0) # reproducibility
27 # importing pylab or pyplot
28 import matplotlib.pyplot as plt
29
30 # Import scikit-plot
31 import scikitplot as sp
Loading the dataset#
37 # Load the data
38 X, y = data_3_classes(return_X_y=True, as_frame=False)
39 X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.5, random_state=0)
Model Training#
Create an instance of the LogisticRegression
46 model = KMeans(n_clusters=4, random_state=1)
Visualize the results#
Plot!
54 ax = sp.estimators.plot_elbow(
55 model,
56 X_train,
57 cluster_ranges=range(1, 11),
58 save_fig=True,
59 save_fig_filename="",
60 # overwrite=True,
61 add_timestamp=True,
62 # verbose=True,
63 )

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