import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans, AgglomerativeClustering
from sklearn.metrics import silhouette_score
from scipy.cluster.hierarchy import dendrogram, linkage
# Step 1: Generate a synthetic dataset
data, labels_true = make_blobs(n_samples=300, centers=4, cluster_std=1.0, random_state=42)
# Step 2: Apply K-Means Clustering
kmeans = KMeans(n_clusters=4, random_state=42)
kmeans_labels = kmeans.fit_predict(data)
kmeans_silhouette = silhouette_score(data, kmeans_labels)
# Step 3: Apply Hierarchical Clustering
hierarchical = AgglomerativeClustering(n_clusters=4)
hierarchical_labels = hierarchical.fit_predict(data)
hierarchical_silhouette = silhouette_score(data, hierarchical_labels)
# Step 4: Visualize Results
fig, axes = plt.subplots(1, 3, figsize=(18, 6))
# Original Data
axes[0].scatter(data[:, 0], data[:, 1], c=labels_true, cmap='viridis', s=50, alpha=0.6)
axes[0].set_title("Original Data")
# K-Means Clustering
axes[1].scatter(data[:, 0], data[:, 1], c=kmeans_labels, cmap='viridis', s=50, alpha=0.6)
axes[1].set_title(f"K-Means Clustering\nSilhouette Score: {kmeans_silhouette:.2f}")
# Hierarchical Clustering
axes[2].scatter(data[:, 0], data[:, 1], c=hierarchical_labels, cmap='viridis', s=50, alpha=0.6)
axes[2].set_title(f"Hierarchical Clustering\nSilhouette Score: {hierarchical_silhouette:.2f}")
plt.tight_layout()
plt.show()
# Step 5: Create a Dendrogram for Hierarchical Clustering
linked = linkage(data, method='ward')
plt.figure(figsize=(10, 7))
dendrogram(linked, truncate_mode='lastp', p=12, leaf_rotation=45., leaf_font_size=15., show_contracted=True)
plt.title("Hierarchical Clustering Dendrogram")
plt.xlabel("Sample index or Cluster size")
plt.ylabel("Distance")
plt.show()
# Summary
print("Summary of Clustering Results:\n")
print(f"K-Means Silhouette Score: {kmeans_silhouette:.2f}")
print(f"Hierarchical Clustering Silhouette Score: {hierarchical_silhouette:.2f}")
aW1wb3J0IG51bXB5IGFzIG5wCmltcG9ydC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