python - 沿给定轴 1 将 numpy ndarray 与 3d 数组相加

标签 python arrays numpy

我有一个形状为 (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/

相关文章:

python - 如何在 emacs 24.x shell 中启用 pdbtrack python 调试?

python - 使用 owlready2 删除个人

php - 简单的 html dom 解析器表到数组

python - 如何翻译/移动一个numpy数组?

python - 将 Numpy 数组转换为图像

python - 值错误 : y contains new labels: ['#' ]

Python 3.7 Scapy 安装失败

javascript - 通过分组 camelCase 属性创建对象

ios - 使用 Objective-C 单击按钮时更改图像

python:为什么random.shuffle改变数组