NumPy: function for simultaneous max() and min()

后端 未结 12 873
天涯浪人
天涯浪人 2020-11-27 04:54

numpy.amax() will find the max value in an array, and numpy.amin() does the same for the min value. If I want to find both max and min, I have to call both functions, which

12条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-27 05:39

    Inspired by the previous answer I've written numba implementation returning minmax for axis=0 from 2-D array. It's ~5x faster than calling numpy min/max. Maybe someone will find it useful.

    from numba import jit
    
    @jit
    def minmax(x):
        """Return minimum and maximum from 2D array for axis=0."""    
        m, n = len(x), len(x[0])
        mi, ma = np.empty(n), np.empty(n)
        mi[:] = ma[:] = x[0]
        for i in range(1, m):
            for j in range(n):
                if x[i, j]>ma[j]: ma[j] = x[i, j]
                elif x[i, j]

提交回复
热议问题