python/numpy - find the maximum value, minimum value and index for maximum and minimum value - Max, NP max,np.amax,np.nanmax,np.fmax,np.maximum

Posted by elacdude on Thu, 03 Mar 2022 06:19:12 +0100


This article is based on PY3 8,system win10,numpy1.12.

np is the abbreviation of numpy.

1, Summary of reference sources and basic knowledge

Performance comparison between python built-in max() and min() functions and Numpy built-in max() and min() functions

Based on python 2.7.15 and system win10, the above article draws a conclusion: the running speed of max() built in python is better than numpy module, but it is convenient and flexible to use numpy Max is better.

numpy learning summarizes the differences between 36- max, Amax, nanmax, Fmax and maximum

The concepts of propagating Nan and ignoring Nan given in the above article refer to "whether to directly establish the result as Nan or ignore Nan when encountering Nan in the process of operation". For example, the sum() function propagates Nan by default, so the result of sum([1,2,np.nan]) is Nan. And NP Nansum() function ignores Nan by default, so NP The result of nansum ([1,2, NP. Nan]) is 3.

2, Calculate the maximum and minimum values in the container

Take the maximum value as an example. The effect of the minimum value is symmetrical to it.

Except that the maximum value is to pick it up when encountering Inf, and the maximum value is to throw it away when encountering Inf, the rest is basically the same, so I won't repeat it.

2.1 python built-in max() function

Function introduction: solve the maximum value of the iteratable object or the maximum value of multiple parameters

# Syntax 1: there is only one container
    max(iterable, *[, key, default])

# 1.1 containers are lists, arrays, tuples, sets, and strings
import numpy as np
print(max([1,2,3]))  # Output result 3
print(max(np.array([1,2,3])))  # Output result 3
print(max((1,2,3)))  # Output result 3
print(max({1,2,3}))  # Output result 3
print(max('I love you, my girl.'))  # The output is y

# 1.2 multidimensional containers and multi-layer nesting
print(max([[1, 2], [3, 4]]))  # The comparison is performed outside the list [2,4], and the output is the first shape [2,4]
print(max([[[1, 2], [3, 4]]]))  # List shape (1, 2, 2), output [[1, 2], [3, 4]], verify again
print(max((((1, 2, 3)),((2,3,4)))))  # Tuple shape is (2,1,3), output (2,3,4)
print(max(((((1, 2, 3)),((2,3,4))))))  # Whether the tuple shape is (1,2,1,3) or output (2,3,4) indicates that redundant parentheses will be removed for tuples

#1.3 key parameter usage, which specifies the location / attribute of the container to be compared
print(max([{'name':'zhang','age':13},{'name':'shen','age':56}],key=lambda x:x['age']))  # Output as {'name':'shen ','age': 56}

#1.4 default parameter usage specifies that if the maximum value cannot be obtained, the default value will be returned to avoid errors
print(max((),default=1))  # The output is 1
print(max(()))  # An empty tuple was passed in and the maximum value could not be obtained. Error ValueError: max() arg is an empty sequence

#1.5 if only one comparable parameter is passed in and this parameter is not a container, an error will be reported
print(max(1))  # TypeError: 'int' object is not iterable
print(max(1.3))  # TypeError: 'float' object is not iterable

# 2. Syntax 2: pass in multiple parameters (either a single value or a container)
    max(arg1, arg2, *args[, key])    

# 2.1 pass in multiple individual values
print(max(1,2,3,4,5,6))  # The output is 6

# 2.2 incoming multiple containers
print(max([1,2],[3,4],[6,-9999]))  # The output is [6, - 9999], indicating that only the first number is compared
print(max([[1,2],[3,4]],[[0,5],[6,7]]))  # The output is [[1, 2], [3, 4]], indicating that only the first number is compared

# 2.3 use key between multiple parameters
print(max({'name':'zhang','age':13},{'name':'shen','age':56},key=lambda x:x['age']))  # Output as {'name':'shen ','age': 56}

# 3. Other precautions

# 3.1 never add the parameter axis, because the built-in max function does not have this parameter

# 3.2 the built-in max() function will propagate nan, while numpy Nanmax() can avoid nan
print(max([np.nan,1,2,3]))  # The output is nan, indicating that the built-in max function will propagate nan
print(np.nanmax([np.nan,1,2,3]))  # 3.0


2.2 NP. Of numpy module Max function (same as np.amax), NP Nanmax function

