计算每个点都有时间序列的点之间的相关性
Calculating correlation between points where each points has a timeseries
我可以使用一些建议来更快地编写代码来解决我的问题。我正在研究如何计算 space (X,Y,Z) 中各点之间的相关性,对于每个点,我都有随时间变化的速度数据,理想情况下,我希望每个点 P1 都能计算速度相关性所有其他点。
最后我想要一个矩阵,对于每对坐标 (X1,Y1,Z1), (X2,Y2,Z2) 我得到 Pearson 相关系数。我不完全确定如何在 python 中最好地组织这个。到目前为止,我所做的是定义不同方向的点线,并为每条线计算点之间的相关性。这适用于分析,但我最终做的循环需要很长时间才能执行,我认为最好只计算所有点之间的相关性。现在我正在使用 pandas DataFrame 和 statsmodels 来做相关性(stats.pearsonr(point_X_time.Vx,point_Y_time.Vx),但我不知道如何有效地并行化它.
我现在将所有数据都放在一个 DataFrame 中,头部看起来像:
Velocity X Y Z Time
0 -12.125850 2.036 0 1.172 10.42
1 -12.516033 2.036 0 1.164 10.42
2 -11.816067 2.028 0 1.172 10.42
3 -10.722124 2.020 0 1.180 10.42
4 -10.628474 2.012 0 1.188 10.42
行数约为 300 000 行,但如果代码速度更快,可以轻松增加行数。
解决方案一:
groups = df.groupby(["X", "Y", "Z"])
您按 space 中的点对数据进行分组。
然后你遍历所有点的组合并计算相关性
import itertools
import numpy as np
for combinations in itertools.combinations(groups.groups.keys(),2):
first = groups.get_group(combinations[0])["Velocity"]
second = groups.get_group(combinations[1])["Velocity"]
if len(first) == len(second):
print(f"{combinations} {np.corrcoef(first, second)[0,1]:.2f}")
方案二:
df["cc"] = df.groupby(["X", "Y", "Z"]).cumcount()
df.set_index(["cc","X", "Y", "Z"])
df.unstack(level=[1,2,3])["Velocity"].corr()
我可以使用一些建议来更快地编写代码来解决我的问题。我正在研究如何计算 space (X,Y,Z) 中各点之间的相关性,对于每个点,我都有随时间变化的速度数据,理想情况下,我希望每个点 P1 都能计算速度相关性所有其他点。
最后我想要一个矩阵,对于每对坐标 (X1,Y1,Z1), (X2,Y2,Z2) 我得到 Pearson 相关系数。我不完全确定如何在 python 中最好地组织这个。到目前为止,我所做的是定义不同方向的点线,并为每条线计算点之间的相关性。这适用于分析,但我最终做的循环需要很长时间才能执行,我认为最好只计算所有点之间的相关性。现在我正在使用 pandas DataFrame 和 statsmodels 来做相关性(stats.pearsonr(point_X_time.Vx,point_Y_time.Vx),但我不知道如何有效地并行化它.
我现在将所有数据都放在一个 DataFrame 中,头部看起来像:
Velocity X Y Z Time 0 -12.125850 2.036 0 1.172 10.42 1 -12.516033 2.036 0 1.164 10.42 2 -11.816067 2.028 0 1.172 10.42 3 -10.722124 2.020 0 1.180 10.42 4 -10.628474 2.012 0 1.188 10.42
行数约为 300 000 行,但如果代码速度更快,可以轻松增加行数。
解决方案一:
groups = df.groupby(["X", "Y", "Z"])
您按 space 中的点对数据进行分组。
然后你遍历所有点的组合并计算相关性
import itertools
import numpy as np
for combinations in itertools.combinations(groups.groups.keys(),2):
first = groups.get_group(combinations[0])["Velocity"]
second = groups.get_group(combinations[1])["Velocity"]
if len(first) == len(second):
print(f"{combinations} {np.corrcoef(first, second)[0,1]:.2f}")
方案二:
df["cc"] = df.groupby(["X", "Y", "Z"]).cumcount()
df.set_index(["cc","X", "Y", "Z"])
df.unstack(level=[1,2,3])["Velocity"].corr()