xbe.at

Machine learning / Classification and clustering

Comparing K-Means and Gaussian Mixture Models for Clustering in Python

5 min read, 12 sections

Contents
  1. Introduction to Clustering Algorithms
  2. K-Means Clustering
  3. K-Means Clustering Visualization
  4. Gaussian Mixture Models (GMM)
  5. GMM Visualization
  6. Choosing the Number of Clusters
  7. Advantages of K-Means
  8. Disadvantages of K-Means
  9. Advantages of GMM
  10. Disadvantages of GMM
  11. Choosing Between K-Means and GMM
  12. Conclusion

Introduction to Clustering Algorithms

Clustering is an unsupervised machine learning technique that groups similar data points together based on their features. Two popular clustering algorithms are K-Means and Gaussian Mixture Models (GMM). In this slideshow, we'll explore the differences between these two algorithms and implement them in Python.

K-Means Clustering

K-Means is a simple and popular clustering algorithm that partitions data points into K clusters based on their distances from cluster centroids. The algorithm iteratively assigns data points to the nearest centroid and updates the centroids until convergence.

from sklearn.cluster import KMeans
import numpy as np

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

# Create a KMeans instance with 2 clusters
kmeans = KMeans(n_clusters=2, random_state=0)

# Fit the model and get cluster labels
labels = kmeans.fit_predict(X)

# Print cluster labels
print(labels)

K-Means Clustering Visualization

To visualize the K-Means clustering results, we can use a scatter plot and color the data points based on their assigned cluster labels.

import matplotlib.pyplot as plt

# Plot the data points
plt.scatter(X[:, 0], X[:, 1], c=labels)
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=100, c='red')
plt.title('K-Means Clustering')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.show()

Gaussian Mixture Models (GMM)

GMM is a probabilistic model that assumes that the data points are generated from a mixture of Gaussian distributions. It assigns data points to clusters based on their likelihood of belonging to each Gaussian component.

from sklearn.mixture import GaussianMixture
import numpy as np

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

# Create a GMM instance with 2 components
gmm = GaussianMixture(n_components=2, random_state=0)

# Fit the model and get cluster labels
labels = gmm.fit_predict(X)

# Print cluster labels
print(labels)

GMM Visualization

Similar to K-Means, we can visualize the GMM clustering results using a scatter plot and color the data points based on their assigned cluster labels.

import matplotlib.pyplot as plt

# Plot the data points
plt.scatter(X[:, 0], X[:, 1], c=labels)
plt.title('Gaussian Mixture Model Clustering')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.show()

Choosing the Number of Clusters

Both K-Means and GMM require specifying the number of clusters (K or n_components) beforehand. To choose an appropriate number, we can use techniques like the Elbow method for K-Means or the Bayesian Information Criterion (BIC) for GMM.

from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

# Sample data
X = ...

# Elbow method for K-Means
inertias = []
for k in range(1, 11):
    kmeans = KMeans(n_clusters=k, random_state=0)
    kmeans.fit(X)
    inertias.append(kmeans.inertia_)

# Plot the elbow curve
plt.plot(range(1, 11), inertias)
plt.xlabel('Number of Clusters')
plt.ylabel('Inertia')
plt.show()

Advantages of K-Means

K-Means is a simple and efficient algorithm that works well with compact and well-separated clusters. It is easy to understand and implement, and it has a linear time complexity with respect to the number of data points.

from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt

# Generate sample data
X, y = make_blobs(n_samples=1000, centers=3, n_features=2, random_state=0)

# Create a KMeans instance and fit the model
kmeans = KMeans(n_clusters=3, random_state=0)
labels = kmeans.fit_predict(X)

# Plot the results
plt.scatter(X[:, 0], X[:, 1], c=labels)
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=100, c='red')
plt.title('K-Means Clustering on Well-Separated Blobs')
plt.show()

Disadvantages of K-Means

K-Means has limitations when dealing with non-convex or overlapping clusters. It is sensitive to outliers and the initial centroid positions, and it cannot handle clusters of different sizes or densities well.

from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt

# Generate sample data with non-convex clusters
X, y = make_blobs(n_samples=1000, centers=3, cluster_std=[1.0, 2.5, 0.5], random_state=0)

# Create a KMeans instance and fit the model
kmeans = KMeans(n_clusters=3, random_state=0)
labels = kmeans.fit_predict(X)

