You can always take a look at the .size attribute. It is defined as an integer, and is zero (0) when there are no elements in the array:
Use with AIimport numpy as np
a = np.array([])
if a.size == 0:
# Do something when `a` is empty
Answer from JoshAdel on Stack OverflowYou can always take a look at the .size attribute. It is defined as an integer, and is zero (0) when there are no elements in the array:
Use with AIimport numpy as np
a = np.array([])
if a.size == 0:
# Do something when `a` is empty
One caveat, though.
Note that np.array(None).size returns 1!
This is because a.size is equivalent to np.prod(a.shape),
np.array(None).shape is (), and an empty product is 1.
Use with AI>>> import numpy as np
>>> np.array(None).size
1
>>> np.array(None).shape
()
>>> np.prod(())
1.0
Therefore, I use the following to test if a NumPy array has elements:
Use with AI>>> def elements(array):
... return array.ndim and array.size
>>> elements(np.array(None))
0
>>> elements(np.array([]))
0
>>> elements(np.zeros((2,3,4)))
24
python - numpy.empty giving nonempty array - Stack Overflow
Numpy.Empty - Array Not Exactly Empty
python - Initialized empty numpy array is not really empty (contains zeros) - Stack Overflow
python - Numpy.empty() creating array with non-empty values - Stack Overflow
How do I check if a NumPy array is not empty?
Is there a numpy isempty() function?
Is an array of NaN values empty?
You didn't read the documentation, which says:
Return a new array of given shape and type, without initializing entries.
empty has nothing to do with creating an array that is "empty" in the sense of having no elements. It just means the array doesn't have its values initialized (i.e., they are unpredictable and depend on whatever happens to be in the memory allocated for the array).
foo = np.empty(0)
is what you need instead
you specified its size as 1, you need to set it as 0 instead like they told you in the comments
You need the shape to be (0, 3) so you have the correct number of columns to stack but have actually no data inside:
import numpy as np
pointsarrray = np.empty((0,3))
pointsarrray
# array([], shape=(0, 3), dtype=float64)
Maybe it's the name that is confusing you but numpy.emptys documentation explains what is happening:
Return a new array of given shape and type, without initializing entries.
So it just doesn't initialize the entries (sometimes they are random sometimes they are zero) but empty refers only to the value of the entries not to the shape of the array.
However it's really bad for performance to create an array by appending or stacking. Just collect them in a list and create the array afterwards:
pointsarray = [np.array(myPoint)]
# ...
pointsarray = np.array(pointsarray) # or np.stack(pointsarray, axis=...) whatever you need.
https://numpy.org/doc/stable/reference/generated/numpy.empty.html
It shows that the returned value of numpy.empty is an array with either very small numbers on the order of 1-300 or very large ones on the order of millions or billions.
Why doesn't it just return an empty array?
And why do they use these value instead of 0s? Don't these very small and large values actually take more space since there are more significant digits to represent? For an empty array we would want the lowest memory footprint which should be an array of all 0s.
How can using these numbers with many digits to be represented be faster than an array of all 0s (as the docs claim)?