Numpyの基本操作,PythonのArray処理

特に書いていない場合でも、
import numpy as np
あるいは
from numpy import *
を書いているものとしてください。

参考:Numpyクイックチュートリアル
https://docs.scipy.org/doc/numpy-dev/user/quickstart.html

配列(Array)の作成:array()

>>> import numpy as np
>>> ar = np.array([1, 2, 3, 4, 5])
>>> ar
array([1, 2, 3, 4, 5])

階数(配列の次元数)を返す:ndim属性

>>> import numpy as np
>>> ar = np.array([1, 2, 3, 4, 5])
>>> ar.ndim
1

配列(Array)の値の総数:size

>>> import numpy as np
>>> ar = np.array([1, 2, 3, 4, 5])
>>> ar.size
5

各階の値の数:shape

>>> import numpy as np
>>> ar = np.array([1, 2, 3, 4, 5])
>>> ar.shape
(5,)

配列(Array)の作成:arange()

>>> import numpy as np
>>> ar = np.arange(5)
>>> ar
array([0, 1, 2, 3, 4])
>>> ar.ndim
1
>>> ar.shape
(5,)
>>> ar.size
5
>>> ar = np.arange(3, 5)
>>> ar
array([3, 4])
>>> ar = np.arange(2, 9)
>>> ar
array([2, 3, 4, 5, 6, 7, 8])


# floatでも同様にできる
>>> fl_ar = np.arange(2.0, 13.0, 3.5)
>>> fl_ar
array([ 2. ,  5.5,  9. , 12.5])

# 生成する値の方をdtypeで指定
>>> ar = np.arange(20, 10, -0.4, dtype=np.float)
>>> ar
array([20. , 19.6, 19.2, 18.8, 18.4, 18. , 17.6, 17.2, 16.8, 16.4, 16. ,
       15.6, 15.2, 14.8, 14.4, 14. , 13.6, 13.2, 12.8, 12.4, 12. , 11.6,
       11.2, 10.8, 10.4])

配列(Array)の作成:zeros(),ones(),random(),empty()

すべての値がゼロの配列をつくるzeros()

引数でタプルを渡す。どんな配列をつくるか指定。

>>> ar = np.zeros((5,))                                                                   
>>> ar                                                                                    
array([0., 0., 0., 0., 0.])                                                               
>>> a.ndim
1
>>> ar.ndim
1
>>> ar.shape
(5,)
>>> ar.size
5

# 階数が2
>>> ar2 = np.zeros((3, 5))
>>> ar2
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
>>> ar2.ndim
2
>>> ar2.shape
(3, 5)
>>> ar2.size  # 総数は15個
15

すべての値がイチの配列をつくるones()

zeros()ones()は同じ値で初期化する。

>>> one_ar = np.ones((4, 7))
>>> one_ar
array([[1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1.]])

0.0から1.0までのランダムな値で配列を作成するrandom()

>>> random_ar = np.random.random((6, 8))
>>> random_ar
array([[0.76626547, 0.60511654, 0.49998198, 0.38248588, 0.83673815,
        0.21706767, 0.35267305, 0.67673959],
       [0.88931685, 0.32110564, 0.48266296, 0.96074688, 0.62669309,
        0.05325567, 0.77889556, 0.80401617],
       [0.98079976, 0.80230347, 0.12454216, 0.39592782, 0.52094194,
        0.57635068, 0.02476026, 0.5490413 ],
       [0.14907925, 0.80100428, 0.85416157, 0.05275876, 0.45251503,
        0.95061515, 0.23089537, 0.53930775],
       [0.12175142, 0.85724276, 0.6455671 , 0.79708417, 0.25670577,
        0.59250395, 0.30657821, 0.75747281],
       [0.01655131, 0.54550158, 0.0777452 , 0.04324642, 0.84043967,
        0.95208402, 0.48270439, 0.66220501]])

初期化不要で特定の値で配列を作成するempty()

>>> ar = np.empty((2, 3, 2))
>>> ar
array([[[0.00000000e+000, 0.00000000e+000],
        [5.43472210e-323, 6.93784287e-310],
        [2.12199781e-314, 2.18138751e-314]],

       [[0.00000000e+000, 2.12199579e-314],
        [0.00000000e+000, 1.75871011e-310],
        [3.50977866e+064, 0.00000000e+000]]])

配列(Array)の形状を変える:reshape()

>>> ar = np.arange(8)
>>> ar
array([0, 1, 2, 3, 4, 5, 6, 7])
>>> ar = ar.reshape(2, 4)
>>> ar
array([[0, 1, 2, 3],
       [4, 5, 6, 7]])
