# Stack two arrays one of numbers between -1 and + 1 (size 1,3,3,2), and
# the other full of +1 (Size 1,3,3,1)
# The answer should have a shape that is 1,3,3,3
import numpy as np
b = 1
h = 3
w = 3
f_size = np.zeros = [b,h,w,2]
f = np.random.uniform(low=-1.0, high=1.0, size=f_size)
print("shape of flow is", np.shape(f))# (1,3,3,2)
#print(f)
ones = np.ones((b,h,w,1))
print("shape of ones is", np.shape(ones)) # (1,3,3,1)
stack = np.stack((f, ones), axis=4) # ValueError: all input arrays must
#have the same shape
#hstack = np.hstack((f, ones)) # ValueError: all the input array
#dimensions except for the concatenation axis must match exactly
#vstack = np.vstack((f, ones)) # ValueError: all the input array
#dimensions except for the concatenation axis must match exactly
#dstack = np.dstack((f,ones)) # ValueError: all the input array
#dimensions except for the concatenation axis must match exactly
print("shape with hstack is", np.shape(stack))
#print("shape with hstack is", np.shape(hstack))
#print("shape with vstack is", np.shape(vstack))
#print("shape with dstack is", np.shape(dstack))
预期形状为(1,3,3,3),则出现ValueError:除串联轴外的所有输入数组维必须完全匹配
答案 0 :(得分:0)
在numpy中,这种操作称为连接,而不是堆叠。
stack = np.concatenate((f, ones), axis=3)