Python
k-means
clustering
machine learning
data science

Python k-means algorithm

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

K-means is one of the simplest and most popular unsupervised machine learning algorithms that solve the well-known clustering problem. The procedure follows a straightforward way to classify a given data set into a certain number of clusters (assume k clusters) fixed initially. The aim is to define k centroids, one for each cluster. This article dives into the intricacies of the k-means algorithm, exploring its internal mechanisms, applications, and limitations, along with a Python implementation.

Understanding the K-means Algorithm

How K-means Works

The K-means algorithm can be broken down into the following steps:

  1. Select the Number of Clusters (`k`): The algorithm starts with a predetermined number of clusters, `k`.
  2. Initialize Centroids: Initially, it selects `k` cluster centroids randomly from the dataset. These centroids are pivotal for the initial phase of clustering.
  3. Assign Data Points to Centroids: Next, each data point is allocated to the nearest centroid. Typically, distance measures such as Euclidean, Manhattan, or others can be used to determine the "closeness" of data points to centroids.
  4. Update Centroids: Once all data points are assigned, calculate the new centroid by averaging all the data points in each cluster. The updated centroid is the new mean of the data points within a cluster.
  5. Iterate Until Convergence: Repeat the previous two steps until the centroids no longer change significantly between iterations or a specified number of iterations is reached.

Mathematical Explanation

The K-means algorithm seeks to partition the `n` observations into `k` clusters in which each observation belongs to the cluster with the nearest mean, serving as a prototype for the cluster. The algorithm minimizes the within-cluster sum of squares (WCSS), also known as inertia:

WCSS=_i=1k_xC_ixμ_i2\text{WCSS} = \sum\_{i=1}^{k} \sum\_{x \in C\_i} ||x - \mu\_i||^2

where: • CiC_i represents the cluster `i`, • μi\mu_i is the centroid of the cluster `i`, • xμi2||x - \mu_i||^2 is the squared Euclidean distance between a data point `x` and the centroid `\mu_i`.

Python Implementation

Below is a simple Python implementation using the `scikit-learn` library:

Image Compression: By reducing the number of colors in an image while preserving its visual integrity, K-means can efficiently compress images. • Customer Segmentation: Businesses can use K-means to segment customers into different groups for targeted marketing. • Anomaly Detection: It serves as a baseline for anomaly detection by identifying data points that don't fit well into any cluster. • Choice of `k`: Selecting the optimal number of clusters `k` is a non-trivial task and often requires domain knowledge or experimentation. • Sensitivity to Initialization: Poor initialization can lead to suboptimal clusters. • Assumption of Spherical Clusters: K-means assumes clusters to be spherical, which may not be the case in real-world data. • K-means++: An adjustment method to improve the initialization of the centroids, reducing the likelihood of poor clusterings. • Bisecting K-means: This approach reduces computational cost and can handle large datasets more efficiently.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.