question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

np.float64(array) returns scalar for any single-element array (unlike np.float32, np.int64, etc.)

See original GitHub issue

Hi 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:open
  • Created 5 years ago
  • Reactions:1
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
eric-wiesercommented, Apr 28, 2018

The difference is that unlike all the other floating types, issubclass(np.float64, float) is true, and it seems some of the float behavior leaks through.

1reaction
mhvkcommented, Apr 27, 2018

(Changed the title to reflect more clearly what I think the underlying issue is)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found