沿特定维度将一个 numpy ndarray 附加到另一个
append an numpy ndarray to another one along certain dimensions
存在两个numpy.ndarray
,例如A
的形状是(5,3,2)
,B
的形状是(5,3)
。
A = np.random.rand(5,3,2)
B=np.random.rand(5,3) #----- there was typo here
我想将 B 附加到 A 中,并使结果数组 C
的形状为 (5,3,3)
我尝试使用 np.contaenate
,但它不起作用。
c=np.concatenate((a,b),axis=2)
import numpy as np
a = np.random.rand(5,3,2)
b = np.random.rand(5,3)
b = b[..., np.newaxis] # or np.expand_dims(b, axis=2) or b.reshape(b.shape+(1,))
print(b) # (5, 3, 1)
c = np.append(a, b, axis=2)
print(c.shape) # (5, 3, 2)
存在两个numpy.ndarray
,例如A
的形状是(5,3,2)
,B
的形状是(5,3)
。
A = np.random.rand(5,3,2)
B=np.random.rand(5,3) #----- there was typo here
我想将 B 附加到 A 中,并使结果数组 C
的形状为 (5,3,3)
我尝试使用 np.contaenate
,但它不起作用。
c=np.concatenate((a,b),axis=2)
import numpy as np
a = np.random.rand(5,3,2)
b = np.random.rand(5,3)
b = b[..., np.newaxis] # or np.expand_dims(b, axis=2) or b.reshape(b.shape+(1,))
print(b) # (5, 3, 1)
c = np.append(a, b, axis=2)
print(c.shape) # (5, 3, 2)