[英]Finding the local maxima and local minima in the data python
Data:数据:
+---------------------+------------------+--+
| date_add | fnv_wa | |
+---------------------+------------------+--+
| 2022-06-24 06:00:16 | 46.216866 | |
| 2022-06-24 07:00:16 | 46.216866 | |
| 2022-06-24 08:00:16 | 45.685139 | |
| 2022-06-24 09:00:16 | 45.633936 | |
| 2022-06-24 10:00:16 | 43.487337 | |
| 2022-06-24 11:00:16 | 40.182756 | |
| 2022-06-24 12:00:16 | 40.017330 | |
| 2022-06-24 13:00:16 | 39.548623 | |
| 2022-06-24 14:00:16 | 39.548623 | |
| 2022-06-24 15:00:16 | 38.607271 | |
| 2022-06-24 16:00:16 | 39.989759 | |
| 2022-06-24 17:00:16 | 39.111426 | |
| 2022-06-24 18:00:16 | 37.862854 | |
| 2022-06-24 19:00:16 | 37.862854 | |
| 2022-06-24 20:00:16 | 37.862854 | |
| 2022-06-24 21:00:16 | 36.173146 | |
| 2022-06-24 22:00:16 | 35.164835 | |
+---------------------+------------------+--+
I'm trying to find all the local maxima's and minima's in my data, the approaches I tried are listed below:我试图在我的数据中找到所有局部最大值和最小值,我尝试的方法如下所列:
Implementation:执行:
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']
Implementation:执行:
df['min'] = df.data[(df.data.shift(1) > df.data) & (df.data.shift(-1) > df.data)]
df['max'] = df.data[(df.data.shift(1) < df.data) & (df.data.shift(-1) < df.data)]
The problem with above two approaches, the local maxima at X~10 is not detected.上述两种方法的问题,X~10 处的局部最大值没有被检测到。
Please suggest an approach that can find all the local maxima and local minima in my data.请提出一种可以在我的数据中找到所有局部最大值和局部最小值的方法。
Which value to you use for n
?您对n
使用哪个值?
Your code is working quite fine with n=3
:您的代码在n=3
下工作得很好:
from scipy.signal import argrelextrema
n = 3
df['min'] = df.iloc[argrelextrema(df['fnv_wa'].values, np.less_equal,
order=n)[0]]['fnv_wa']
df['max'] = df.iloc[argrelextrema(df['fnv_wa'].values, np.greater_equal,
order=n)[0]]['fnv_wa']
ax = df.plot(y='fnv_wa')
df.plot(y='max', marker='o', color='g', ax=ax)
df.plot(y='min', marker='o', color='r', ax=ax)
output:输出:
With n=2
:与n=2
:
Rudimentary approach (will probably perform poorly at scale)基本方法(可能在规模上表现不佳)
local_minima = []
local_maxima = []
for i, row in df.iterrows():
if i > 0 and i < len(df)-1:
if df.loc[i,"fnv_wa"] < df.loc[i-1,"fnv_wa"] and df.loc[i,"fnv_wa"] < df.loc[i+1,"fnv_wa"]:
local_minima.append(i)
elif df.loc[i,"fnv_wa"] > df.loc[i-1,"fnv_wa"] and df.loc[i,"fnv_wa"] > df.loc[i+1,"fnv_wa"]:
local_maxima.append(i)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.