NumPy 入門


# NumPy 入門

[Quickstart tutorial](https://docs.scipy.org/doc/numpy-dev/user/quickstart.html)

NumPy のメインオブジェクトは homogeneous な配列である. homogeneous な配列とは, ひとつの配列のなかに複数のデータタイプを持たないという意味で均質な配列である, ということである.

NumPy の array は ndarray とよばれる.

## ndarray の基本


```python
# import
import numpy as np
```


```python
arr = [[1, 2], [3, 4]]
# ndarray を作る
a = np.array(arr)
a
```




    array([[1, 2],
           [3, 4]])




```python
# 次元数
a.ndim
```




    2




```python
# 行と列を tuple で返す
a.shape
```




    (2, 2)




```python
# 要素数
a.size
```




    4




```python
# array のデータ型
a.dtype
```




    dtype('int64')




```python
a.data
```




    <read-write buffer for 0x104a04a80, size 32, offset 0 at 0x104a10b30>



### numpy.array

```
numpy.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)
```

ndarray を作るとき, dtype プロパティにデータ型を指定することができる.
データ型は以下を参照.

[Data types](https://docs.scipy.org/doc/numpy-1.13.0/user/basics.types.html)


```python
a = np.array([1, 3], dtype=float)
a.dtype
```




    dtype('float64')



### numpy.zeros, numpy.ones

```
numpy.zeros(shape, dtype=float, order='C')
```

shape を引数とし, すべての要素を 0 (ones の場合は 1) とするような関数が存在する.


```python
np.zeros([2,2], dtype=int)
```




    array([[0, 0],
           [0, 0]])




```python
np.ones([2,2], dtype=int)

```




    array([[1, 1],
           [1, 1]])




```python
np.empty([2,2])
```




    array([[  0.00000000e+000,   0.00000000e+000],
           [  1.48219694e-323,   0.00000000e+000]])



### numpy.identity

```
numpy.identity(n, dtype=None)
```

単位行列を作る


```python
np.identity(4)
```




    array([[ 1.,  0.,  0.,  0.],
           [ 0.,  1.,  0.,  0.],
           [ 0.,  0.,  1.,  0.],
           [ 0.,  0.,  0.,  1.]])



### numpy.arange

```
numpy.arange([start, ]stop, [step, ]dtype=None)
```

連番や等差数列を作る


```python
np.arange(10)
```




    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])




```python
np.arange(1, 11)
```




    array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])




```python
np.arange(5, 100, 5)
```




    array([ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85,
           90, 95])



### numpy.linspace

```
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
```

等間隔な数列を作る


```python
np.linspace(0, 1)
```




    array([ 0.        ,  0.02040816,  0.04081633,  0.06122449,  0.08163265,
            0.10204082,  0.12244898,  0.14285714,  0.16326531,  0.18367347,
            0.20408163,  0.2244898 ,  0.24489796,  0.26530612,  0.28571429,
            0.30612245,  0.32653061,  0.34693878,  0.36734694,  0.3877551 ,
            0.40816327,  0.42857143,  0.44897959,  0.46938776,  0.48979592,
            0.51020408,  0.53061224,  0.55102041,  0.57142857,  0.59183673,
            0.6122449 ,  0.63265306,  0.65306122,  0.67346939,  0.69387755,
            0.71428571,  0.73469388,  0.75510204,  0.7755102 ,  0.79591837,
            0.81632653,  0.83673469,  0.85714286,  0.87755102,  0.89795918,
            0.91836735,  0.93877551,  0.95918367,  0.97959184,  1.        ])




```python
np.linspace(1, 50)
```




    array([  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.,  11.,
            12.,  13.,  14.,  15.,  16.,  17.,  18.,  19.,  20.,  21.,  22.,
            23.,  24.,  25.,  26.,  27.,  28.,  29.,  30.,  31.,  32.,  33.,
            34.,  35.,  36.,  37.,  38.,  39.,  40.,  41.,  42.,  43.,  44.,
            45.,  46.,  47.,  48.,  49.,  50.])




```python
np.linspace(10, 50, 5)
```




    array([ 10.,  20.,  30.,  40.,  50.])



### numpy.reshape

```
numpy.reshape(a, newshape, order='C')
```

array (ndarray) の shape を変換する.


```python
a = np.array([[1, 2], [3, 4], [5, 6]])
print(a.shape)
a
```

    (3, 2)





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




```python
a.reshape(-1,)
```




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




```python
a.reshape([2, 3])
```




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



### ndarray の参照


```python
a = np.array([[1, 2, 3], [4, 5, 6]])

print(a[0])
print(a[0][0])
# 行, 列の直接指定
print(a[0, 0])
# スライス
print(a[1:2])
print(a[1:])
print(a[:1])
```

    [1 2 3]
    1
    1
    [[4 5 6]]
    [[4 5 6]]
    [[1 2 3]]



```python
# True, False による配列の指定 (True である箇所が表示される)
b = a > 3
print(b)
print(a[b])
```

    [[False False False]
     [ True  True  True]]
    [4 5 6]


### numpy.sort

```
numpy.sort(a, axis=-1, kind='quicksort', order=None)
```

配列がソートされる


```python
a = np.random.random(10)
a
```




    array([ 0.30059093,  0.39776416,  0.03569461,  0.32814799,  0.00305144,
            0.33298982,  0.37045668,  0.35102445,  0.7594396 ,  0.35020994])




```python
# 昇順
np.sort(a)
```




    array([ 0.00305144,  0.03569461,  0.30059093,  0.32814799,  0.33298982,
            0.35020994,  0.35102445,  0.37045668,  0.39776416,  0.7594396 ])




```python
# 降順
np.sort(a)[::-1]
```




    array([ 0.7594396 ,  0.39776416,  0.37045668,  0.35102445,  0.35020994,
            0.33298982,  0.32814799,  0.30059093,  0.03569461,  0.00305144])



## ndarray の演算

Universal Function とよばれる.

### numpy.add

```
numpy.add(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'add'>
```

行列の足し算


```python
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
np.add(a, b)
```




    array([5, 7, 9])




```python
a + b
```




    array([5, 7, 9])



### numpy.subtract

```
numpy.subtract(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
```

行列の引き算


```python
np.subtract(b, a)
```




    array([3, 3, 3])




```python
b - a
```




    array([3, 3, 3])



### numpy.multiply

```
numpy.multiply(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
```

行列の掛け算


```python
np.multiply(a, b)
```




    array([ 4, 10, 18])




```python
a * b
```




    array([ 4, 10, 18])



### numpy.divide

```
numpy.divide(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
```

行列の割り算


```python
a = np.array([1, 2, 3])
b = np.array([0.1, 0.2, 0.3])
np.divide(a, b)
```




    array([ 10.,  10.,  10.])




```python
a / b
```




    array([ 10.,  10.,  10.])



### その他の Universal Function (ufunc)

[Universal functions](https://docs.scipy.org/doc/numpy-1.13.0/reference/ufuncs.html#available-ufuncs)

## ndarray の要約関数


```python
a = np.array([1, 2, 3])
```


```python
# 合計
a.sum()
```




    6




```python
# 平均
a.mean()
```




    2.0




```python
# 最大値
a.max()
```




    3




```python
# 最小値
a.min()
```




    1




```python
# 分散
a.var()
```




    0.66666666666666663