[英]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.