I am implementing a function that reads data from file into a multi-dimensional numpy array. Data is regularly structured in sense of dimension lengths, however, some dimensions may be missing, in which case, I would let the length of that dimension be 0. So I have stumbled upon this behavior:
In [1]: np.random.random((3,3))
Out[1]:
array([[ 0.59756568, 0.47198749, 0.23442854],
[ 0.29374254, 0.58289927, 0.40497268],
[ 0.00481053, 0.63471263, 0.90053086]])
In [2]: np.random.random((0,3,3))
Out[2]: array([], shape=(0, 3, 3), dtype=float64)
OK, so I get an empty array. This makes sense if I look at it as 2nd and 3rd dimensions are subset of the 1st, which is nil, and thus the whole array is nil. However, I would expect np.random.random((3,3,0)) to be equivalent to np.random.random((3,3)). However,
In [3]: np.random.random((3,3,0))
Out[3]: array([], shape=(3, 3, 0), dtype=float64)
An empty array again.
Is this expected behavior? I understand the difference between np.array((3,3)) and np.array((3,3,1)) or np.array((1,3,3)), but I am looking for an explanation why does a dimension of length 0 degenerate the whole array and not only that dimension. Is it just me, or is this one of Python/numpy WTFs?
As I state in a comment, you are getting an empty array because the size of an array is always zero if any of the dimensions are zero. Can I ask what you are trying to do? If you want an empty 3rd dimension you can try something like the following: