This seems surprising to me:
import numpy as np
assert np.ndarray([0]).all()
assert not np.array([0]).all()
What is going on here?
Consider what those two calls produce:
>>> np.ndarray([0])
array([], dtype=float64)
This is an empty 1-D array, because you specified a single dimension of length 0.
>>> np.array([0])
array([0])
This is a 1-D array containing a single element, 0.
The definition of all() isn't just "all elements are True", it's also "no elements are False", as seen here:
>>> np.array([]).all()
True
So:
np.ndarray([0]).all()
is True because it's an empty array, while:
np.array([0]).all()
is False because it's an array of one non-True element.
ndarray and array were compatible... didn't realize the first arg to ndarray was actually a shape. Thank you!
np.ndarray([0]).all()returns empty array where asnp.array([0])returns array with 0 as element. np.all() checks if there is any False i.e (0)ndarrayis a lower level constructor that we don't usually use. It's best reserved for cases where you have a preexisting data buffer. Here's it's more likenp.empty.