我有一个形状为 (2,3,3) 的 numpy ndarray,例如:
array([[[ 1, 2, 3],
[ 4, 5, 6],
[12, 34, 90]],
[[ 4, 5, 6],
[ 2, 5, 6],
[ 7, 3, 4]]])
我迷失在 np.sum(高于 ndarray ,axis=1)中,为什么答案是:
array([[17, 41, 99],
[13, 13, 16]])
谢谢
最佳答案
Axes are defined for arrays with more than one dimension. A 2-dimensional array has two corresponding axes: the first running vertically downwards across rows (axis 0), and the second running horizontally across columns (axis 1).
设 A 为数组,然后在您的示例中,当轴为 1 时,添加 [i,:,k]。同样,对于轴 0,添加 [:,j,k],当轴为 2 时,添加 [i,j,:]。
A = np.array([
[[ 1, 2, 3],[ 4, 5, 6], [12, 34, 90]],
[[ 4, 5, 6],[ 2, 5, 6], [ 7, 3, 4]]
])
np.sum(A,axis = 0)
array([[ 5, 7, 9],
[ 6, 10, 12],
[19, 37, 94]])
np.sum(A,axis = 1)
array([[17, 41, 99],
[13, 13, 16]])
np.sum(A,axis = 2)
array([[ 6, 15,136],
[15, 13, 14]])
关于python - 沿给定轴 1 将 numpy ndarray 与 3d 数组相加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37142135/