及时关联 ID 的移动模式

Correlating movement patterns of IDs in time

我每个 ID 都有一些 x、y 坐标,并使用 groupby('ID').diff() 函数减去 x 和 y 坐标之间的差异,以便识别方向模式/个人 ID . 如果方向(xx 和 yy)在 0 附近,则 ID 没有移动。现在,如何找到不同 ID 及其方向之间的相关性?理想情况下,我想将相互靠近的 ID 和位于 "silent" 的 ID 配对。非常感谢任何帮助!!!

ID      Time                    X   Y   xx  yy
42403   2019-07-24 08:00:00.255 225 235 1.0 1.0
42386   2019-07-24 08:00:00.255 257 232 -1.0 0.0
42403   2019-07-24 08:00:00.495 226 235 1.0 0.0
42386   2019-07-24 08:00:00.495 257 232 0.0 0.0
42403   2019-07-24 08:00:00.733 226 235 0.0 0.0
42386   2019-07-24 08:00:00.733 257 232 0.0 0.0
42403   2019-07-24 08:00:00.008 224 234 -2.0 -1.0
42386   2019-07-24 08:00:00.008 258 232 1.0 0.0
42403   2019-07-24 08:00:00.255 225 235 1.0 1.0
42386   2019-07-24 08:00:00.255 257 232 -1.0 0.0
42403   2019-07-24 08:00:00.495 226 235 1.0 0.0
42386   2019-07-24 08:00:00.495 257 232 0.0 0.0
42403   2019-07-24 08:00:00.733 226 235 0.0 0.0
42386   2019-07-24 08:00:00.733 257 232 0.0 0.0
42403   2019-07-24 08:00:01.009 224 235 -2.0 0.0
42386   2019-07-24 08:00:01.009 258 232 1.0 0.0
42403   2019-07-24 08:00:01.371 225 235 1.0 0.0
42386   2019-07-24 08:00:01.371 259 232 1.0 0.0
42403   2019-07-24 08:00:01.611 226 235 1.0 0.0
42386   2019-07-24 08:00:01.611 258 232 -1.0 0.0
42403   2019-07-24 08:00:01.736 226 235 0.0 0.0
42386   2019-07-24 08:00:01.736 258 232 0.0 0.0
42403   2019-07-24 08:00:02.066 226 235 0.0 0.0
42386   2019-07-24 08:00:02.066 259 232 1.0 0.0
42403   2019-07-24 08:00:02.281 226 234 0.0 -1.0
42386   2019-07-24 08:00:02.281 259 232 0.0 0.0
42403   2019-07-24 08:00:02.568 226 234 0.0 0.0
42386   2019-07-24 08:00:02.568 259 232 0.0 0.0
42403   2019-07-24 08:00:02.769 225 234 -1.0 0.0
42386   2019-07-24 08:00:02.769 259 232 0.0 0.0
42403   2019-07-24 08:00:03.010 225 234 0.0 0.0
42386   2019-07-24 08:00:03.010 259 232 0.0 0.0
42403   2019-07-24 08:00:03.242 225 233 0.0 -1.0
42386   2019-07-24 08:00:03.242 259 232 0.0 0.0
42403   2019-07-24 08:00:03.574 225 235 0.0 2.0
42386   2019-07-24 08:00:03.574 259 232 0.0 0.0
42403   2019-07-24 08:00:03.760 224 235 -1.0 0.0
42386   2019-07-24 08:00:03.760 259 231 0.0 -1.0
42403   2019-07-24 08:00:03.971 224 234 0.0 -1.0
42386   2019-07-24 08:00:03.971 259 232 0.0 1.0
42403   2019-07-24 08:00:04.231 224 234 0.0 0.0
42386   2019-07-24 08:00:04.231 259 232 0.0 0.0
42403   2019-07-24 08:00:04.567 224 234 0.0 0.0
42386   2019-07-24 08:00:04.567 259 232 0.0 0.0
42403   2019-07-24 08:00:04.849 223 234 -1.0 0.0
42386   2019-07-24 08:00:04.849 259 232 0.0 0.0
42403   2019-07-24 08:00:05.054 223 234 0.0 0.0
42386   2019-07-24 08:00:05.054 259 232 0.0 0.0
42403   2019-07-24 08:00:05.288 224 235 1.0 1.0
42386   2019-07-24 08:00:05.288 259 232 0.0 0.0
42403   2019-07-24 08:00:05.597 225 234 1.0 -1.0
42386   2019-07-24 08:00:05.597 259 232 0.0 0.0
42403   2019-07-24 08:00:05.783 222 232 -3.0 -2.0
42386   2019-07-24 08:00:05.783 259 232 0.0 0.0
42403   2019-07-24 08:00:06.014 222 233 0.0 1.0

我会首先获取您想要计算列中相关性的值,然后使用已经实现的 pandas .corr() 方法。所以对于它来说,unstack 方法是一种简单的方法。

这是我的代码:

# considering that the position measures are already sorted we will create indexes for every step and call them 'measure'
df['measure'] = [_ // 2 for _ in range(len(df))] # 2 IDs in total
# no more need of time data
df.drop(labels='Time', axis=1, inplace=True)

# this line of work does all the work. I am using the .corr() method from pandas
df.set_index(['ID', 'measure']).unstack('ID').corr()

输出应该是一个相关矩阵,我用一种奇特的方法使用 seaborn 生成了这个热图:

如果您也对可视化感兴趣,请查看 seaborn 的热图方法。