# Plot the results
plt.scatter(X[:, 0], X[:, 1], c=labels)
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=100, c='red')
plt.title('K-Means Clustering on Non-Convex Clusters')
plt.show()

Advantages of GMM

GMM is a more flexible and robust algorithm that can handle non-convex and overlapping clusters. It can model clusters with different sizes, densities, and covariance structures, making it suitable for complex data distributions.

from sklearn.datasets import make_blobs
from sklearn.mixture import GaussianMixture
import matplotlib.pyplot as plt

# Generate sample data with overlapping clusters
X, y = make_blobs(n_samples=1000, centers=3, cluster_std=1.5, random_state=0)

# Create a GMM instance and fit the model
gmm = GaussianMixture(n_components=3, random_state=0)
labels = gmm.fit_predict(X)

# Plot the results
plt.scatter(X[:, 0], X[:, 1], c=labels)
plt.title('Gaussian Mixture Model Clustering on Overlapping Clusters')
plt.show()

Disadvantages of GMM

GMM is computationally more expensive than K-Means, especially for large datasets and high-dimensional data. It can be sensitive to initialization and may converge to local optima, leading to suboptimal solutions.

from sklearn.datasets import make_blobs
from sklearn.mixture import GaussianMixture
import time

# Generate a large sample dataset
X, y = make_blobs(n_samples=100000, centers=5, n_features=10, random_state=0)

# Time the fitting process for GMM
start_time = time.time()
gmm = GaussianMixture(n_components=5, random_state=0)
gmm.fit(X)
end_time = time.time()

print(f"Time taken to fit GMM on large dataset: {end_time - start_time:.2f} seconds")

Choosing Between K-Means and GMM

When choosing between K-Means and GMM, consider the characteristics of your data and the desired clustering behavior. K-Means is a good choice for simple and well-separated clusters, while GMM is more suitable for complex and overlapping clusters or when you need to model different cluster shapes and densities.

from sklearn.datasets import make_blobs, make_moons
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.mixture import GaussianMixture

# Generate well-separated blobs
X_blobs, y_blobs = make_blobs(n_samples=1000, centers=3, n_features=2, random_state=0)

# Generate overlapping moons
X_moons, y_moons = make_moons(n_samples=1000, noise=0.05, random_state=0)

# Cluster the blobs using K-Means
kmeans = KMeans(n_clusters=3, random_state=0)
labels_blobs = kmeans.fit_predict(X_blobs)

# Cluster the moons using GMM
gmm = GaussianMixture(n_components=2, random_state=0)
labels_moons = gmm.fit_predict(X_moons)

# Plot the results
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))

ax1.scatter(X_blobs[:, 0], X_blobs[:, 1], c=labels_blobs)
ax1.set_title('K-Means Clustering on Well-Separated Blobs')

ax2.scatter(X_moons[:, 0], X_moons[:, 1], c=labels_moons)
ax2.set_title('Gaussian Mixture Model Clustering on Overlapping Moons')

plt.show()

Conclusion

In this slideshow, we explored the differences between K-Means and Gaussian Mixture Models for clustering in Python. Both algorithms have their strengths and weaknesses, and the choice depends on the specific problem and data characteristics. Understanding these algorithms and their implementation is essential for effective data analysis and clustering tasks.

More in Classification and clustering

  1. Condensing Random Forest into Single Decision Tree7 min

    Condensing a random forest model into a single decision tree is a technique used to simplify complex ensemble models while retaining their predictive power....

  2. Comparing Elbow Curve and Silhouette Analysis for KMeans Clustering in Python11 min

    KMeans is a popular unsupervised machine learning algorithm used for clustering data points into distinct groups. It aims to partition n observations into k...

  3. Conditional Independence Assumption in Naive Bayes9 min

    Naive Bayes assumes that features are conditionally independent given the class label, meaning the presence of one feature does not affect the presence of...

  4. Comparing Decision Trees and Random Forests11 min

    Decision trees and random forests are both powerful machine learning algorithms used for classification and regression tasks. While they share some...

  5. Decision Tree in Machine Learning Using Python9 min

    Decision Trees are a type of supervised learning algorithm used for both classification and regression tasks. They are tree-like models where each internal...

  6. Compact Clustering with Hierarchical Linkage Methods in Python8 min

    Hierarchical clustering is a popular unsupervised learning technique used to group similar data points into clusters. The choice of linkage method in...