python - Pandas 列中的多个局部最大值超过阈值
问题描述
在 pandas、numpy 或其他方法中是否有更有效的方法来查找并返回也超过阈值并由称为 local_max 的距离分隔的局部最大值的索引,如下所示?我认为我拥有的代码正在工作,但不是很干净。
import pandas as pd
import numpy as np
np.random.seed(101)
df = pd.DataFrame(np.random.randn(20,1),columns=['surge'])
surge
0 2.706850
1 0.628133
2 0.907969
3 0.503826
4 0.651118
5 -0.319318
6 -0.848077
7 0.605965
8 -2.018168
9 0.740122
10 0.528813
11 -0.589001
12 0.188695
13 -0.758872
14 -0.933237
15 0.955057
16 0.190794
17 1.978757
18 2.605967
19 0.683509
surge_threshold = .7
local_max = 5 # seperate results exeeding the surge threshold by 5 rows and return the highest local surge value.
# look for a surge
df.dropna(inplace=True)
i = df.first_valid_index()
markers = []
while i + 1 <= df.index[-1]:
if df.loc[i,'surge'] > surge_threshold:
# check if markers is an empty list
if markers:
if (i - markers[-1] < local_max):
if (df.loc[i,'surge'] >= df.loc[markers[-1],'surge']):
markers[-1] = i
else:
markers.append(i)
else:
markers.append(i)
i += 1
print(markers)
这个 df 的结果是 [0,9,18] 因为它们是超过 0.7 的局部最大值的索引,并且至少相隔 5 行。
解决方案
推荐阅读
- css - 当我需要不同的宽度时,如何处理 CSS 网格中的动态列数?
- sql - 为什么 SQL Server 死锁牺牲品错误 (1205) 会结束其所有事务?
- silverstripe - silverstripe 4 可翻译模块
- python - objectpath json查询以在python中获取数组值
- javascript - 添加 if 语句后,我的“提示”不会加载到网页上
- svg - 与 SVG 元素的样式和鼠标交互
- c++ - 如何使 Git 提交哈希在 C++ 代码中可用而无需重新编译?
- c# - 返回视图(“NameOfTheView”)不起作用
- c++ - copy_if 与保存序列并使用复制
- c# - DataAdapter.Fill() 在加载大十进制值时抛出溢出异常