【问题标题】:Finding peaks in pandas series with non integer index在具有非整数索引的熊猫系列中查找峰值
【发布时间】:2021-08-22 09:37:58
【问题描述】:

我有以下系列并试图找到应该是[1,8.5] 的峰值索引或应该是[279,139] 的峰值。使用的阈值为 100。我尝试了很多方法,但它总是忽略系列索引并返回 [1,16]

0.5       0
1.0     279
1.5     256
2.0      84
2.5      23
3.0      11
3.5       3
4.0       2
4.5       7
5.0       5
5.5       4
6.0       4
6.5      10
7.0      30
7.5      88
8.0     133
8.5     139
9.0      84
9.5      55
10.0     26
10.5     10
11.0      8
11.5      4
12.0      4
12.5      1
13.0      0
13.5      0
14.0      1
14.5      0

我试过这段代码

thresh = 100
peak_idx, _ = find_peaks(out.value_counts(sort=False), height=thresh)
plt.plot(out.value_counts(sort=False).index[peak_idx], out.value_counts(sort=False)[peak_idx], 'r.')
out.value_counts(sort=False).plot.bar()
plt.show()
peak_idx

这是输出 array([ 1, 16], dtype=int64)

【问题讨论】:

  • 代码没问题,如果你检查它确实找到了正确的峰值,那么你在图表中绘制点的方式有问题
  • 您误解了 find_peaks 的工作原理,它确实返回峰的索引而不是峰的值

标签: python pandas dataframe series peak-detection


【解决方案1】:

你做对了,唯一误解是find_peaks 找到了峰的索引,而不是峰本身。

这是明确指出的文档:

Returns

peaksndarray

    Indices of peaks in x that satisfy all given conditions.

参考:https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.find_peaks.html

在这里试试这个代码:

thresh = 100
y = [0,279,256, 84, 23, 11,  3,  2,  7,  5,  4,  4, 10, 30, 88,133,139, 84, 55, 26, 10,  8,  4,  4,  1,  0,  0,  1,  0]
x = [0.5 ,1.0 ,1.5 ,2.0 ,2.5 ,3.0 ,3.5 ,4.0 ,4.5 ,5.0 ,5.5 ,6.0 ,6.5 ,7.0 ,7.5 ,8.0 ,8.5 ,9.0 ,9.5 ,10.0,10.5,11.0,11.5,12.0,12.5,13.0,13.5,14.0,14.5]
peak_idx, _ = find_peaks(x, height=thresh)
out_values = [x[peak] for peak in peak_idx]

这里 out_vaules 将包含您想要的内容

【讨论】:

  • 谢谢。我有一种感觉,我错过了一些微不足道的事情。
猜你喜欢
  • 2016-04-19
  • 2013-08-22
  • 2017-02-21
  • 2018-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多