使用 DBSCAN 进行轨迹聚类
Trajectory clustering using DBSCAN
我正在尝试确定轨迹上的路径。我有一条带有经纬度点的轨迹。
这是我的代码:
def clustersDBSCAN(data):
from sklearn.cluster import DBSCAN
a=data
coords = a['Long']
coords['Lat'] = a['Lat']
coords = coords.to_numpy(coords)
kms_per_radian = 6371.0088
epsilon = 0.02 / kms_per_radian
db = DBSCAN(eps=epsilon, min_samples=1, algorithm='ball_tree', metric='haversine').fit(np.radians(coords))
cluster_labels = db.labels_
a['clusters']=cluster_labels
return a
我输入的是一个带有一些其他变量的 DataFrame。当我 运行 我的程序时,它使我出现以下错误:
Traceback (most recent call last):
File "<ipython-input-160-1bb326319131>", line 19, in <module>
TestEtude1 = clustersDBSCAN(TestEtude1)
File "<ipython-input-160-1bb326319131>", line 14, in clustersDBSCAN
db = DBSCAN(eps=epsilon, min_samples=1, algorithm='ball_tree', metric='haversine').fit(np.radians(coords))
TypeError: loop of ufunc does not support argument 0 of type float which has no callable radians method
编辑 :
我的数据是这样的:
Lat Long Type de point
136701 53.87030526540526 7.305133353275677 1
136702 53.870307858385225 7.305140443133933 0
136703 53.87031363700621 7.305150308822018 0
136704 53.87031595061333 7.305142298625614 0
136705 53.87032064860515 7.305141557055512 0
136706 53.870326088345934 7.305156457965349 2
136707 53.87030945094248 7.305160487693352 1
136708 53.870349819652134 7.305194852863318 0
136709 53.870340745293994 7.305186559915658 0
136710 53.8702835623423 7.305181727204434 0
点1的类型是指轨迹的起点,点2的类型是指轨迹的终点。 1和2之间,有0类型的point点,是我中间按时间点排序的。
数据的特征包括纬度和经度。由于它是一个 pandas 数据框,在这种情况下,您可以切分要用于执行聚类的功能。
查看代码,可以看出传递的特征不正确,可以这样做:
将 np.radians(coords)
替换为 fit()
中的 np.radians(data[["Lat","Long"]])
,它应该可以工作。
我正在尝试确定轨迹上的路径。我有一条带有经纬度点的轨迹。
这是我的代码:
def clustersDBSCAN(data):
from sklearn.cluster import DBSCAN
a=data
coords = a['Long']
coords['Lat'] = a['Lat']
coords = coords.to_numpy(coords)
kms_per_radian = 6371.0088
epsilon = 0.02 / kms_per_radian
db = DBSCAN(eps=epsilon, min_samples=1, algorithm='ball_tree', metric='haversine').fit(np.radians(coords))
cluster_labels = db.labels_
a['clusters']=cluster_labels
return a
我输入的是一个带有一些其他变量的 DataFrame。当我 运行 我的程序时,它使我出现以下错误:
Traceback (most recent call last):
File "<ipython-input-160-1bb326319131>", line 19, in <module>
TestEtude1 = clustersDBSCAN(TestEtude1)
File "<ipython-input-160-1bb326319131>", line 14, in clustersDBSCAN
db = DBSCAN(eps=epsilon, min_samples=1, algorithm='ball_tree', metric='haversine').fit(np.radians(coords))
TypeError: loop of ufunc does not support argument 0 of type float which has no callable radians method
编辑 :
我的数据是这样的:
Lat Long Type de point
136701 53.87030526540526 7.305133353275677 1
136702 53.870307858385225 7.305140443133933 0
136703 53.87031363700621 7.305150308822018 0
136704 53.87031595061333 7.305142298625614 0
136705 53.87032064860515 7.305141557055512 0
136706 53.870326088345934 7.305156457965349 2
136707 53.87030945094248 7.305160487693352 1
136708 53.870349819652134 7.305194852863318 0
136709 53.870340745293994 7.305186559915658 0
136710 53.8702835623423 7.305181727204434 0
点1的类型是指轨迹的起点,点2的类型是指轨迹的终点。 1和2之间,有0类型的point点,是我中间按时间点排序的。
数据的特征包括纬度和经度。由于它是一个 pandas 数据框,在这种情况下,您可以切分要用于执行聚类的功能。
查看代码,可以看出传递的特征不正确,可以这样做:
将 np.radians(coords)
替换为 fit()
中的 np.radians(data[["Lat","Long"]])
,它应该可以工作。