58
60

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NumPyでのaxis指定

Posted at

ゼロから作るDeep Learningという本をコツコツと読み進めているのですが。。。

数値計算ライブラリNumPyを利用した、行列に対してaxis(軸)を指定して集計を行うという以下のような式

> m = np.array(...)
> m.sum(axis=0)

これがどう動くのか、いまいち脳の処理が追いつかないので、絵にしてみました。

ndim=2の場合

> m = np.array([[2,1],[3,4]])

axis=0

> m.sum(axis=0)
array([5, 5])

1.png

axis=1

> m.sum(axis=1)
array([3, 7])

1.png

ndim=3の場合

m = np.array([[[7,8],[2,1]],[[6,5],[3,4]]])

axis=0

> m.sum(axis=0)
array([[13, 13]
       [ 5,  5]])

1.png

axis=1

> m.sum(axis=1)
array([[9, 9]
       [9, 9]])

1.png

axis=2

> m.sum(axis=2)
array([[15, 3]
       [11, 7]])

1.png

58
60
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
58
60

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?