This is a simple example of Gaussian Mixture Model (GMM) using Python and scikit-learn.
Gaussian Mixture Models (GMMs) are probabilistic models used for clustering and density estimation. Unlike k-means, which assumes spherical clusters, GMMs model data as a mixture of multiple Gaussian distributions with different means and covariances. Each data point has a probability of belonging to each cluster, and GMMs can handle clusters with different shapes and sizes.
Key concepts of Gaussian Mixture Models:
GMMs are versatile and can be applied to various tasks, including clustering, anomaly detection, and generating synthetic data.
Python Source Code:
# Import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.mixture import GaussianMixture
from sklearn.metrics import silhouette_score
# Generate synthetic data with three clusters
np.random.seed(42)
X, _ = make_blobs(n_samples=300, centers=3, random_state=42)
# Build a GMM model
gmm = GaussianMixture(n_components=3, random_state=42)
gmm.fit(X)
# Make predictions on the data
labels = gmm.predict(X)
# Calculate silhouette score for model evaluation
silhouette_avg = silhouette_score(X, labels)
print(f'Silhouette Score: {silhouette_avg}')
# Plot the results
plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', marker='o', edgecolors='black')
plt.scatter(gmm.means_[:, 0], gmm.means_[:, 1], marker='X', s=200, color='red', label='Cluster Centers')
plt.title('Gaussian Mixture Model (GMM) Example')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.legend()
plt.show()
Explanation:
make_blobs
function from scikit-learn.GaussianMixture
.