I want create 3d array with unknown length and append to the array as follows:
a = np.array([])
for n in range(1,3):
data=np.full((2,3),n)
a = np.append(a,data,axis=0)
print(a)
The expected output is:
[[[1,1,1]
[1,1,1]]
[[2,2,2]
[2,2,2]]]
However, I got an error:
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 2 dimension(s)
How to declare an empty 3d numpy array with unknown length and append to it?
np.array([]).shapegives a result. So doesnp.array([[]]).shapeornp.zeros((0,2,3)).shape.np.appendis a cover function fornp.concatenate. Especially when axis is provided all it does isnp.concatenate((a,b), axis=0). That means you have to understand dimensions, and understand whatconcatenateexpects. There's no room for sloppiness here.