I am attempting to create a numpy array of empty arrays without using loops. Using loops, I can use a simplified operation like
a = np.empty((3, 3), object)
for i in range(a.size):
a.ravel()[i] = np.array([])
Or a marginally more sophisticated approach based on np.nditer:
a = np.empty((3, 3), object)
it = np.nditer(a, flags=['multi_index', 'refs_ok'])
for i in it:
a[it.multi_index] = np.array([])
I can't seem to find an indexing expression that will allow me to make this assignment in a vectorized manner.
I've tried the following:
>>> a[:] = np.zeros((0, 3, 3))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not broadcast input array from shape (0,3,3) into shape (3,3)
>>> a[..., None] = np.zeros((3, 3, 0))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not broadcast input array from shape (3,3,0) into shape (3,3,1)
>>> a = np.full((3, 3), np.array([]), object)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.9/site-packages/numpy/core/numeric.py", line 343, in full
multiarray.copyto(a, fill_value, casting='unsafe')
File "<__array_function__ internals>", line 5, in copyto
ValueError: could not broadcast input array from shape (0,) into shape (3,3)
Even np.nditer does not allow me to write back:
>>> a = np.empty((3, 3), object)
>>> for x in np.nditer(a, flags=['refs_ok'], op_flags=['readwrite']):
... x[...] = np.array([])
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ValueError: could not broadcast input array from shape (0,) into shape ()
Is there any way to make an object array of empty arrays in a vectorized manner?
The exact output I'm looking for is
array([[array([], dtype=float64), array([], dtype=float64), array([], dtype=float64)],
[array([], dtype=float64), array([], dtype=float64), array([], dtype=float64)],
[array([], dtype=float64), array([], dtype=float64), array([], dtype=float64)]], dtype=object)
For the purposes of this question, I don't care if the references are all the same array or different arrays.
a[0, 0] = 'asdf'to store stuff in the array. Trying to do that with an array of shape(3, 3, 0)wouldn't store anything - it'd broadcast the assignment over a subarray of shape(0,)and store the string in the resulting 0 cells.