>>> ar.ndim
2
>>> ar.shape
(2, 4)
>>> ar.size
8

>>> ar.reshape(4, 2)
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7]])
>>> ar.ndim
2
>>> ar.shape
(2, 4)
>>> ar.size
8

# shapeでも同じ結果
>>> ar.shape = (2, 4)
>>> ar
array([[0, 1, 2, 3],
       [4, 5, 6, 7]])

# 指定する形状は配列の中身の要素と合致していなければならない
>>> ar = ar.reshape(5, 8)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: cannot reshape array of size 8 into shape (5,8)

配列(Array)の要素を取得

>>> ar = np.arange(5)
>>> ar[3]
3
>>> ar[-1]
4

>>> ar = np.arange(12)
>>> ar.shape = (3, 4)
>>> ar
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> ar[2, 3]
11
>>> ar[1, 2]
6
>>> ar[0, 0]
0

スライスで切り出し

>>> ar = np.arange(12)
>>> ar = ar.reshape(2, 6)
>>> ar
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11]])

# 0番目の配列で3番目からスライス
>>> ar[0, 3:]
array([3, 4, 5])

# -1番目の配列の3番目までをスライス
>>> ar[-1, :3]
array([6, 7, 8])

# 各配列の3番目と4番目を500に入れ替える
>>> ar[:, 3:5] = 500
>>> ar
array([[  0,   1,   2, 500, 500,   5],
       [  6,   7,   8, 500, 500,  11]])

配列(Array)のコピーcopy()と参照

>>> arr3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
>>> arr3d
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]]])

>>> arr3d[0]
array([[1, 2, 3],
       [4, 5, 6]])

>>> old_values = arr3d[0].copy()
>>> arr3d[0] = 60
>>> arr3d
array([[[60, 60, 60],
        [60, 60, 60]],

       [[ 7,  8,  9],
        [10, 11, 12]]])

>>> arr3d[0] = old_values
>>> arr3d
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]]])

配列(Array)の数学演算

配列(Array)の四則演算

>>> ar = np.array([[1., 2., 3.], [4., 5., 6.]])
>>> ar
array([[1., 2., 3.],
       [4., 5., 6.]])

>>> ar * ar
array([[ 1.,  4.,  9.],
       [16., 25., 36.]])

>>> ar - ar
array([[0., 0., 0.],
       [0., 0., 0.]])

>>> ar += 5
>>> ar
array([[ 6.,  7.,  8.],
       [ 9., 10., 11.]])

>>> 1 / ar
array([[0.16666667, 0.14285714, 0.125     ],
       [0.11111111, 0.1       , 0.09090909]])

>>> ar * 10
array([[ 60.,  70.,  80.],
       [ 90., 100., 110.]])

>>> a = np.zeros((3, 5)) + 11
>>> a
array([[11., 11., 11., 11., 11.],
       [11., 11., 11., 11., 11.],
       [11., 11., 11., 11., 11.]])

平均を返すmean(),足した合計を出すsum()

>>> x = np.array([1, 2, 3, 4, 5])
>>> x.mean()
3.0
>>> x.sum()
15

最大を返すmaximum(),最小を返すminimum()

>>> x = np.array([1, 2, 3, 4, 5])
>>> y = np.array([0, 4, 1, 5, 4])
>>> np.maximum(x, y)
array([1, 4, 3, 5, 5])
>>> np.minimum(x, y)
array([0, 2, 1, 4, 4])

ソートするsort(),重複のないユニークな要素を返すunique()

>>> x = np.array([2, 1, 0, 4, 5])
>>> x.sort()
>>> x
array([0, 1, 2, 4, 5])


>>> names = np.array(['Python', 'Numpy', 'Numpy', 'Python', 'Python3'])
>>> np.unique(names)
array(['Numpy', 'Python', 'Python3'], dtype='<U7')

dot()で配列のドット積を計算

>>> x = np.array([2, 1, 0, 4, 5])
>>> y = array([0, 4, 1, 5, 4])
>>> dot(x, y)
44  # 2*0 + 1*4 + 0*1 + 4*5 + 5*4 = 44

平方根のsqrt(),ネイピア数の累乗のexp()

>>> ar = np.arange(10)
>>> np.sqrt(ar)
array([0.        , 1.        , 1.41421356, 1.73205081, 2.        ,
       2.23606798, 2.44948974, 2.64575131, 2.82842712, 3.        ])

>>> np.exp(ar)
array([1.00000000e+00, 2.71828183e+00, 7.38905610e+00, 2.00855369e+01,
       5.45981500e+01, 1.48413159e+02, 4.03428793e+02, 1.09663316e+03,
       2.98095799e+03, 8.10308393e+03])