Data Science — Clustering
K-means
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
X, y = make_blobs(n_samples=300, centers=4, random_state=42)
kmeans = KMeans(n_clusters=4, random_state=42)
labels = kmeans.fit_predict(X)
plt.scatter(X[:, 0], X[:, 1], c=labels)
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], c='red', marker='x')
plt.show()
Elbow method
inertias = []
for k in range(1, 10):
kmeans = KMeans(n_clusters=k, random_state=42)
kmeans.fit(X)
inertias.append(kmeans.inertia_)
plt.plot(range(1, 10), inertias)
plt.xlabel('K')
plt.ylabel('Inertia')
plt.show()
DBSCAN
from sklearn.cluster import DBSCAN
dbscan = DBSCAN(eps=0.5, min_samples=5)
labels = dbscan.fit_predict(X)
Hierarchical clustering
from sklearn.cluster import AgglomerativeClustering
hierarchical = AgglomerativeClustering(n_clusters=4)
labels = hierarchical.fit_predict(X)
Evaluation
from sklearn.metrics import silhouette_score
score = silhouette_score(X, labels)
print(f"Silhouette score: {score:.4f}")
Mini Practice
- Apply K-means
- Find optimal K
- Try DBSCAN
- Evaluate clusters
Up Next
Continue with Dimensionality Reduction - Feature reduction.
Related Topics
Frequently Asked Questions about Clustering
What is Clustering in Data Science?
Clustering is a fundamental concept in Data Science. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Clustering?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Clustering.
Why is Clustering important in Data Science?
Clustering is essential for Data Science development. Understanding this concept will help you write better code and solve real-world problems more effectively.