Python中使用timeseries的联系人跟踪

2024-04-19 00:26:46 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有时间序列数据(x轴上的时间,y-z平面上的坐标。在

给定一个受感染用户的种子集,我希望在t时间内从种子集中的点获取距离d以内的所有用户。这基本上就是联系追踪。在

实现这一目标的明智方法是什么?在

天真的方法是这样的:

points_at_end_of_iteration = []
for p in seed_set:
    other_ps = find_points_t_time_away(t)
    points_at_end_of_iteration += find_points_d_distance_away_from_set(other_ps)

有什么更聪明的方法来做到这一点——最好是将所有数据保存在RAM中(尽管我不确定这是否可行)。熊猫是个好选择吗?我也在考虑Bandicoot,但它似乎不能为我做到这一点。在

请让我知道我是否可以改进这个问题-也许它太宽泛了。在

编辑:

我认为上面的算法是有缺陷的。在

这样更好吗:

^{pr2}$

infected_set我认为应该是一个hashmap {user_id: {last_time: ..., last_pos: ...}, user_id2: ...}

一个潜在的问题是用户是独立处理的,因此user2的下一个时间戳可能是user1之后的几小时或几天。在

如果我插值,让每个用户都有每个时间点(比如每小时)的信息,那么联系人跟踪可能会更容易,尽管这会增加大量的数据量。在

数据格式/样本

user_id = 123
timestamp = 2015-05-01 05:22:25
position = 12.111,-12.111 # lat,long

有一个csv文件包含所有记录:

uid1,timestamp1,position1
uid1,timestamp2,position2
uid2,timestamp3,position3

还有一个文件目录(相同格式),每个文件对应于一个用户。在

记录/uid1.csv
记录/uid2.csv


Tags: ofcsv数据方法用户记录时间种子
1条回答
网友
1楼 · 发布于 2024-04-19 00:26:46

带插值的第一个解:

# i would use a shelf (a persistent, dictionary-like object,
# included with python).
import shelve

# hashmap of clean users indexed by timestamp)
# { timestamp1: {uid1: (lat11, long11), uid12: (lat12, long12), ...},
#   timestamp2: {uid1: (lat21, long21), uid2: (lat22, long22), ...},
#   ...
# }
#
clean_users = shelve.open("clean_users.dat")

# load data in clean_users from csv (shelve use same syntax than
# hashmap). You will interpolate data (only data at a given timestamp
# will be in memory at the same time). Note: the timestamp must be a string

# hashmap of infected users indexed by timestamp (same format than clean_users)
infected_users = shelve.open("infected_users.dat")

# for each iteration
for iteration in range(1, N):

    # compute current timestamp because we interpolate each user has a location
    current_timestamp = timestamp_from_iteration(iteration)

    # get clean users for this iteration (in memory)
    current_clean_users = clean_user[current_timestamp]

    # get infected users for this iteration (in memory)
    current_infected_users = infected_user[current_timestamp]

    # new infected user for this iteration
    new_infected_users = dict()

    # compute new infected_users for this iteration from current_clean_users and
    # current_infected_users then store the result in new_infected_users

    # remove user in new_infected_users from clean_users

    # add user in new_infected_users to infected_users

# close the shelves
infected_users.close()
clean_users.close()

无插值的二次解:

^{pr2}$

相关问题 更多 >