min_value = np.iinfo(im.dtype).min
max_value = np.iinfo(im.dtype).max

docs:

  • np.iinfo (machine limits for integer types)
  • np.finfo (machine limits for floating point types)
Answer from Bruno Gelb on Stack Overflow
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.max.html
numpy.max — NumPy v2.2 Manual
>>> import numpy as np >>> a = np.arange(4).reshape((2,2)) >>> a array([[0, 1], [2, 3]]) >>> np.max(a) # Maximum of the flattened array 3 >>> np.max(a, axis=0) # Maxima along the first axis array([2, 3]) >>> np.max(a, axis=1) # Maxima along the second axis array([1, 3]) >>> np.max(a, where=[False, True], initial=-1, axis=0) array([-1, 3]) >>> b = np.arange(5, dtype=float) >>> b[2] = np.nan >>> np.max(b) np.float64(nan) >>> np.max(b, where=~np.isnan(b), initial=-1) 4.0 >>> np.nanmax(b) 4.0 ·
Discussions

np.max doesn't work for comparing float numbers
print (np.max(450.0802234473462, 85.0)) File "<__array_function__ internals>", line 6, in amax File "E:\miniconda3\envs\python37\lib\site-packages\numpy\core\fromnumeric.py", line 2621, in amax keepdims=keepdims, initial=initial, where=where) File "E:\miniconda3\envs\python37\lib\site-packages\numpy\core\fromnumeric.py", line 90, in _wrapreduction return ufunc.reduce(obj, axis, dtype, out, **passkwargs) TypeError: 'float... More on github.com
🌐 github.com
2
February 17, 2020
python - How to get the range of valid Numpy data types? - Stack Overflow
I'm interested in finding for a particular Numpy type (e.g. np.int64, np.uint32, np.float32, etc.) what the range of all possible valid values is (e.g. np.int32 can store numbers up to 2**31-1). Of course, I guess one can theoretically figure this out for each type, but is there a way to do this at run time to ensure more portable code? ... Save this answer. ... Show activity on this post. ... CopyIn [12]: finfo('d').max ... More on stackoverflow.com
🌐 stackoverflow.com
floating point - Python: Return max float instead of infs? - Stack Overflow
I have several functions with multiple calculations that might return inf, like so: In [10]: numpy.exp(5000) Out[10]: inf I'd rather it return the maximum float value: In [11]: sys.float_info.ma... More on stackoverflow.com
🌐 stackoverflow.com
python - Numpy 2d array with float values find the maximum value in a single row and store in another array - Stack Overflow
8 Find maximum of each row in a numpy array and the corresponding element in another array of the same size More on stackoverflow.com
🌐 stackoverflow.com
🌐
Note.nkmk.me
note.nkmk.me › home › python
Maximum and Minimum float Values in Python | note.nkmk.me
August 11, 2023 - However, in Python, the double-precision type is named float, and there's no dedicated single-precision type. Note that in NumPy, you can explicitly specify the type with the number of bits, such as float32 or float64.
🌐
GitHub
github.com › numpy › numpy › issues › 15586
np.max doesn't work for comparing float numbers · Issue #15586 · numpy/numpy
February 17, 2020 - Reproducing code example: import numpy as np print (np.max(450.0802234473462, 85.0)) Error message: print (np.max(450.0802234473462, 85.0)) File " ", line 6, in amax File "E:\miniconda3\envs\python37\lib\site...
Author: numpy
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.max.html
numpy.max — NumPy v2.5 Manual
>>> import numpy as np >>> a = np.arange(4).reshape((2,2)) >>> a array([[0, 1], [2, 3]]) >>> np.max(a) # Maximum of the flattened array 3 >>> np.max(a, axis=0) # Maxima along the first axis array([2, 3]) >>> np.max(a, axis=1) # Maxima along the second axis array([1, 3]) >>> np.max(a, where=[False, True], initial=-1, axis=0) array([-1, 3]) >>> b = np.arange(5, dtype=np.float64) >>> b[2] = np.nan >>> np.max(b) np.float64(nan) >>> np.max(b, where=~np.isnan(b), initial=-1) 4.0 >>> np.nanmax(b) 4.0 ·
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.finfo.html
numpy.finfo — NumPy v2.5 Manual
The number of bits in the exponent portion of the floating point representation. ... The exponent that yields eps. ... The largest representable number. ... The smallest positive power of the base (2) that causes overflow. Corresponds to the C standard MAX_EXP.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › article › get-the-machine-limits-information-for-float-types-in-python
Get the Machine limits information for float types in Python
February 24, 2022 - -65500.0 Maximum of float16 type... 65500.0 · Check the machine limits for 32-bit floating-point numbers ? import numpy as np # Get machine limits for float32 b = np.finfo(np.float32) print("Minimum of float32 type...") print(b.min) print("Maximum ...
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.iinfo.html
numpy.iinfo — NumPy v2.5 Manual
Maximum value of given dtype. ... The equivalent for floating point data types.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › find maximum float value in python
Find Maximum Float Value in Python - Spark By {Examples}
May 31, 2024 - How to find the maximum float value in Python? You can find the maximum value of the float data type using the sys.float_info module or the finfo()
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.max.html
numpy.max — NumPy v2.0 Manual
>>> a = np.arange(4).reshape((2,2)) >>> a array([[0, 1], [2, 3]]) >>> np.max(a) # Maximum of the flattened array 3 >>> np.max(a, axis=0) # Maxima along the first axis array([2, 3]) >>> np.max(a, axis=1) # Maxima along the second axis array([1, 3]) >>> np.max(a, where=[False, True], initial=-1, axis=0) array([-1, 3]) >>> b = np.arange(5, dtype=float) >>> b[2] = np.nan >>> np.max(b) np.float64(nan) >>> np.max(b, where=~np.isnan(b), initial=-1) 4.0 >>> np.nanmax(b) 4.0 ·
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.max.html
numpy.max — NumPy v2.1 Manual
>>> import numpy as np >>> a = np.arange(4).reshape((2,2)) >>> a array([[0, 1], [2, 3]]) >>> np.max(a) # Maximum of the flattened array 3 >>> np.max(a, axis=0) # Maxima along the first axis array([2, 3]) >>> np.max(a, axis=1) # Maxima along the second axis array([1, 3]) >>> np.max(a, where=[False, True], initial=-1, axis=0) array([-1, 3]) >>> b = np.arange(5, dtype=float) >>> b[2] = np.nan >>> np.max(b) np.float64(nan) >>> np.max(b, where=~np.isnan(b), initial=-1) 4.0 >>> np.nanmax(b) 4.0 ·
🌐
Real Python
realpython.com › numpy-max-maximum
NumPy's max() and maximum(): Find Extreme Values in Arrays – Real Python
October 22, 2025 - But here, you just want to get the best view of the weekly maximum values. The solution, in this case, is another NumPy package function, np.fmax(): ... Now, two of the missing values have simply been ignored, and the remaining floating-point value at that index has been taken as the maximum.
🌐
SciPy
docs.scipy.org › doc › numpy-1.13.0 › reference › generated › numpy.iinfo.html
numpy.iinfo — NumPy v1.13 Manual
The equivalent for floating point data types. ... >>> ii16 = np.iinfo(np.int16) >>> ii16.min -32768 >>> ii16.max 32767 >>> ii32 = np.iinfo(np.int32) >>> ii32.min -2147483648 >>> ii32.max 2147483647
Top answer
1 of 2
5

numpy.max is the same thing as numpy.amax:

>>> import numpy
>>> numpy.max # Notice it says 'amax' in the output
<function amax at 0x0228B5D0>
>>> numpy.max is numpy.amax
True
>>>

Or, more specifically, max is an alias for the amax function.

The purpose of this function is listed in the docs link you gave, but it seems that it is mainly used to find the maximum value inside a numpy.array regardless of how many nested levels it has. You can mimic this behavior with a simple function to flatten a list:

def flatten(lst):
    for item in lst:
        if isinstance(item, list):
            # Use 'yield from flatten(item)' in Python 3.3 or greater
            for sub_item in flatten(item):
                yield sub_item
        else:
            yield item

and the built-in max function:

max(flatten(my_list))

See a demonstration below:

>>> def flatten(lst):
...     for item in lst:
...         if isinstance(item, list):
...             for sub_item in flatten(item):
...                 yield sub_item
...         else:
...             yield item
...
>>> array = [[1, 2, 3], [4, 5, 6]]
>>> max(flatten(array))
6
>>>
2 of 2
1

You can't replicate the behavior of np.max very easily in pure Python, simply because multi-dimensional arrays aren't standard in Python. If the A and B in your code are such arrays, it would be best to keep the NumPy function.

For flat (one-dimensional) arrays, the Python max and np.max do the same thing and could be exchanged:

>>> a = np.arange(27)
>>> max(a)
26
>>> np.max(a)
26

For arrays with more than one dimension, max won't work:

>>> a = a.reshape(3, 3, 3)
>>> max(a)
ValueError: The truth value of an array with more than one element is ambiguous [...]
>>> np.max(a)
26

By default, np.max flattens the 3D array and returns the maximum. (You can also find the maximum along particular axes, and so on.) The Python max cannot do this.

To replace np.max, you'd need to write nested loops over the axes of the array; effectively trying to find the maximum in a list of nested lists. This is certainly possible, but is likely to be very slow:

>>> max([max(y) for y in x for x in a])
26