K-Means Clustering

K-Means Clustering

K-Means Clustering is one of the most popular unsupervised machine learning algorithms used for partitioning a dataset into distinct groups. It operates by finding clusters in data points based on feature similarity, optimizing the position of cluster centers (centroids).

Key Concepts

What is Clustering?

Clustering is the process of grouping a set of objects in such a way that objects in the same group (or cluster) are more similar to each other than to those in other groups. K-Means is one of the simplest and most efficient clustering techniques.

How K-Means Works

1. Initialization: Choose the number of clusters (K) and randomly initialize K centroids. 2. Assignment Step: Assign each data point to the nearest centroid, forming K clusters. 3. Update Step: Recalculate the centroids as the mean of all data points assigned to each cluster. 4. Repeat: Repeat the Assignment and Update steps until the centroids no longer change significantly.

Algorithm Steps

The K-Means algorithm can be summarized in the following steps:

1. Select K: Choose the number of clusters (K). 2. Randomly initialize centroids: Select K data points at random as the initial centroids. 3. Assign data points: For each data point, calculate the distance to each centroid and assign the point to the nearest centroid. 4. Update centroids: Calculate the new centroids as the average of all points in each cluster. 5. Convergence check: Check if the centroids have changed significantly. If not, the algorithm converges, and the process stops.

Example

Let’s say we have the following 2D dataset:

| Point | X | Y | |-------|----|----| | A | 1 | 2 | | B | 1 | 4 | | C | 1 | 0 | | D | 10 | 2 | | E | 10 | 4 | | F | 10 | 0 |

If we set K = 2, the algorithm might initialize the centroids at points A and D. After several iterations, we might end up with two clusters: - Cluster 1: Points A, B, C - Cluster 2: Points D, E, F

Visualizing this can help in understanding how the points are grouped based on their distance from the centroids.

Advantages and Disadvantages

Advantages:

- Simple and easy to implement. - Fast and efficient for large datasets. - Works well when clusters are spherical.

Disadvantages:

- Requires the number of clusters (K) to be defined beforehand. - Sensitive to initial centroid placement. - Cannot handle non-spherical clusters effectively.

Practical Applications

- Market segmentation: Grouping customers based on purchasing behavior. - Image compression: Reducing the number of colors in an image. - Document clustering: Grouping similar documents for information retrieval.

Conclusion

K-Means is a foundational algorithm in machine learning that helps in understanding how to group data based on similarity. It serves as a stepping stone for more advanced clustering techniques and is widely used in various fields.

Code Example

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

`python import numpy as np import matplotlib.pyplot as plt from sklearn.cluster import KMeans

Sample data points

X = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]])

Create KMeans instance with 2 clusters

kmeans = KMeans(n_clusters=2)

Fit the model

kmeans.fit(X)

Get cluster centroids and labels

centroids = kmeans.cluster_centers_ labels = kmeans.labels_

Plotting the data points and centroids

plt.scatter(X[:, 0], X[:, 1], c=labels, s=50, cmap='viridis') plt.scatter(centroids[:, 0], centroids[:, 1], c='red', s=200, alpha=0.75) plt.title('K-Means Clustering') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.show() ` This code will visualize the data points along with the centroids of the clusters formed by K-Means.

Back to Course View Full Topic