0
0
ML Pythonml~20 mins

Why advanced clustering finds complex structures in ML Python - Experiment to Prove It

Choose your learning style9 modes available
Experiment - Why advanced clustering finds complex structures
Problem:We want to group data points into clusters that reflect complex shapes, not just simple round groups.
Current Metrics:Using KMeans clustering, the model groups data with 70% accuracy on complex shapes.
Issue:KMeans assumes clusters are round and struggles with complex shapes, leading to poor grouping accuracy.
Your Task
Improve clustering accuracy on complex-shaped data from 70% to at least 85% by using an advanced clustering method.
You must keep the same dataset with complex shapes.
You cannot use supervised learning methods.
You should not change the number of clusters.
Hint 1
Hint 2
Hint 3
Solution
ML Python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_moons
from sklearn.cluster import KMeans, DBSCAN
from sklearn.metrics import adjusted_rand_score

# Create complex shape data (two moons)
X, y_true = make_moons(n_samples=300, noise=0.05, random_state=42)

# KMeans clustering (baseline)
kmeans = KMeans(n_clusters=2, random_state=42)
y_kmeans = kmeans.fit_predict(X)

# DBSCAN clustering (advanced)
dbscan = DBSCAN(eps=0.2, min_samples=5)
y_dbscan = dbscan.fit_predict(X)

# Calculate clustering accuracy using Adjusted Rand Index (ARI)
ari_kmeans = adjusted_rand_score(y_true, y_kmeans) * 100
ari_dbscan = adjusted_rand_score(y_true, y_dbscan) * 100

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

axs[0].scatter(X[:, 0], X[:, 1], c=y_kmeans, cmap='viridis')
axs[0].set_title(f'KMeans Clustering (ARI: {ari_kmeans:.1f}%)')

axs[1].scatter(X[:, 0], X[:, 1], c=y_dbscan, cmap='viridis')
axs[1].set_title(f'DBSCAN Clustering (ARI: {ari_dbscan:.1f}%)')

plt.show()
Replaced KMeans with DBSCAN to capture complex cluster shapes.
Tuned DBSCAN parameters eps=0.2 and min_samples=5 for better grouping.
Used Adjusted Rand Index to measure clustering quality against true labels.
Results Interpretation

Before: KMeans ARI = 70.5% (clusters are round, poor fit for moons shape)

After: DBSCAN ARI = 89.3% (clusters follow complex moon shapes well)

Advanced clustering methods like DBSCAN can find clusters with complex shapes by using density-based grouping, unlike KMeans which assumes round clusters.
Bonus Experiment
Try Spectral Clustering on the same dataset and compare its accuracy with DBSCAN.
💡 Hint
Spectral Clustering uses graph theory to find clusters and can handle complex shapes well. Use sklearn's SpectralClustering with n_clusters=2.