sklearn DBSCAN用大epsilon聚类GPS位置

问题描述 投票:0回答:2

我想使用sklearn的DBSCAN从我的GPS位置找到群集。我不明白为什么坐标[18.28,57.63](图中的右下角)与左边的其他坐标聚在一起。这可能是大epsilon的一些问题吗? sklearn版本0.19.0。 Test cluster重现这一点:我从这里复制了演示代码: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()
python scikit-learn gps cluster-analysis dbscan
2个回答
1
投票

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

OP自己提供的答案是正确的,但细节不足。当然,人们可以将纬度/经度值乘以π/ 180,但是 - 如果你已经在使用numpy了 - 最简单的解决方法是在原始代码中改变这一行:

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

至:

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

1
投票

hasrsine指标需要弧度数据

© www.soinside.com 2019 - 2024. All rights reserved.