A Guide to K-Means Clustering in Python | Towards Data Science

K-means clustering is an unsupervised ML algorithm that we can use to split our dataset into logical groupings — called clusters. Because it is unsupervised, we don’t need to rely on having labeled data to train with.
These clusters are created by splitting the data into clearly distinct groups where the values that make up each group are similar — and the values between different groups are different.
The K in K-means refers to the number of clusters. The clustering mechanism itself works by labeling each datapoint in our dataset to a random cluster. We then loop through a process of:
Taking the mean value of all datapoints in each clusterSetting this mean value as the new cluster center (centroid)Re-labeling each data point to its closest cluster centroid.
Taking the mean value of all datapoints in each cluster
Setting this mean value as the new cluster center (centroid)
Re-labeling each data point to its closest cluster centroid.
We continue this process until the centroids stop moving — which means we have successfully clustered our dataset!
There’s a huge range of applications for this algorithm, a few of the most common and more interesting include:
Document clustering (e.g., policies, regulations, tech specifications)
Market segmentation
Fraud detection [1]
Identifying crime hot-spots [2]
We will be using a simple dataset in this article. However, K-Means can be used effectively on incredibly rich and diverse datasets — some with dozens of features and millions of samples.
We will be using Sci-kit Learn to implement K-means. But before we do that, we need data. Here we can use Sci-kit Learn’s make_blobs function to generate a given number of artificially generated clusters:
from sklearn.datasets import make_blobsX, y = make_blobs(n_samples=500, n_features=3, centers=5,                  cluster_std=2)
And with that, inside the variable X we now have our artificial data containing 500 datapoints, each consisting of 3 features/dimensions, and each belonging to one of 5 blobs. Variable y tells us which cluster each datapoint belongs to — usually, we would not know this.
Now we have our data; we can run K-Means to cluster it! Again we use sci-kit learn:
from sklearn.cluster import KMeansmodel = KMeans(n_clusters=5)
Here we have initialized our K-Means model with 5 cluster centroids. We can then run our model with our data X using the fit method:
model.fit(X)
Now we can access our data classification and centroid coordinates using the labels_ and cluster_centers_ methods respectively.
When plotting the centroids and labeled datapoints, we can see that the algorithm has identified five clusters:
The one part missing from our understanding of K-Means is how to choose K?
By far, the most popular approach is the Elbow Method. Fortunately, it’s incredibly simple too.
First, we need to run our algorithm with some value of K — once complete, we can pull the sum of squared error (SSE) for our X-values from the intertia_ attribute:
sse = model.inertia_
SSE is calculated as the sum of the squared distance between each datapoint and its allocated cluster centroid. If all datapoints are tightly congregated around their allocated centroid, then the SSE will be low — otherwise, it will be high.
It is important that we have enough clusters to match the clusters in our dataset, but not too many clusters than the SSE is minimized simply by assigning every datapoint it’s own cluster.
sse = []for K in range(1, 10):    model = KMeans(n_clusters=K)    model.fit(X)    sse.append(model.inertia_)
We need to find the magic spot where we correctly identify the most logical number of clusters. This spot can be identified by calculating the SSE for a range of K values and identifying the ‘elbow’ in the graph shown above.
That’s it for this introduction to cluster with the K-Means algorithm! We’ve covered:
What K-Means is
Where it’s used
How we can use it
Choosing K with the elbow method
I hope you enjoyed this article — if you have any questions or ideas, let me know via Twitter or in the comments below.
Thanks for reading!
[1] A. Ghorbani, S. Farzai, Fraud Detection in Automobile Insurance using a Data Mining Base Approach (2018), IJMEC
[2] M. Zulfadhilah et al., Cyber Profiling using Log Analysis and K-Means Clustering (2016), IJACSA
*All images are by the author except where stated otherwise