Introduction

Welcome back to our exploration of clustering algorithms! Today, we'll cover an improved version of the k-means algorithm — the mini-batch k-means. While related to k-means, this variant enhances computational speed and maintains exceptional clustering quality. Let's discuss its Python implementation.

Understanding the Mini-Batch Concept

In machine learning, mini-batches refer to subsets of data that are randomly selected for every algorithm iteration. This approach optimizes computational functions. Specifically for mini-batch k-means, this technique significantly accelerates the clustering process.

Generative Dataset and Preliminaries
Python Mini-Batch K-Means Algorithm

Let's put theory into practice by implementing the mini-batch k-means. The mini_batch_kMeans function accepts the following parameters:

  • data: Our sample dataset contains 2-dimensional coordinates representing the location of each data point.
  • k: The number of clusters our algorithm should identify.
  • iterations: The number of iterations that our algorithm will perform. Each iteration moves the centroids, resulting in increasingly accurate clustering with each step.
  • batch_size: The number of data points randomly selected in each iteration. We maximize computational efficiency by not using the entire dataset in each iteration.

Our mini-batch k-means algorithm starts by initializing the centroids. Then, it enters an iterative process: in each iteration, it randomly selects a mini-batch, calculates Euclidean distances, assigns each point to the closest centroid, and recalculates the centroids based on the currently assigned points.

Python
# Implement mini-batch K-Means
def mini_batch_kMeans(data, k, iterations=10, batch_size=20):
    centers = initialize_centers(data, k)
    for _ in range(iterations):
        idx = np.random.choice(len(data), size=batch_size)
        batch = data[idx, :]
        dists = euclidean_distance(batch[:, None, :], centers[None, :, :])
        labels = np.argmin(dists, axis=1)
        for i in range(k):
            if np.sum(labels == i) > 0:
                centers[i] = np.mean(batch[labels == i], axis=0)
    return centers

centers = mini_batch_kMeans(data, k=2)
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal