Home > Software engineering >  Finding the local maxima and local minima in the data python
Finding the local maxima and local minima in the data python

Time:06-30

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:

  1. Approach 1: Using scipy.signal's argrelextremafrom to find the local maxima's and minima's, but the limitation is when the data window is large, its now able to identify.

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']
  1. Approach 2: Using dataframes shift functionality:

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.

enter image description here

Please suggest an approach that can find all the local maxima and local minima in my data.

CodePudding user response:

Which value to you use for n?

Your code is working quite fine with 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:

enter image description here

With n=2:

enter image description here

CodePudding user response:

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)
  • Related