我想使用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()
答案 0 :(得分:1)
hasrsine指标需要以弧度为单位的数据
答案 1 :(得分:1)
我最近犯了同样的错误(使用hdbscan),这是导致某些“奇怪”结果的原因。例如,相同点有时会包含在群集中,有时会标记为噪声点。 “这怎么是?”,我一直在想。原来是因为我直接经过纬度/经度,而不是先转换为弧度。
OP的自我提供的答案是正确的,但细节不足。当然,可以将lat / lon值乘以π/ 180,但是-如果您已经在使用numpy
,则最简单的解决方法是在原始代码中更改此行:
db = DBSCAN(eps=epsilon, ... metric='haversine').fit(X)
收件人:
db = DBSCAN(eps=epsilon, ... metric='haversine').fit(np.radians(X))