python - 如何找到具有不同时间值的两个趋势之间的相关性
问题描述
我有一张包含时间和两种风速趋势的表格。我有兴趣找到两种风速趋势之间的相关性。这是下面的图表。
然而,问题是风速的记录时间略有不同。例如,可以在 2018 年 1 月 15 日上午 10:30 记录来自 1 号风速的数据,而可以在 2018 年 1 月 15 日上午 10:31 记录来自 2 号风速的最接近的数据。当我尝试使用来自 Jupyter Labs (python) 的 Pandas 运行 pearson/spearman 相关时,我收到一个错误,因为它们没有任何共同的时间。有没有办法让我仍然可以计算这两个趋势之间的相关性,尽管它们有不同的时间?如果没有,你会建议什么来解决这个问题?
解决方案
如果您认为两次读数之间存在滞后,您可以使用 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
它可能看起来是完美的解决方案,至少发现移动量将帮助您继续。
推荐阅读
- react-native - React Native FlatList 未使用状态值呈现
- c++ - 仅来自纯 C/C++ XLL 的 Excel 2010 功能区
- jenkins - 启动jetty server后Jenkins构建应该会成功,如何做到这一点?
- java - 内部开关不能接受用户输入
- lodash - 在 Lodash 的流程中得到预期的函数错误
- python - Python - 如何捕获读取文件的 read() 错误
- javascript - Electron 中的“断点设置但未绑定”问题
- javascript - getElementById 不需要。为什么?
- php - 在 PHP 中实现相同的方法重载行为的最佳方法是什么?
- python - 如何水平更改一个复选框到另一个第二个复选框之间的空间?