plot_elbow#

scikitplot.api.estimators.plot_elbow(clf, X, title='Elbow Curves', cluster_ranges=None, n_jobs=1, show_cluster_time=True, ax=None, fig=None, figsize=None, title_fontsize='large', text_fontsize='medium')[source]#

Plot the elbow curve for different values of K in KMeans clustering.

Parameters:
clfobject

A clusterer instance with fit, fit_predict, and score methods, and an n_clusters hyperparameter. Typically an instance of sklearn.cluster.KMeans.

Xarray-like of shape (n_samples, n_features)

The data to cluster, where n_samples is the number of samples and n_features is the number of features.

titlestr, optional, default=”Elbow Plot”

The title of the generated plot.

cluster_rangeslist of int or None, optional, default=range(1, 12, 2)

List of values for n_clusters over which to plot the explained variances.

n_jobsint, optional, default=1

The number of jobs to run in parallel.

show_cluster_timebool, optional

Whether to include a plot of the time taken to cluster for each value of K.

axmatplotlib.axes.Axes, optional

The axes on which to plot the curve. If None, a new set of axes will be created.

figsizetuple, optional

Tuple denoting the figure size of the plot, e.g., (6, 6). If None, the default size will be used.

title_fontsizestr or int, optional, default=”large”

Font size of the title. Accepts Matplotlib font sizes, such as “small”, “medium”, “large”, or an integer value.

text_fontsizestr or int, optional, default=”medium”

Font size of the text labels. Accepts Matplotlib font sizes, such as “small”, “medium”, “large”, or an integer value.

Returns:
axmatplotlib.axes.Axes

The axes on which the plot was drawn.

Examples

>>> from sklearn.cluster import KMeans
>>> from sklearn.datasets import load_iris as data_3_classes
>>> import scikitplot as skplt
>>> X, y = data_3_classes(return_X_y=True, as_frame=False)
>>> kmeans = KMeans(random_state=0)
>>> skplt.estimators.plot_elbow(
>>>     kmeans,
>>>     X,
>>>     cluster_ranges=range(1, 10),
>>> );

(Source code, png)

Elbow Curve