clustering
Discover patterns in unlabeled data using clustering, dimensionality reduction, and anomaly detection
What this skill does
# Clustering Skill
> Discover hidden patterns and groupings in unlabeled data.
## Quick Start
```python
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import silhouette_score
# Always scale before clustering
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Cluster
kmeans = KMeans(n_clusters=5, random_state=42, n_init=10)
labels = kmeans.fit_predict(X_scaled)
# Evaluate
score = silhouette_score(X_scaled, labels)
print(f"Silhouette Score: {score:.4f}")
```
## Key Topics
### 1. Clustering Algorithms
| Algorithm | Best For | Key Params |
|-----------|----------|------------|
| **K-Means** | Spherical clusters | n_clusters |
| **DBSCAN** | Arbitrary shapes, noise | eps, min_samples |
| **Hierarchical** | Nested clusters | linkage |
| **HDBSCAN** | Variable density | min_cluster_size |
```python
from sklearn.cluster import KMeans, DBSCAN, AgglomerativeClustering
import hdbscan
algorithms = {
'kmeans': KMeans(n_clusters=5, random_state=42),
'dbscan': DBSCAN(eps=0.5, min_samples=5),
'hierarchical': AgglomerativeClustering(n_clusters=5),
'hdbscan': hdbscan.HDBSCAN(min_cluster_size=15)
}
```
### 2. Finding Optimal K
```python
from sklearn.metrics import silhouette_score, calinski_harabasz_score
import matplotlib.pyplot as plt
def find_optimal_k(X, max_k=15):
"""Find optimal number of clusters."""
metrics = {'inertia': [], 'silhouette': [], 'calinski': []}
K = range(2, max_k + 1)
for k in K:
kmeans = KMeans(n_clusters=k, random_state=42, n_init=10)
labels = kmeans.fit_predict(X)
metrics['inertia'].append(kmeans.inertia_)
metrics['silhouette'].append(silhouette_score(X, labels))
metrics['calinski'].append(calinski_harabasz_score(X, labels))
# Find optimal k
optimal_k = K[np.argmax(metrics['silhouette'])]
return optimal_k, metrics
```
### 3. Dimensionality Reduction
| Method | Preserves | Speed |
|--------|-----------|-------|
| **PCA** | Global variance | Fast |
| **t-SNE** | Local structure | Slow |
| **UMAP** | Both local/global | Fast |
```python
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
import umap
# PCA for preprocessing
pca = PCA(n_components=0.95) # Keep 95% variance
X_pca = pca.fit_transform(X)
# UMAP for visualization
reducer = umap.UMAP(n_components=2, random_state=42)
X_2d = reducer.fit_transform(X)
```
### 4. Anomaly Detection
```python
from sklearn.ensemble import IsolationForest
from sklearn.neighbors import LocalOutlierFactor
# Isolation Forest
iso_forest = IsolationForest(contamination=0.1, random_state=42)
anomalies = iso_forest.fit_predict(X) # -1 for anomaly
# Local Outlier Factor
lof = LocalOutlierFactor(n_neighbors=20, contamination=0.1)
anomalies = lof.fit_predict(X)
```
### 5. Cluster Validation
```python
from sklearn.metrics import (
silhouette_score,
calinski_harabasz_score,
davies_bouldin_score
)
def validate_clustering(X, labels):
"""Comprehensive cluster validation."""
return {
'silhouette': silhouette_score(X, labels), # Higher = better
'calinski_harabasz': calinski_harabasz_score(X, labels), # Higher = better
'davies_bouldin': davies_bouldin_score(X, labels), # Lower = better
'n_clusters': len(set(labels) - {-1})
}
```
## Best Practices
### DO
- Always scale features before clustering
- Try multiple algorithms
- Use multiple validation metrics
- Visualize clusters in 2D
- Interpret clusters with domain knowledge
### DON'T
- Don't use K-Means for non-spherical clusters
- Don't ignore the silhouette score per cluster
- Don't assume first result is optimal
- Don't use t-SNE for new point projection
## Exercises
### Exercise 1: Elbow Method
```python
# TODO: Implement elbow method to find optimal K
# Plot inertia vs K and identify elbow point
```
### Exercise 2: Compare Algorithms
```python
# TODO: Compare K-Means, DBSCAN, and HDBSCAN
# on the same dataset using silhouette score
```
## Unit Test Template
```python
import pytest
import numpy as np
from sklearn.datasets import make_blobs
def test_clustering_finds_groups():
"""Test clustering finds expected number of clusters."""
X, y_true = make_blobs(n_samples=100, centers=3, random_state=42)
kmeans = KMeans(n_clusters=3, random_state=42)
labels = kmeans.fit_predict(X)
assert len(set(labels)) == 3
def test_scaling_improves_score():
"""Test that scaling improves clustering quality."""
X, _ = make_blobs(n_samples=100, centers=3)
X[:, 0] *= 100 # Make first feature much larger
# Without scaling
labels_raw = KMeans(n_clusters=3).fit_predict(X)
score_raw = silhouette_score(X, labels_raw)
# With scaling
X_scaled = StandardScaler().fit_transform(X)
labels_scaled = KMeans(n_clusters=3).fit_predict(X_scaled)
score_scaled = silhouette_score(X_scaled, labels_scaled)
assert score_scaled > score_raw
```
## Troubleshooting
| Problem | Cause | Solution |
|---------|-------|----------|
| All in one cluster | Wrong eps/K | Reduce eps or increase K |
| Too many clusters | Parameters too sensitive | Increase eps or min_samples |
| Poor silhouette | Wrong algorithm | Try different clustering method |
| Memory error | Large dataset | Use MiniBatchKMeans |
## Related Resources
- **Agent**: `03-unsupervised-learning`
- **Previous**: `supervised-learning`
- **Next**: `deep-learning`
---
**Version**: 1.4.0 | **Status**: Production Ready
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.