# Run2 Custom Distance Metrics

> How to compute precomputed distance matrices for custom distance metrics in Scikit-Learn DBSCAN.

- Skill: `cxcscmu/run2-custom-distance-metrics-2` (Agent Skill)
- Install (CLI): `npx get-skill add cxcscmu/run2-custom-distance-metrics-2`
- Raw SKILL.md: https://api.skillmd.com/api/skills/cxcscmu/run2-custom-distance-metrics-2/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: cxcscmu (https://skillmd.com/u/cxcscmu)
- Updated: 2026-11-24
- Page: https://skillmd.com/skills/cxcscmu/run2-custom-distance-metrics-2

---


# Fast Custom Distance Metrics in Scikit-Learn

To effectively use custom distance metrics with DBSCAN, it is highly recommended to precompute the distance matrix. Python scalar functions passed to `metric` are typically too slow. Instead, use numpy broadcasting to compute the matrix.

## Installation
Ensure you have scikit-learn and numpy installed:
`pip install scikit-learn numpy`

## Usage
```python
import numpy as np
from sklearn.cluster import DBSCAN

def custom_dist_matrix(points, w):
    """
    Computes a distance matrix for N points.
    d(a, b) = sqrt((w * Δx)^2 + ((2 - w) * Δy)^2)
    """
    points = np.asarray(points)
    # Broadcasting to find all pairwise differences
    dx = w * (points[:, 0:1] - points[:, 0:1].T)
    dy = (2 - w) * (points[:, 1:2] - points[:, 1:2].T)
    return np.sqrt(dx**2 + dy**2)

# X is an N by 2 array of points
# dist_matrix = custom_dist_matrix(X, w=1.5)
# db = DBSCAN(eps=10, min_samples=5, metric='precomputed').fit(dist_matrix)
```

