numpy max vs amax vs maximum

后端 未结 4 1550
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 19:31

numpy has three different functions which seem like they can be used for the same things --- except that numpy.maximum can only be used element-wise, w

4条回答
  •  天涯浪人
    2020-11-28 19:53

    For completeness, in Numpy there are four maximum related functions. They fall into two different categories:

    • np.amax/np.max, np.nanmax: for single array order statistics
    • and np.maximum, np.fmax: for element-wise comparison of two arrays

    I. For single array order statistics

    NaNs propagator np.amax/np.max and its NaN ignorant counterpart np.nanmax.

    • np.max is just an alias of np.amax, so they are considered as one function.

      >>> np.max.__name__
      'amax'
      >>> np.max is np.amax
      True
      
    • np.max propagates NaNs while np.nanmax ignores NaNs.

      >>> np.max([np.nan, 3.14, -1])
      nan
      >>> np.nanmax([np.nan, 3.14, -1])
      3.14
      

    II. For element-wise comparison of two arrays

    NaNs propagator np.maximum and its NaNs ignorant counterpart np.fmax.

    • Both functions require two arrays as the first two positional args to compare with.

      # x1 and x2 must be the same shape or can be broadcast
      np.maximum(x1, x2, /, ...);
      np.fmax(x1, x2, /, ...)
      
    • np.maximum propagates NaNs while np.fmax ignores NaNs.

      >>> np.maximum([np.nan, 3.14, 0], [np.NINF, np.nan, 2.72])
      array([ nan,  nan, 2.72])
      >>> np.fmax([np.nan, 3.14, 0], [np.NINF, np.nan, 2.72])
      array([-inf, 3.14, 2.72])
      
    • The element-wise functions are np.ufunc(Universal Function), which means they have some special properties that normal Numpy function don't have.

      >>> type(np.maximum)
      
      >>> type(np.fmax)
      
      >>> #---------------#
      >>> type(np.max)
      
      >>> type(np.nanmax)
      
      

    And finally, the same rules apply to the four minimum related functions:

    • np.amin/np.min, np.nanmin;
    • and np.minimum, np.fmin.

提交回复
热议问题