用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/cxcscmu/SkillLearnBench --skill dbscan-custom-metrics命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
基于 SOC 职业分类
正在显示 SKILL.md
| name | dbscan-custom-metrics |
| description | Implement DBSCAN clustering with custom distance metrics using scikit-learn's pairwise_distances. |
DBSCAN can use custom distance metrics by computing a precomputed distance matrix or using pairwise_distances with a custom metric function.
A custom metric function takes two 1D arrays (two points) and returns a scalar distance:
def custom_metric(u, v, shape_weight):
"""Compute weighted distance between two points."""
dx = u[0] - v[0]
dy = u[1] - v[1]
return np.sqrt((shape_weight * dx)**2 + ((2 - shape_weight) * dy)**2)
For DBSCAN with a custom metric, use metric='precomputed' and pass a precomputed distance matrix:
from sklearn.metrics.pairwise import pairwise_distances
from sklearn.cluster import DBSCAN
# Compute precomputed distance matrix
distances = pairwise_distances(
points,
metric=custom_metric,
metric_params={'shape_weight': w}
)
# Run DBSCAN with precomputed distances
clusterer = DBSCAN(eps=epsilon, min_samples=min_samples, metric='precomputed')
labels = clusterer.fit_predict(distances)
import numpy as np
from sklearn.metrics.pairwise import pairwise_distances
from sklearn.cluster import DBSCAN
def shape_weighted_distance(u, v, shape_weight):
"""Distance metric with shape weighting."""
dx = u[0] - v[0]
dy = u[1] - v[1]
return np.sqrt((shape_weight * dx)**2 + ((2 - shape_weight) * dy)**2)
def cluster_with_custom_metric(points, epsilon, min_samples, shape_weight):
"""Cluster points using DBSCAN with custom distance metric."""
if len(points) == 0:
return np.array([], dtype=int)
# Compute distance matrix
distances = pairwise_distances(
points,
metric=shape_weighted_distance,
metric_params={'shape_weight': shape_weight}
)
# Run DBSCAN
clusterer = DBSCAN(eps=epsilon, min_samples=min_samples, metric='precomputed')
labels = clusterer.fit_predict(distances)
return labels