Finding local maxima/minima with Numpy in a 1D numpy array

后端 未结 12 1730
迷失自我
迷失自我 2020-11-22 15:13

Can you suggest a module function from numpy/scipy that can find local maxima/minima in a 1D numpy array? Obviously the simplest approach ever is to have a look at the neare

12条回答
  •  孤街浪徒
    2020-11-22 15:24

    Another one:

    
    def local_maxima_mask(vec):
        """
        Get a mask of all points in vec which are local maxima
        :param vec: A real-valued vector
        :return: A boolean mask of the same size where True elements correspond to maxima. 
        """
        mask = np.zeros(vec.shape, dtype=np.bool)
        greater_than_the_last = np.diff(vec)>0  # N-1
        mask[1:] = greater_than_the_last
        mask[:-1] &= ~greater_than_the_last
        return mask
    

提交回复
热议问题