简体   繁体   中英

Logic behind inbuilt function argrelextrema Python

There is this function argrelextrema in scipy.signal which finds local extrema in an array. This is what I tried :

import numpy as np

from scipy.signal import argrelextrema

z = np.array([[56,32,12,10,13],[33,55,77,32,11],[12,66,89,43,33]])

argrelextrema(z, np.greater)

Output :

(array([], dtype=int64), array([], dtype=int64))

Aren't the elements z[1][2] and z[2][2] clearly local extrema? Then why don't they appear in the output? Thanks in advance.

argrelextrema takes an axis arg, that default to 0 , which can be counter-intuitive. So on your main example, they are indeed no local maximum in this case. On your second example in answer to @tamasgal, 12 < 98 > 89, so there is indeed a local maximum, at row 1, column 2 as stated by the output.

If you set axis=1 in your call, this would return the correct result:

import numpy as np
from scipy.signal import argrelextrema

z = np.array([[56,32,12,10,13],[33,55,77,32,11],[12,66,89,43,33]])

argrelextrema(z, np.greater, axis=1)

Output:

(array([1, 2]), array([2, 2]))

Note that there is also the argrelmax alias if you want to use np.greater as operator.

argrelextrema仅适用于一维数组。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM