dbscan-anisotropic-distance
Efficiently computing DBSCAN with a custom weighted (anisotropic) distance metric using coordinate scaling, avoiding the overhead of slow custom distance functions.
- License 许可
- MIT
- 最近更新
- 2026-10-02
- Stars
- ★ 93
- Forks
- 7
01 前言 Front Matter
- name
- dbscan-anisotropic-distance
- description
- Efficiently computing DBSCAN with a custom weighted (anisotropic) distance metric using coordinate scaling, avoiding the overhead of slow custom distance functions.
03 SKILL.md 正文
Efficient Anisotropic Distance for DBSCAN
When using scikit-learn's DBSCAN, passing a custom Python function to the metric argument forces the algorithm to bypass highly optimized C/Cython code, making it run extremely slowly (or requiring precomputing massive $N \times N$ distance matrices).
If your custom distance metric takes the form: $d(a, b) = \sqrt{(w_x \cdot \Delta x)^2 + (w_y \cdot \Delta y)^2}$
This is mathematically identical to performing a standard Euclidean distance search on scaled coordinates. By temporarily transforming the dataset, you can use the default metric='euclidean' in DBSCAN at full C-optimized speeds.
Implementation
import numpy as np
from sklearn.cluster import DBSCAN
def get_cluster_centroids_weighted(df_points, min_samples, epsilon, shape_weight):
if len(df_points) == 0:
return []
# 1. Extract coordinates
X = df_points[['x', 'y']].values.astype(float)
# 2. Scale coordinates to represent the weighted Euclidean distance
# Example: d = sqrt((w * dx)^2 + ((2-w) * dy)^2)
# We multiply the x column by w, and the y column by (2 - w)
X_scaled = X.copy()
X_scaled[:, 0] *= shape_weight
X_scaled[:, 1] *= (2 - shape_weight)
# 3. Run DBSCAN using standard Euclidean distance on the scaled data
db = DBSCAN(eps=epsilon, min_samples=min_samples, metric='euclidean')
labels = db.fit_predict(X_scaled)
# 4. Compute centroids using the ORIGINAL (unscaled) coordinates
centroids = []
for k in set(labels):
if k == -1:
continue # Skip noise points
cluster_mask = (labels == k)
# Always return the real-world spatial coordinates for the centroid!
cluster_centroid = X[cluster_mask].mean(axis=0)
centroids.append(cluster_centroid)
return np.array(centroids)
04 同仓库更多 Skills
object_counter
Count occurrences of an object in the image using computer vision algorithm.
travel-planning-workflow
Invoke this skill when performing travel planning. It defines a structured workflow for generating itineraries. Following this workflow is essential to ensure the final plan satisfies user requirements.
bundled-dataset-search-tool
invoke this skill when you need to perform database search for travel planning. This skill provides some useful pre-packaged tools to look up accommodations, attractions, cities, driving distance, flights, and restaurants from the bundled dataset.
glm-output
Output-processing guidance for GLM tasks. Especially useful after glm-basics and glm-calibration when you need verifier-matching metrics from output.nc and a final /root/metrics.json.
glm-calibration
Calibration guidance for GLM tasks. Often most effective after glm-basics has clarified the setup; glm-output is the companion skill for exact final metric computation.
glm-basics
A good starting point for GLM calibration tasks. Use to inspect glm3.nml, confirm how GLM runs, and identify the relevant files before moving on to calibration and output evaluation.