Python 的轨迹聚类/聚合

Trajectory Clustering/ Aggregation with Python

我正在使用 DBSCAN 处理定位社交媒体帖子并聚类它们的位置 (latitude/longitude)。在我的数据集中,我有许多用户多次发布,这使我能够推导出他们的轨迹(从一个地方到另一个位置的时间顺序序列)。例如:

3945641 [[38.9875, -76.94], [38.91711157, -77.02435118], [38.8991, -77.029], [38.8991, -77.029], [38.88927534, -77.04858468])

我已经为我的整个数据集导出了轨迹,我的下一步是对轨迹进行聚类或聚合,以识别位置之间运动密集的区域。关于如何处理 Python 中轨迹 clustering/aggregation 的任何想法?

这是我一直在使用的一些代码,用于创建轨迹作为行 strings/JSON 指令:

import pandas as pd
import numpy as np
import ujson as json
import time

# Import Data
data = pd.read_csv('filepath.csv', delimiter=',', engine='python')
#print len(data),"rows"
#print data

# Create Data Fame
df = pd.DataFrame(data, columns=['user_id','timestamp','latitude','longitude','cluster_labels])
#print data.head()

# Get a list of unique user_id values
uniqueIds = np.unique(data['user_id'].values)

# Get the ordered (by timestamp) coordinates for each user_id
output = [[id,data.loc[data['user_id']==id].sort_values(by='timestamp')[['latitude','longitude']].values.tolist()] for id in uniqueIds]

# Save outputs as csv
outputs = pd.DataFrame(output)
#print outputs
outputs.to_csv('filepath_out.csv', index=False, header=False)

# Save outputs as JSON
#outputDict = {}
#for i in output:
# outputDict[i[0]]=i[1]

#with open('filepath.json','w') as f:
#json.dump(outputDict, f, sort_keys=True, indent=4, ensure_ascii=False,)

编辑

我遇到了一个 python 包 NetworkX,并且正在讨论从我的集群创建网络图而不是对轨迹 lines/segments 进行集群的想法。关于聚类轨迹的任何意见 v.s。将聚类转化为图表,以识别位置之间密集聚集的运动。

下面是一些集群的示例:

为了回答我自己 1 年多的问题,我想出了几个解决方案,解决了这个问题(以及类似的问题),尽管没有 Python(这是我的希望)。首先,使用我在 GIS StackExchange 中为用户提供的一种方法,使用 ArcGIS 和一些内置工具来执行线密度分析 (https://gis.stackexchange.com/questions/42224/creating-polyline-based-heatmap-from-gps-tracks/270524#270524). This takes GPS points, creates lines, segments the lines, and then clusters them. The second method uses SQL (ST_MakeLine primarily) and a Postgres/GIS/CARTO data base to create lines ordered by ascending timestamp, and then grouped by users (e.g. https://carto.com/blog/jets-and-datelines/). One can then count the number of line occurrences (assuming points are clustered with clearly defined centroids similar to the initial question of mine above), and treat this as a cluster (e.g. , https://carto.com/blog/alteryx-and-carto-to-explore-london-bike-data/)。