60
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

NumPyでのaxis指定

ゼロから作る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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
60
Help us understand the problem. What are the problem?