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.
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:
- Select the Number of Clusters (`k`): The algorithm starts with a predetermined number of clusters, `k`.
- Initialize Centroids: Initially, it selects `k` cluster centroids randomly from the dataset. These centroids are pivotal for the initial phase of clustering.
- 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.
- 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.
- 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:
where: • represents the cluster `i`, • is the centroid of the cluster `i`, • 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
- Python K-means fails to fit data when over 100 samples
- Python Keras An layer output exactly the same thing as input
- Python Keras LSTM learning converges too fast on high loss
- Python model.fit error, None values not supported
- Python kernel dies on Jupyter Notebook with tensorflow 2
- Python memory usage of numpy arrays
- Python Kafka multiprocess vs thread
- Python kernel dies when importing tensorflow 1.7
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.