import numpy as np

# 1,np.max is actually NP amax
np.max.__name__        # 'amax'
np.max is np.amax      # True
np.amax                # <function numpy.core.fromnumeric.amax>
np.max                 # <function numpy.core.fromnumeric.amax>

# 2. Grammar
    np.amax(array_like, axis=None, out=None, keepdims=<no value>, initial=<no value>)

    2.1 array_like: It can be an array or a container similar to an array (tuple and list). Only one such parameter can be passed
    2.2 axis: This is python built-in max()There is no parameter to specify which dimension to compare
    2.3 keepdims: Keep dimension
    2.4 As long as it includes nan,Just return nan,That is, communication nan. 
    2.5 As long as it includes Inf,Just return Inf,because Inf Is positive infinity.

# 3. Examples

# 3.1 when axis=None, compare all numbers to get a maximum value
print(np.max([12, 34]))  # 34
print(np.max([[12, 34], [56, 78]]))  # The result is 78 instead of [56,78], indicating that when axis=None is not specified, the members under all dimensions are compared and finally a maximum value is returned
print(np.max([[[[[[12, 34], [56, 78]]]]]]))  # Output 78 indicates that no matter how many layers are nested, the largest element is returned, which is different from the built-in max()

# 3.2 when axis= X (an integer), compare on the specified dimension
print(np.max([[12, 34], [10, 78]], axis=0))  # [12 78]
print(np.max([[12, 34], [10, 78]], axis=1))  # [34 78]

# 3.3 when keepdims=True, the dimension specified by axis will not be zeroed
print(np.max([[12, 34], [10, 78]], axis=0, keepdims=True))  # [[12 78]]
print(np.max([[12, 34], [10, 78]], axis=1, keepdims=True))  # [[34], [78]]

As for NP Nanmax, in addition to ignoring nan, has other features similar to NP max(),np. Same as Amax ().

2.3 NP. Of numpy module Maximum() function

# 1. Grammar
    np.maximum(X, Y, out=None)
# 2. Introduction
    # 2.1 compare X and Y bit by bit, and select the maximum value. You must accept two objects for comparison. No more or less
    # 2.2 will spread NAN; If Inf exists, return Inf directly

# 3. Code implementation
>>> np.maximum([2, 3, 4], [1, 5, 2])
array([2, 5, 4])

>>> np.maximum(np.eye(2), [0.5, 2]) # broadcasting
array([[ 1. ,  2. ],
       [ 0.5,  2. ]])
>>> np.maximum([np.nan, 0, np.nan], [0, np.nan, np.nan])
array([ NaN,  NaN,  NaN])

>>> np.maximum(np.Inf, 1)


2.4 NP. Of numpy module Fmax function

np.fmax function and NP Discrimination of maximum function:

2.4.1 common ground:

(1) compare the element values of the two parameters and finally return the maximum value corresponding to the position.

(2) you can use the broadcast mechanism.

(3) only two comparison objects must be passed in, and an error will be reported if there is more or less

(4) they are all ufunc functions, which can be used in combination with attributes such as calculate, reduce and reduceat.

For point 4, see:

2.4.2 differences: NP Fmax ignores Nan and NP Maximum propagates Nan. Memory skills: f is forget, forget Nan, don't know who it is, and ignore it directly.

import numpy as np

# 1. Grammar
    np.fmax(x1, x2)

# 2. Introduction
    # 2.1 x1 and x2 are a numerical value or a container (array, list, tuple) respectively. They are compared element by element and return the maximum value (floating-point type) at each position,
    # 2.2 NAN will be ignored, but if both numbers are NAN, NAN will be returned; Once there is Inf in a certain position, Inf must be returned
    # 2.3 two objects for comparison must be accepted, and errors must be reported for more and less

# 3. Implementation code
# 3.1 pass in two numbers
print(np.fmax(1, 3))  # 3
print(np.fmax(1, 3, 2, 4, 5, 6))  #TypeError: fmax() takes from 2 to 3 positional arguments but 6 were given

# 3.2 if there is nan in the container, avoid it if you can, and use it if you can't avoid it; If there is Inf in the container, it must be used
print(np.fmax([1,np.nan],[2,999]))  # [ 2. 999.]
print(np.fmax([1,np.nan],[2,np.nan]))  # [ 2. nan]
print(np.fmax([1,np.Inf],[2,999]))  # [ 2. inf]

Topics: Python Back-end