pandas 寻找局部最大值和局部最小值

hgb9j2n6  于 2023-09-29  发布在  其他
关注(0)|答案(2)|浏览(119)

数据类型:

+---------------------+------------------+--+ 
|      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        |  |
+---------------------+------------------+--+

我试图找到我的数据中的所有局部最大值和最小值,我尝试的方法如下所示:
1.方法一:使用scipy.signal的argrelextremafrom来找到局部最大值和最小值,但限制是当数据窗口很大时,它现在能够识别。
实施情况:

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.方法二:使用 Dataframe 移位功能:
实施情况:

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)]

上述两种方法的问题是,在X~10处的局部最大值未被检测到。

请建议一种方法,可以找到所有的局部最大值和局部最小值在我的数据。

5f0d552i

5f0d552i1#

n使用哪个值?
您的代码在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)

产出:

n=2

9vw9lbht

9vw9lbht2#

基本方法(在大规模下可能表现不佳)

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)

相关问题