Custom Distance Metrics for DBSCAN
SkillMonitoring & opsDefine custom distance/similarity metrics for clustering and ML algorithms. Use when working with DBSCAN, sklearn, or scipy distance functions with application-specific metrics.
Available today. Use it from your connected AI after setup.
No other account needed.
Connect ahel once, and every AI you use reads what you have installed.
Then ask your AI: use the Custom Distance Metrics for DBSCAN skill
What this skill tells your AI
The instructions your AI receives, as published by cxcscmu/skilllearnbench in skills/human_authored/dbscan-parameter-tuning/custom-distance-metrics/SKILL.md and read by ahel’s review.
Overview
DBSCAN in sklearn supports custom distance metrics via metric='precomputed' (pass a distance matrix) or metric=callable with the pairwise distance function.
Approach: Precomputed Distance Matrix
For small-to-medium datasets per image, computing a full pairwise distance matrix is efficient:
from sklearn.cluster import DBSCAN
from scipy.spatial.distance import pdist, squareform
import numpy as np
def weighted_euclidean(points, w):
"""Compute pairwise weighted Euclidean distance.
d(a,b) = sqrt((w*dx)^2 + ((2-w)*dy)^2)
"""
scaled = points * [w, 2 - w]
return squareform(pdist(scaled, metric='euclidean'))
# Usage
dist_matrix = weighted_euclidean(points_xy, shape_weight)
db = DBSCAN(eps=epsilon, min_samples=min_samples, metric='precomputed')
labels = db.fit_predict(dist_matrix)
Key Points
pdist+squareformis faster than looping over pairs- Scale the coordinates before computing standard Euclidean = same as custom weighted metric
- When w=1, this equals standard Euclidean distance
- Cluster centroids are computed from original (unscaled) coordinates
Signals
- GitHub stars
- 83
- Forks
- 5
- Last commit
- Jul 2026
Advanced
- Catalog kind
- skill
- Gateway key
custom-distance-metrics- Source
- github.com/cxcscmu/skilllearnbench