LoginSignup
2
1

More than 3 years have passed since last update.

知らなかったnumpyの関数まとめ

Last updated at Posted at 2020-03-15
$ python
>>> import numpy as np

maximum

>>> np.maximum([1,2], [3,1]) # array([3, 2])
>>> np.maximum(0, [-1, 2, -2, 4]) # array([0, 2, 0, 4])

argmax

>>> np.argmax([1,3,4,2]) # 2

max

>>> a = np.array([[1,4],[2,3]])
>>> np.max(a, axis=0) # array([2, 4])
>>> np.max(a, axis=1) # array([4, 3])

diagonal

>>> a = np.arange(9).reshape(3,3) # array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
>>> a.diagonal() # array([0, 4, 8])
>>> np.fliplr(a).diagonal() # array([2, 4, 6])

allclose

行列が近似的に等しいか調べる

where

>>> test = np.array([True, False])
>>> np.where(test, 1, 2) # array([1, 2])
2
1
0

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
2
1