np.argmax を使ってみる
Python
Published: 2019-09-10

やったこと

np.argmax を使ってみます。

確認環境

$ python
Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 21 2017, 18:29:43)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.__version__
'1.16.4'

調査

>>> b = [0, 5, 2, 3, 4, 5]
>>> argmax(b)
>>> np.argmax(b)
1
>>> a = np.random.random(20)
>>> a = a.reshape(4, 5)
>>> a
array([[0.49340889, 0.29507221, 0.56335119, 0.83046262, 0.7579564 ],
       [0.10089851, 0.43410885, 0.5479143 , 0.55304626, 0.92677723],
       [0.21139963, 0.06923998, 0.15509698, 0.33185919, 0.70188447],
       [0.58790613, 0.74386543, 0.25404441, 0.67682735, 0.45157852]])
>>> np.argmax(a)
9
>>> np.argmin(a)
11
>>> np.argmax(a, axis=0)
array([3, 3, 0, 0, 1])
>>> np.argmax(a, axis=1)
array([3, 4, 4, 1])

axis を指定しない場合、flatな配列としてみたときの indexが返されます。 また、axis=0で列ごと、axis=1で行ごとに見ます。

参考