np.float64(array) returns scalar for any single-element array (unlike np.float32, np.int64, etc.)
See original GitHub issueHi all, I’m reporting a problem when using np.float64
as a constructor.
For any a = np.array([[scalar]])
assuming type(scalar) in (int, float)
, this raises AssertionError
:
assert np.float64(a).shape is a.shape
The shape after casting to np.float64
is ()
instead of the expected (1,1)
. We can see what’s going on more clearly when only this last assertion fails:
scalar = 0.0 # example
a = np.array([[scalar]])
assert type(a) is np.ndarray
assert type(np.float64(a)) is np.ndarray #AssertionError
The problem is that np.float64(a) is np.float64(scalar)
. Contrary to expectations, np.float64(a) is not np.float64([[scalar]])
This happens with v1.14.2
as well as master
. I’ve tested with python 3.6.4
/2.7.11
on ubuntu 16.04
/Mac OS 10.12.3
.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:13 (6 by maintainers)
Top Results From Across the Web
Converting numpy dtypes to native python types
Generally, problems are easily fixed by explicitly converting array scalars to Python scalars, using the corresponding Python type function (e.g., int, float, ...
Read more >Data types — NumPy v1.24 Manual
NumPy generally returns elements of arrays as array scalars (a scalar with an associated dtype). Array scalars differ from Python scalars, but for...
Read more >NumPy Reference
NumPy provides an N-dimensional array type, the ndarray, ... of the array, 3) the array-scalar Python object that is returned when a single...
Read more >1.4.3. More elaborate arrays
Different data type sizes¶ ; float16, 16 bits ; float32, 32 bits ; float64, 64 bits (same as float ) ; float96, 96...
Read more >Multi-dimensional Arrays - Julia Documentation
creates a one-dimensional array (i.e., a vector) containing the comma-separated arguments as its elements. The element type ( eltype ) of the resulting...
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 Free
Top 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
The difference is that unlike all the other floating types,
issubclass(np.float64, float)
is true, and it seems some of thefloat
behavior leaks through.(Changed the title to reflect more clearly what I think the underlying issue is)