在 pandas 和 scipy.signal 中查找局部最大值/最小值的索引

问题描述

我的目标是在 pandas 或 matplotlib 中找到函数的局部最大值和最小值的索引。

假设我们有一个噪声信号,其局部最大值和最小值已经绘制如下链接

https://stackoverflow.com/a/50836425/15934571

这是代码(我只是从上面的链接复制粘贴):

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from scipy.signal import argrelextrema

# Generate a noisy AR(1) sample

np.random.seed(0)
rs = np.random.randn(200)
xs = [0]
for r in rs:
    xs.append(xs[-1] * 0.9 + r)
df = pd.DataFrame(xs,columns=['data'])

n = 5  # number of points to be checked before and after

# Find local peaks

df['min'] = df.iloc[argrelextrema(df.data.values,np.less_equal,order=n)[0]]['data']
df['max'] = df.iloc[argrelextrema(df.data.values,np.greater_equal,order=n)[0]]['data']

# Plot results

plt.scatter(df.index,df['min'],c='r')
plt.scatter(df.index,df['max'],c='g')
plt.plot(df.index,df['data'])
plt.show()

所以,我不知道如何从这一点继续并找到与图中获得的局部最大值和最小值相对应的索引。非常感谢您的帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)