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

后端 未结 12 875
天涯浪人
天涯浪人 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:20

    The shortest way I've come up with is this:

    mn, mx = np.sort(ar)[[0, -1]]
    

    But since it sorts the array, it's not the most efficient.

    Another short way would be:

    mn, mx = np.percentile(ar, [0, 100])
    

    This should be more efficient, but the result is calculated, and a float is returned.

提交回复
热议问题