Pandas 实时关联入侵检测系统的运动模式

Pandas 实时关联入侵检测系统的运动模式,pandas,time-series,coordinates,correlation,pairing,Pandas,Time Series,Coordinates,Correlation,Pairing,我每个ID有一些x、y坐标,并使用groupby('ID').diff()函数减去x和y坐标之间的差异,以识别方向模式/单个ID。 如果方向(xx和yy)在0左右,则ID不会移动。现在,我如何找到不同ID及其方向之间的相关性?理想情况下,我希望将相互移动的ID与“静默”的ID配对。非常感谢您的帮助 ID Time X Y xx yy 42403 2019-07-24 08:00:00.255 225 235 1.0 1.0 42386

我每个ID有一些x、y坐标,并使用groupby('ID').diff()函数减去x和y坐标之间的差异,以识别方向模式/单个ID。 如果方向(xx和yy)在0左右,则ID不会移动。现在,我如何找到不同ID及其方向之间的相关性?理想情况下,我希望将相互移动的ID与“静默”的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()方法。因此,对于它来说,非堆叠方法是一种简单的方法

这是我的密码:

# 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提供的热图方法。

非常感谢您的支持。这太棒了。你知道如何将时间作为变量进行关联吗?有没有办法将数据分成几秒或几分钟,并查看id在时间上的相关性?您可以构建一个三维矩阵,其中第三个矩阵将是时间(一个时间空间)。然后,为了检查不同轴上的运动是否与时间相关,可以计算时间轴上的平均值和标准偏差。希望这会有所帮助。你能选择我对你上一个问题的回答作为一个好答案吗?这将为我的个人资料添加点