pythonnumpy

Why do I get a value error while creating a ragged array but only for a certain shape?


import numpy as np

for h in range(10):
    try:
        array = np.array([np.zeros((h, 4)), np.zeros((3, h))], dtype=object)
    except ValueError:
        print(f'Value Error for h={h} only.')

In the above code, ValueError only happens for h=3. This seems arbitrary.

The full error being,

  File "path/to/arr.py", line 4, in <module>
    array = np.array([np.zeros((h, 4)), np.zeros((3, h))], dtype=object)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: could not broadcast input array from shape (3,4) into shape (3,)

How may I avoid this and why does this happen?


Solution

  • import numpy as np
    
    for h in range(10):
        array = np.empty(2, dtype=object)
        array[0] = np.zeros((h, 4))
        array[1] = np.zeros((3, h))