发生原因
由于,每次append的数组大小不同造成的shape将每个数组看成一个独立的元素,如下:
x = np.array([[[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]],[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]],[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]]],
[[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]],[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]],[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]]],
[[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]],[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]],[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]]]]
)
y = np.array([[[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]],[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]],[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]]]
])
z = np.array([[[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]],[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]],[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]]],
[[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]],[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]],[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]]]]
)
s = []
s.append(x)
s.append(y)
s.append(z)
s = np.array(s)
print(s.shape)
结果为:
解决方法
y = []
for i in range(3):
s =np.array(x[i][:4])
y.append(s)
y = np.array(y)
print(y.shape)