gpt4 book ai didi

python - 计算时间序列中的峰值

转载 作者:太空宇宙 更新时间:2023-11-03 15:35:27 24 4
gpt4 key购买 nike

我正在计算 numpy 数组中峰和谷的数量。

我有一个像这样的 numpy 数组:

stack = np.array([0,0,5,4,1,1,1,5,1,1,5,1,1,1,5,1,1,5,1,1,5,1,1,5,1,1,5,1,1])

绘图后,这些数据看起来像这样:

Time Series plot of 'stack'array

我要查找此时间序列中的峰值数量:

这是我的代码,它适用于像这样的示例,其中时间序列表示中有明显的波峰和波谷。我的代码返回已找到峰值的数组索引。

#example
import numpy as np
from scipy.signal import argrelextrema

stack =
np.array([0,0,5,4,1,1,1,5,1,1,5,1,1,1,5,1,1,5,1,1,5,1,1,5,1,1,5,1,1])

# for local maxima
y = argrelextrema(stack, np.greater)

print(y)

结果:

(array([ 2,  7, 10, 14, 17, 20, 23, 26]),)

已发现并能正确计数的清晰峰有8个。

我的解决方案似乎无法很好地处理不太清晰且比较困惑的数据。

下面的数组不能很好地工作并且找不到我需要的峰:

array([ 0.        ,  5.70371806,  5.21210157,  3.71144767,  3.9020162 ,
3.87735984, 3.89030171, 6.00879918, 4.91964227, 4.37756275,
4.03048542, 4.26943028, 4.02080471, 7.54749062, 3.9150576 ,
4.08933851, 4.01794766, 4.13217794, 4.15081972, 8.11213474,
4.6561735 , 4.54128693, 3.63831552, 4.3415324 , 4.15944019,
8.55171441, 4.86579459, 4.13221943, 4.487663 , 3.95297979,
4.35334706, 9.91524674, 4.44738182, 4.32562141, 4.420753 ,
3.54525697, 4.07070637, 9.21055852, 4.87767969, 4.04429321,
4.50863677, 3.38154581, 3.73663523, 3.83690315, 6.95321174,
5.11325128, 4.50351938, 4.38070175, 3.20891173, 3.51142661,
7.80429569, 3.98677631, 3.89820773, 4.15614576, 3.47369797,
3.73355768, 8.85240649, 6.0876192 , 3.57292324, 4.43599135,
3.77887259, 3.62302175, 7.03985076, 4.91916556, 4.22246518,
3.48080777, 3.26199699, 2.89680969, 3.19251448])

绘图,此数据如下所示:

Data 2

同样的代码返回:

(array([ 1,  4,  7, 11, 13, 15, 19, 23, 25, 28, 31, 34, 37, 40, 44, 50, 53,
56, 59, 62]),)

此输出错误地将数据点计为峰值。

理想输出

理想的输出应该返回清晰峰的数量,在本例中为 11 个,它们位于索引处:

[1,7,13,19,25,31,37,44,50,56,62]

我认为我的问题是由于 argrelextrema 函数的聚合性质造成的。

最佳答案

你应该在 scipy.signal 模块中尝试 find_peaks

您的数据示例

import numpy as np
from scipy.signal import find_peaks
import matplotlib.pyplot as plt

a = np.array([ 0. , 5.70371806, 5.21210157, 3.71144767, 3.9020162 , 3.87735984, 3.89030171, 6.00879918, 4.91964227, 4.37756275,
4.03048542, 4.26943028, 4.02080471, 7.54749062, 3.9150576 ,
4.08933851, 4.01794766, 4.13217794, 4.15081972, 8.11213474,
4.6561735 , 4.54128693, 3.63831552, 4.3415324 , 4.15944019,
8.55171441, 4.86579459, 4.13221943, 4.487663 , 3.95297979,
4.35334706, 9.91524674, 4.44738182, 4.32562141, 4.420753 ,
3.54525697, 4.07070637, 9.21055852, 4.87767969, 4.04429321,
4.50863677, 3.38154581, 3.73663523, 3.83690315, 6.95321174,
5.11325128, 4.50351938, 4.38070175, 3.20891173, 3.51142661,
7.80429569, 3.98677631, 3.89820773, 4.15614576, 3.47369797,
3.73355768, 8.85240649, 6.0876192 , 3.57292324, 4.43599135,
3.77887259, 3.62302175, 7.03985076, 4.91916556, 4.22246518,
3.48080777, 3.26199699, 2.89680969, 3.19251448])

# Here you should fine tune parameters to get what you want
peaks = find_peaks(a, prominence=1.5)
print("Peaks position:", peaks[0])

# Plotting
plt.plot(a)
plt.title("Finding Peaks")

[plt.axvline(p, c='C3', linewidth=0.3) for p in peaks[0]]

plt.show()

情节

plot

输出

# Peaks position: [ 1  7 13 19 25 31 37 44 50 56 62]

关于python - 计算时间序列中的峰值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55081808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com