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)
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)
You're looking for numpy.iinfo for integer types. Documentation here.
There's also numpy.finfo for floating point types. Documentation here.
np.max doesn't work for comparing float numbers
python - How to get the range of valid Numpy data types? - Stack Overflow
floating point - Python: Return max float instead of infs? - Stack Overflow
python - Numpy 2d array with float values find the maximum value in a single row and store in another array - Stack Overflow
Quoting from a numpy discussion list:
That information is available via
numpy.finfo()andnumpy.iinfo():In [12]: finfo('d').max Out[12]: 1.7976931348623157e+308 In [13]: iinfo('i').max Out[13]: 2147483647 In [14]: iinfo('uint8').max Out[14]: 255
Link here.
You can use numpy.iinfo(arg).max to find the max value for integer types of arg, and numpy.finfo(arg).max to find the max value for float types of arg.
>>> numpy.iinfo(numpy.uint64).min
0
>>> numpy.iinfo(numpy.uint64).max
18446744073709551615L
>>> numpy.finfo(numpy.float64).max
1.7976931348623157e+308
>>> numpy.finfo(numpy.float64).min
-1.7976931348623157e+308
iinfo only offers min and max, but finfo also offers useful values such as eps (the smallest number > 0 representable) and resolution (the approximate decimal number resolution of the type of arg).
You can use a decorator:
import sys
def noInf(f):
def wrapped(*args, **kwargs):
res = f(*args, **kwargs)
if res == float('inf'):
return sys.float_info.max
return res
return wrapped
@noInf
def myMult(x, y):
return x*y
print(myMult(sys.float_info.max, 2)) # prints 1.79769313486e+308
I don't know about 'replacing' infinity value, but what about using min()?
Something like (assuming x yields your value):
return min ( [ x, sys.float_info.max ] )
Upd: it was originally wrong, now this is just the essence of the the previous correct answer:
a = np.array([[0.0543275 , 0.51249827, 0.43317423],
[0.07144389, 0.51152126, 0.41703486],
[0.0776112 , 0.48593384, 0.43645496]])
b = np.zeros_like(a)
b[np.arange(a.shape[0]), np.argmax(a, axis=1)] = 1
Since np.argmax() gives us indices of the max elements, we just use them for indexing directly. Now b contains desired output:
array([[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.]])
you can also do: b.astype(int) to turn to integers.
Here is an option that works
for e, i in enumerate(a):
for f, j in enumerate(i):
if j == max(i):
a[e][f] = 1
else:
a[e][f] = 0
This will convert the array that you use to the desired form:
<class 'numpy.ndarray'>
[[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]]
Can you rework the calculation so it works with the logarithms of the numbers instead?
That's pretty much how the built-in floats work in any case...
You would only convert the number back to linear for display, at which point you'd separate the integer and fractional parts; the fractional part gets exponentiated as normal to give the 8 digits of precision, and the integer part goes into the "×10ⁿ" or "×eⁿ" or "×2ⁿ" part of the output (depending on what base logarithm you use).
You might want to look at bigfloat. It fits your need for unlimited size, but unfortunately, it only provides basic operations (abs, max, min, pow, round, sum) and lacks any NumPy infrastructure.
A more feature-rich alternative is mpmath. It also supports arbitrary-precision arithmetic and offers a much wider range of mathematical functions, though it too operates outside of NumPy.
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
>>>
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