arrays.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:extract 作者: dblalock 项目源码 文件源码
def idxsOfRelativeExtrema(x, maxima=True, allowEq=False, axis=0):
    """
    >>> idxsOfRelativeExtrema([2,1,5])
    array([0, 2])
    >>> idxsOfRelativeExtrema([2,1])
    array([0])
    >>> idxsOfRelativeExtrema([1,2])
    array([1])
    >>> idxsOfRelativeExtrema([2,1,5], maxima=False)
    array([1])
    >>> idxsOfRelativeExtrema([1,1,1], allowEq=False)
    array([], dtype=int64)
    >>> idxsOfRelativeExtrema([0,1,1,1,0], allowEq=False)
    array([], dtype=int64)
    >>> idxsOfRelativeExtrema([1,1,1], allowEq=True)
    array([0, 1, 2])
    """
    if len(x) == 0:
        return np.empty(1) # []
    if len(x) == 1:
        return np.zeros(1) # [0]

    x = np.asarray(x)
    pad = -np.inf if maxima else np.inf
    if maxima:
        if allowEq:
            func = np.greater_equal
        else:
            func = np.greater
    else:
        if allowEq:
            func = np.less_equal
        else:
            func = np.less
    if len(x.shape) == 1:
        x = np.r_[x, pad] # combine with wrap to check endpoints
        return signal.argrelextrema(x, func, mode='wrap')[0]
    elif axis == 0:
        pad = np.zeros((1, x.shape[1])) + pad
        x = np.vstack((x, pad))
        return signal.argrelextrema(x, func, mode='wrap', axis=axis)
    elif axis == 1:
        pad = np.zeros((x.shape[0], 1)) + pad
        x = np.hstack((x, pad))
        return signal.argrelextrema(x, func, mode='wrap', axis=axis)
    else:
        raise NotImplementedError("only supports axis={0, 1}!")
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号