Cluster Analysis for Machine Learning

Last modified: 2023-08-20

Clustering Machine Learning

We can find the number of clusters using methods such as K-means.

Find Optimal Number of Clusters

K-means & Elbow Curve


We may find the optimal number of clusters by using K-means algorithm and observing the Elbow graph.

import numpy as np
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

clusters = np.load("example.npy")

# specify the range of the number of clusters
K = range(1, 10)

distortions = []
for i in K:
	kmeans = KMeans(n_clusters=i)

plt.plot(K, distortions)
plt.xlabel("Number of clusters")

Seeing the output graph, the last point where the distortion (or inertia) drops sharply may be the optimal number of clusters.