首页 > 解决方案 > 如何找到具有不同时间值的两个趋势之间的相关性

问题描述

我有一张包含时间和两种风速趋势的表格。我有兴趣找到两种风速趋势之间的相关性。这是下面的图表。

在此处输入图像描述

然而,问题是风速的记录时间略有不同。例如,可以在 2018 年 1 月 15 日上午 10:30 记录来自 1 号风速的数据,而可以在 2018 年 1 月 15 日上午 10:31 记录来自 2 号风速的最接近的数据。当我尝试使用来自 Jupyter Labs (python) 的 Pandas 运行 pearson/spearman 相关时,我收到一个错误,因为它们没有任何共同的时间。有没有办法让我仍然可以计算这两个趋势之间的相关性,尽管它们有不同的时间?如果没有,你会建议什么来解决这个问题? 在此处输入图像描述

标签: pythonpandasstatisticsjupyter-notebook

解决方案


如果您认为两次读数之间存在滞后,您可以使用 argmax 来获得此滞后,下面的简单示例可能会对您有所帮助

import numpy, scipy
from scipy.signal import correlate

x = numpy.array([1.0,2.0,3.0,4.0,5.0,6.0,7.0])
y = numpy.array([6.0,7.0,1.0,2.0,3.0,4.0,5.0])

sample_size = x.size

x -= x.mean(); x /= x.std()
y -= y.mean(); y /= y.std()

xcorr = correlate(x, y)

dt = numpy.arange(1-sample_size, sample_size)

time_different = dt[xcorr.argmax()]

print(time_different)

>> -2 # this indicates we have shifting two samples

它可能看起来是完美的解决方案,至少发现移动量将帮助您继续。


推荐阅读