繁体   English   中英

sklearn DBSCAN将具有大epsilon的GPS位置聚类

[英]sklearn DBSCAN to cluster GPS positions with big epsilon

我想使用sklearn中的DBSCAN从我的GPS位置中找到簇。 我不明白为什么坐标[18.28,57.63](图中的右下角)与其他坐标一起聚集在左侧。 大epsilon可能有问题吗? sklearn版本0.19.0。 测试集群 要重现此内容:我从此处复制了演示代码: http : //scikit-learn.org/stable/auto_examples/cluster/plot_dbscan.html,但我将示例数据替换为一些坐标(请参见下面的代码中的变量X)。 我从这里得到了灵感: http : //geoffboeing.com/2014/08/clustering-to-reduce-spatial-data-set-size/

import numpy as np

from sklearn.cluster import DBSCAN
from sklearn import metrics
from sklearn.datasets.samples_generator import make_blobs
from sklearn.preprocessing import StandardScaler


# #############################################################################
# Generate sample data

X = np.array([[ 11.95,  57.70],
       [ 16.28,  57.63],
       [ 16.27,  57.63],
       [ 16.28,  57.66],
       [ 11.95,  57.63],
       [ 12.95,  57.63],
       [ 18.28,  57.63],
       [ 11.97,  57.70]])

# #############################################################################
# Compute DBSCAN
kms_per_radian = 6371.0088
epsilon = 400 / kms_per_radian
db = DBSCAN(eps=epsilon, min_samples=2, algorithm='ball_tree', metric='haversine').fit(X)
core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
core_samples_mask[db.core_sample_indices_] = True
labels = db.labels_

# Number of clusters in labels, ignoring noise if present.
n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)

print('Estimated number of clusters: %d' % n_clusters_)

# #############################################################################
# Plot result
import matplotlib.pyplot as plt

# Black removed and is used for noise instead.
unique_labels = set(labels)
colors = [plt.cm.Spectral(each)
          for each in np.linspace(0, 1, len(unique_labels))]
for k, col in zip(unique_labels, colors):
    if k == -1:
        # Black used for noise.
        col = [0, 0, 0, 1]

    class_member_mask = (labels == k)

    xy = X[class_member_mask & core_samples_mask]
    plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
             markeredgecolor='k', markersize=14)

    xy = X[class_member_mask & ~core_samples_mask]
    plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
             markeredgecolor='k', markersize=6)

plt.title('Estimated number of clusters: %d' % n_clusters_)
plt.show()

Haversine指标需要弧度数据

我最近犯了同样的错误(使用hdbscan ),这是某些“奇怪”结果的原因。 例如, 一点有时会包含在群集中,有时会标记为噪声点。 “这怎么可能 ?”,我一直在想。 原来是因为我直接通过纬度/经度,而不是先不转换为弧度。

OP的自我提供的答案是正确的,但细节不足。 当然,可以将lat / lon值乘以π/ 180,但是-如果您已经在使用numpy ,则最简单的解决方法是在原始代码中更改此行:

db = DBSCAN(eps=epsilon, ... metric='haversine').fit(X)

至:

db = DBSCAN(eps=epsilon, ... metric='haversine').fit(np.radians(X))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM