numpy.float() returns non-numpy python builtin float
See original GitHub issuenumpy.float()
returns a non-numpy python builtin float type (same goes for numpy.int()
):
>>> print type(np.float(0))
<type 'float'>
>>> print type(np.int(0))
<type 'int'>
This is different to behavior of dtype
options in array initialization:
>>> print type(np.arange(2, dtype=np.float)[0])
<type 'numpy.float64'>
>>> print type(np.arange(2, dtype=np.int)[0])
<type 'numpy.int64'>
I find this unexpected and surprising. This may have little practical impact as it only affects initialization of single numbers, but it means that e.g. numpy floating point operation error handling is circumvented in this case (which is how I stumbled upon this):
>>> np.seterr(all="ignore")
{'over': 'ignore', 'divide': 'ignore', 'invalid': 'ignore', 'under': 'ignore'}
>>> np.geterr()
{'over': 'ignore', 'divide': 'ignore', 'invalid': 'ignore', 'under': 'ignore'}
>>> np.float(0) / np.float(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: float division by zero
Issue Analytics
- State:
- Created 10 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Data types — NumPy v1.24 Manual
To convert the type of an array, use the .astype() method (preferred) or the type itself as ... Note that, above, we use...
Read more >How to check the size of a float in python? - Stack Overflow
Properties of a Python float can be requested via sys.float_info . It returns information such as max/min value, max/min exp value, etc.
Read more >Using NumPy to Convert Array Elements to Float Type
Method 1 : Here, we can utilize the astype() function that is offered by NumPy. This function creates another copy of the initial...
Read more >NumPy Data Types - W3Schools
e.g. -1, -2, -3; float - used to represent real numbers. ... The NumPy array object has a property called dtype that returns...
Read more >Python: Convert float to int in numpy array
I'm trying to convert floats to integer in python and store the integer in ... try: _password+=int(i) except: _password+=i return _password.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
#6103 suggests deprecating these, for those following the trail
Specifically,
numpy.float
andnumpy.int
are only present because of backwards compatibility with a very early version of numpy that exposednumpy.float64
andnumpy.int32
(ornp.int64
on appropriate 64-bit platforms) under those names. It was soon realized thatfrom numpy import *
caused the builtinfloat
andint
to be obscured. However, because some people had started usingdtype=numpy.float
explicitly, we kept the name aliased tofloat
such that it would continue working.Please do not use these aliases. If you see documentation using them, please let us know so we can correct it.