Python
K-means
Data Clustering
Machine Learning
Data Analysis

Python K-means fails to fit data when over 100 samples

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

Understanding Python K-means and Its Limitations with Large Datasets

K-means clustering is one of the most popular unsupervised machine learning algorithms used to partition datasets into clusters, based on feature similarity. Given its simplicity and efficiency in handling small and moderately sized datasets, it's widely used in data science. However, some users have reported issues with K-means failing to fit the data accurately when the sample size exceeds 100 samples. This article examines the causes of such failures, providing technical insights and potential solutions.

How K-means Works

K-means aims to partition `n` observations into `k` clusters, where each observation belongs to the cluster with the nearest mean. The algorithm typically proceeds through the following steps:

  1. Initialization: Randomly select `k` initial centroids.
  2. Assignment Step: Assign each data point to the nearest centroid, forming `k` clusters.
  3. Update Step: Calculate the new centroids as the mean of all points in each cluster.
  4. Convergence: Repeat the assignment and update steps until convergence (i.e., centroids no longer move or move negligibly).

The algorithm's iterative nature helps to minimize the within-cluster sum of squares (WCSS), effectively compacting the data points within clusters.

Limitations in Large Datasets

In practice, some issues may arise when employing K-means with larger datasets (e.g., over 100 samples):

  1. Initialization Sensitivity: The algorithm's performance significantly depends on the initial selection of centroids. With larger datasets, poor initialization can lead to suboptimal clustering.
  2. Scalability: The time complexity of each K-means iteration is O(nkm)O(n \cdot k \cdot m), where `n` is the number of data points, `k` is the number of clusters, and `m` is the number of features. As `n` increases, the algorithm may become sluggish or require more computational resources.
  3. Curse of Dimensionality: As the dataset grows in size, maintaining meaningful distance metrics between points becomes challenging due to the curse of dimensionality, potentially leading to less intuitive clustering outcomes.
  4. Convergence Issues: With an increase in data points, K-means can become trapped in local optima, failing to find the global optimum.

Example Problem with Large Dataset

Consider a scenario where the dataset consists of 150 samples, each with 3 features. Using the Python library `scikit-learn`, one attempts to cluster the data as follows:

  • K-means++ Initialization: Using `k-means++` ensures a smarter selection of initial centroids, helping avoid poor clustering outcomes.
  • Dimensionality Reduction: Techniques like PCA (Principal Component Analysis) can reduce the feature space's dimensionality, alleviating the curse of dimensionality.
  • Mini-batch K-means: This variation of K-means uses small subsets of the data to update centroids incrementally, making it more scalable and faster for larger datasets.
  • Multiple Initializations: Setting `n_init` to a higher value in `scikit-learn` conducts multiple K-means runs, returning the best clustering solution among them.

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.