Numpy ndarray を使おう

## 1.　Numpy ndarray を使う

```python
import numpy as np
```

### 1.1.　初期化しないベクトル（内容は不定）

```python
np.empty(10)
```

    array([0.54296942, 0.98085815, 0.52797689, 0.09862195, 0.60034229,
           0.86971354, 0.82463765, 0.13218043, 0.36232667, 0.3283693 ])

### 1.2.　0.0 を n 個含むベクトルを定義する

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

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

### 1.3.　0.1 を n 個含むベクトルを定義する

```python
a = np.repeat(0.1, 10)
a
```

    array([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1])

### 1.4.　要素を同じ回数繰り返すベクトルを定義する

```python
np.repeat(np.array([0.2, 0.3, 0.5, 0.7]), 5)
```

    array([0.2, 0.2, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3, 0.3, 0.3, 0.5, 0.5, 0.5,
           0.5, 0.5, 0.7, 0.7, 0.7, 0.7, 0.7])

```python
np.tile(np.array([0.2, 0.3, 0.5, 0.7]), 5)
```

    array([0.2, 0.3, 0.5, 0.7, 0.2, 0.3, 0.5, 0.7, 0.2, 0.3, 0.5, 0.7, 0.2,
           0.3, 0.5, 0.7, 0.2, 0.3, 0.5, 0.7])

### 1.5.　要素を指定回数繰り返すベクトルを定義する

```python
a = np.repeat([0.2, 0.3, 0.5, 0.7], [2, 3, 5, 7])
a
```

    array([0.2, 0.2, 0.3, 0.3, 0.3, 0.5, 0.5, 0.5, 0.5, 0.5, 0.7, 0.7, 0.7,
           0.7, 0.7, 0.7, 0.7])

### 1.6.　等差数列

#### 1.6.1.　公差 1

```python
np.array(range(10))
```

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

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

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

#### 1.6.2.　初項指定，末項指定，公差指定

```python
np.arange(10, 15, 0.5)
```

    array([10. , 10.5, 11. , 11.5, 12. , 12.5, 13. , 13.5, 14. , 14.5])

#### 1.6.3.　初項指定，末項指定，長さ指定

```python
np.linspace(0, 10, 6)
```

    array([ 0.,  2.,  4.,  6.,  8., 10.])

```python
np.linspace(0, 10, 7)
```

    array([ 0.        ,  1.66666667,  3.33333333,  5.        ,  6.66666667,
            8.33333333, 10.        ])

### 1.7.　等比数列

```python
np.logspace(0, 4, num = 5, dtype=int)
```

    array([    1,    10,   100,  1000, 10000])

```python
np.logspace(0, 5, num = 6, base=3)
```

    array([  1.,   3.,   9.,  27.,  81., 243.])

### 1.8.　2次元配列

#### 1.8.1.　二重リストで定義

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

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

#### 1.8.2.　ベクトルを返す関数で次元ごとのサイズを指定する

```python
n = 5
# リストで書くと [[False]*n for i in range(n)] のようなものを
[[False]*n for i in range(n)]
```

    [[False, False, False, False, False],
     [False, False, False, False, False],
     [False, False, False, False, False],
     [False, False, False, False, False],
     [False, False, False, False, False]]

```python
np.empty((3, 5))
```

    array([[ 0.    ,  0.    ,  1.    ,  0.0625,  0.25  ],
           [ 1.    ,  1.    ,  1.    ,  1.    ,  5.0625],
           [ 2.25  ,  1.    , 16.    ,  4.    ,  1.    ]])

```python
np.full((3, 2), 5)
```

    array([[5, 5],
           [5, 5],
           [5, 5]])

```python
np.zeros((5, 5), dtype=bool)
```

    array([[False, False, False, False, False],
           [False, False, False, False, False],
           [False, False, False, False, False],
           [False, False, False, False, False],
           [False, False, False, False, False]])

```python
np.ones((5, 5), dtype=bool)
```

    array([[ True,  True,  True,  True,  True],
           [ True,  True,  True,  True,  True],
           [ True,  True,  True,  True,  True],
           [ True,  True,  True,  True,  True],
           [ True,  True,  True,  True,  True]])

### 1.9.　行・列を指定してソートする

#### 1.9.1.　sort メソッドはインプレース

```python
e = np.array([[4, 3, 5], [1, 2, 1], [2, 3, 2]])
e.sort(axis=1)
e
```

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

```python
e.sort(axis=0)
e
```

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

np.sort はインプレースではない

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

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

```python
e
```

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

### 1.10.　単位行列

```python
np.identity(3)
```

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

```python
np.eye(3)
```

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

### 1.11.　対角行列

```python
np.eye(3, 2)
```

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

```python
np.eye(3, 5)
```

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

```python
np.diag(np.array([1, 2, 3, 4]))
```

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

#### 1.11.1.　対角成分に代入

```python
r = np.reshape(np.arange(1, 10), (3, 3))
diag2 = np.eye(3) == 1
r[diag2] = 999
print(r)
```

    [[999   2   3]
     [  4 999   6]
     [  7   8 999]]

### 1.12.　三角行列

#### 1.12.1.　要素が 1 の下三角行列

```python
np.tri(3)
```

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

```python
np.tri(3, 5, 2, dtype=int)
```

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

#### 1.12.2.　対角からk要素下

```python
np.tri(3, 5, -1)
```

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

#### 1.12.3.　代入

```python
r = np.reshape(np.arange(1, 10), (3, 3))
p = np.reshape(np.arange(1, 10)*100, (3, 3))
lower = np.tri(3) == 1
r[lower] = p[lower]
print(r)
```

    [[100   2   3]
     [400 500   6]
     [700 800 900]]

#### 1.12.4.　下三角行列の抽出

```python
a = np.reshape(np.arange(1, 10), (3,3))
print(a)
```

    [[1 2 3]
     [4 5 6]
     [7 8 9]]

```python
print(np.tril(a))
```

    [[1 0 0]
     [4 5 0]
     [7 8 9]]

```python
print(np.tril(a, -1))
```

    [[0 0 0]
     [4 0 0]
     [7 8 0]]

#### 1.12.5.　上三角行列の抽出

```python
a = np.reshape(np.arange(1, 10), (3,3))
print(a)
```

    [[1 2 3]
     [4 5 6]
     [7 8 9]]

```python
print(np.triu(a))
```

    [[1 2 3]
     [0 5 6]
     [0 0 9]]

```python
print(np.triu(a, 1))
```

    [[0 2 3]
     [0 0 6]
     [0 0 0]]

#### 1.12.6.　三角行列の要素の抽出

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

    [[1 1 1]
     [2 2 2]
     [3 3 3]]

```python
a = np.triu(np.ones_like(y), 1) > 0
print(a)
```

    [[False  True  True]
     [False False  True]
     [False False False]]

```python
print(y[a])
```

    [1 1 2]

### 1.13.　対角要素

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

    [[1 2 3]
     [4 5 6]
     [7 8 9]]

```python
np.diag(a)
```

    array([1, 5, 9])

### 1.14.　メッシュグリッド

```python
a, b = np.meshgrid([1,2,3], [4,5,6,7])
print(a)
```

    [[1 2 3]
     [1 2 3]
     [1 2 3]
     [1 2 3]]

```python
print(b)
```

    [[4 4 4]
     [5 5 5]
     [6 6 6]
     [7 7 7]]

### 1.15.　全ての行要素，列要素の変更

```python
a = np.arange(12)
b = a.reshape((3, 4))
print(b)
```

    [[ 0  1  2  3]
     [ 4  5  6  7]
     [ 8  9 10 11]]

```python
b[1] = 100
print(b)
```

    [[  0   1   2   3]
     [100 100 100 100]
     [  8   9  10  11]]

```python
b[:, 2] = 200
print(b)
```

    [[  0   1 200   3]
     [100 100 200 100]
     [  8   9 200  11]]

### 1.16.　配列の連結

```python
a = np.array([1.0, 2.0, 3.0])
b = np.array([5.0, 6.0, 7.0])
np.vstack((a, b))
```

    array([[1., 2., 3.],
           [5., 6., 7.]])

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

    array([1., 2., 3., 5., 6., 7.])

```python
x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([[10, 11, 12], [20, 21, 22]])
```

```python
np.vstack((x, y))
```

    array([[ 1,  2,  3],
           [ 4,  5,  6],
           [10, 11, 12],
           [20, 21, 22]])

```python
np.hstack((x, y))
```

    array([[ 1,  2,  3, 10, 11, 12],
           [ 4,  5,  6, 20, 21, 22]])

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

    array([1., 2., 3., 5., 6., 7.])

```python
np.concatenate(([a], [b]))
```

    array([[1., 2., 3.],
           [5., 6., 7.]])

```python
np.concatenate((x, y), axis=0)
```

    array([[ 1,  2,  3],
           [ 4,  5,  6],
           [10, 11, 12],
           [20, 21, 22]])

```python
np.concatenate((x, y), axis=1)
```

    array([[ 1,  2,  3, 10, 11, 12],
           [ 4,  5,  6, 20, 21, 22]])

```python
np.c_[a, b]
```

    array([[1., 5.],
           [2., 6.],
           [3., 7.]])

```python
np.r_[a, b]
```

    array([1., 2., 3., 5., 6., 7.])

```python
np.c_[x, y]
```

    array([[ 1,  2,  3, 10, 11, 12],
           [ 4,  5,  6, 20, 21, 22]])

```python
np.r_[x, y]
```

    array([[ 1,  2,  3],
           [ 4,  5,  6],
           [10, 11, 12],
           [20, 21, 22]])

### 1.17.　配列の分割

```python
a = np.arange(12).reshape(3, 4)
np.split(a, 2, axis=1)
```

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

```python
x, y = np.split(a, 2, axis=1)
x
```

    array([[0, 1],
           [4, 5],
           [8, 9]])

```python
y
```

    array([[ 2,  3],
           [ 6,  7],
           [10, 11]])

```python
x, y, z = np.split(a, [1, 3], axis=1)
x
```

    array([[0],
           [4],
           [8]])

```python
y
```

    array([[ 1,  2],
           [ 5,  6],
           [ 9, 10]])

```python
z
```

    array([[ 3],
           [ 7],
           [11]])

```python
x, y = np.split(a, [1], axis=0)
x
```

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

```python
y
```

    array([[ 4,  5,  6,  7],
           [ 8,  9, 10, 11]])

### 1.18.　逆転

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

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

### 1.19.　任意の要素から無作為抽出

```python
np.random.choice(['a', 'b', 'c'], 10)
```

    array(['c', 'b', 'b', 'a', 'c', 'a', 'c', 'b', 'b', 'b'], dtype='<U1')

### 1.20.　配列をシャッフルする

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

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

```python
np.random.shuffle(a)
a
```

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

### 1.21.　整数乱数配列

```python
np.random.randint(0, 5, 10)  # [0, 5)
```

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

```python
np.random.randint(0,6, (4, 6))
```

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

### 1.22.　一様乱数配列

```python
np.random.rand(5)
```

    array([0.77652573, 0.66557185, 0.24269832, 0.58326534, 0.45768616])

```python
np.random.rand(4, 5)
```

    array([[0.33821307, 0.7982781 , 0.75849507, 0.47283488, 0.77677102],
           [0.33283041, 0.3547948 , 0.5136131 , 0.41986897, 0.25914676],
           [0.80811828, 0.06705775, 0.67246877, 0.10392812, 0.84117669],
           [0.00656908, 0.92959769, 0.17285006, 0.7929529 , 0.39475123]])

### 1.23.　正規乱数配列

```python
np.random.normal(0, 1, 5)  # μ，σ，n
```

    array([-1.64930951, -1.54775496, -0.14741955,  2.19361774, -0.25132001])

```python
np.random.normal(50, 10, (4, 5))
```

    array([[41.37826507, 52.11736377, 67.4529769 , 47.30894323, 44.86792191],
           [62.78798789, 36.03928626, 60.61663526, 48.52174999, 35.82257121],
           [48.1838473 , 39.77767396, 43.03464413, 33.12420759, 64.05057324],
           [56.75662169, 30.12300927, 47.425552  , 48.92858433, 44.39202913]])

### 1.24.　標準正規乱数配列

```python
np.random.randn(5)  # n,  (μ=0，σ=1)
```

    array([-1.64930951, -1.54775496, -0.14741955,  2.19361774, -0.25132001])

```python
np.random.randn(4, 5)
```

    array([[ 0.56668756,  0.65459934, -0.83598793, -0.80786311, -1.42285691],
           [-0.79637241, -1.49401664,  1.14888839, -1.38985433, -0.21149632],
           [ 0.05892271,  0.06737828,  1.46461575, -0.21965432,  0.47423523],
           [ 0.58777079,  1.17415001,  0.86777467, -0.43134558, -0.50830685]])

### 1.25.　横ベクトル

1×n 行列である。

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

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

```python
np.zeros((1, 5))
```

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

### 1.26.　縦ベクトル

n×1 行列である。

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

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

```python
np.zeros((5, 1))
```

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

```python
np.empty(9)
```

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

### 1.27.　既存の行列と同じサイズの行列を作る

```python
a = np.ones((3, 5))
np.zeros_like(a)
```

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

```python
np.empty_like(a)
```

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

### 1.28.　次元数の変更

```python
a = np.arange(15)
b = np.reshape(a, (3, 5))
print(a)
print(b)
```

    [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
    [[ 0  1  2  3  4]
     [ 5  6  7  8  9]
     [10 11 12 13 14]]

resize はインプレース

```python
a = np.arange(15)
a.resize((3, 5))
print(a)
```

    [[ 0  1  2  3  4]
     [ 5  6  7  8  9]
     [10 11 12 13 14]]

```python
a.resize((15,))
print(a)
```

    [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]

### 1.29.　行列をベクトルに変換，スカラーも配列に変換

```python
x = np.array([[1,2,3,4],[6,7,8,9]])
np.ravel(x)
```

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

```python
np.ravel(123)
```

    array([123])

### 1.30.　n 番目に大きい要素がどこにあるか

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

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

```python
(-x).argsort()
```

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

```python
#### 1.30.1.　argsort を使ってソートする
```

```python
order = x.argsort()
x[order]
```

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

```python
rev = (-x).argsort()
x[rev]
```

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

## 2.　配列の演算

### 2.1.　ベクトルと定数

```python
a = np.array(range(10))
a
```

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

```python
a + 2
```

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

```python
a - 4
```

    array([-4, -3, -2, -1,  0,  1,  2,  3,  4,  5])

```python
a*3
```

    array([ 0,  3,  6,  9, 12, 15, 18, 21, 24, 27])

```python
a/5
```

    array([0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8])

```python
a**0.5
```

    array([0.        , 1.        , 1.41421356, 1.73205081, 2.        ,
           2.23606798, 2.44948974, 2.64575131, 2.82842712, 3.        ])

### 2.2.　配列と定数

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

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

```python
a + 1
```

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

```python
a - 2
```

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

```python
a*3
```

    array([[ 3,  6,  9],
           [12, 15, 18]])

```python
a/4
```

    array([[0.25, 0.5 , 0.75],
           [1.  , 1.25, 1.5 ]])

```python
a**0.5
```

    array([[1.        , 1.41421356, 1.73205081],
           [2.        , 2.23606798, 2.44948974]])

### 2.3.　ベクトルとベクトル

```python
a = np.array(range(5))
b = [2, 4, 1, 8, 3]
print(a)
print(b)
```

    [0 1 2 3 4]
    [2, 4, 1, 8, 3]

```python
a + b
```

    array([ 2,  5,  3, 11,  7])

```python
a - b
```

    array([-2, -3,  1, -5,  1])

```python
a*b
```

    array([ 0,  4,  2, 24, 12])

```python
a/b
```

    array([0.        , 0.25      , 2.        , 0.375     , 1.33333333])

```python
a**b
```

    array([   0,    1,    2, 6561,   64])

### 2.4.　内積

```python
a = np.array(range(5))
b = [2, 4, 1, 8, 3]
np.dot(a, b)
```

    42

```python
sum(a*b)
```

    42

### 2.5.　外積

#### 2.5.1.　デフォルトでは演算子は "*"

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

    array([[ 0,  0,  0,  0,  0],
           [ 2,  4,  1,  8,  3],
           [ 4,  8,  2, 16,  6],
           [ 6, 12,  3, 24,  9],
           [ 8, 16,  4, 32, 12]])

```python
np.outer([2, 3], [3, 5, 7])
```

    array([[ 6, 10, 14],
           [ 9, 15, 21]])

#### 2.5.2.　掛け算以外

```python
np.add.outer(a, b)
```

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

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

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

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

    array([[ 0,  0,  0,  0,  0],
           [ 2,  4,  1,  8,  3],
           [ 4,  8,  2, 16,  6],
           [ 6, 12,  3, 24,  9],
           [ 8, 16,  4, 32, 12]])

```python
np.divide.outer(a, b)
```

    array([[0.        , 0.        , 0.        , 0.        , 0.        ],
           [0.5       , 0.25      , 1.        , 0.125     , 0.33333333],
           [1.        , 0.5       , 2.        , 0.25      , 0.66666667],
           [1.5       , 0.75      , 3.        , 0.375     , 1.        ],
           [2.        , 1.        , 4.        , 0.5       , 1.33333333]])

```python
np.maximum.outer(a, b)
```

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

```python
np.minimum.outer(a, b)
```

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

### 2.6.　ベクトルと行列

```python
a = np.array([1,2,3])
b = np.array([[11, 12, 13], [14, 15, 16]])
```

```python
a + b
```

    array([[12, 14, 16],
           [15, 17, 19]])

```python
a - b
```

    array([[-10, -10, -10],
           [-13, -13, -13]])

```python
a*b
```

    array([[11, 24, 39],
           [14, 30, 48]])

```python
a/b
```

    array([[0.09090909, 0.16666667, 0.23076923],
           [0.07142857, 0.13333333, 0.1875    ]])

```python
a**b
```

    array([[       1,     4096,  1594323],
           [       1,    32768, 43046721]])

### 2.7.　行列と行列

```python
a = np.array([[1,2,3], [4, 5, 6]])
b = np.array([[11, 12, 13], [14, 15, 16]])
```

```python
a + b
```

    array([[12, 14, 16],
           [18, 20, 22]])

```python
a - b
```

    array([[-10, -10, -10],
           [-10, -10, -10]])

```python
a*b
```

    array([[11, 24, 39],
           [56, 75, 96]])

```python
a/b
```

    array([[0.09090909, 0.16666667, 0.23076923],
           [0.28571429, 0.33333333, 0.375     ]])

```python
a**b
```

    array([[            1,          4096,       1594323],
           [    268435456,   30517578125, 2821109907456]])

### 2.8.　行列積

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

```python
a.dot(b)
```

    array([[ 16,  13,  26],
           [ 40,  31,  65],
           [ 64,  49, 104]])

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

    array([[ 16,  13,  26],
           [ 40,  31,  65],
           [ 64,  49, 104]])

```python
a @ b
```

    array([[ 16,  13,  26],
           [ 40,  31,  65],
           [ 64,  49, 104]])

### 2.9.　転置

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

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

```python
a.T
```

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

### 2.10.　逆行列

```python
from scipy import linalg
b = np.array([[3, 1, 4], [2, 3, 5], [3, 2, 4]])
linalg.inv(b)
```

    array([[-0.28571429, -0.57142857,  1.        ],
           [-1.        ,  0.        ,  1.        ],
           [ 0.71428571,  0.42857143, -1.        ]])

```python
(linalg.inv(b)).dot(b)  # 逆行列ともとの行列の行列積は単位行列になる
```

    array([[ 1.00000000e+00, -2.22044605e-16, -4.44089210e-16],
           [ 0.00000000e+00,  1.00000000e+00,  0.00000000e+00],
           [ 0.00000000e+00,  0.00000000e+00,  1.00000000e+00]])

### 2.11.　連立方程式

```python
b = np.array([[3, 1, 4], [2, 3, 5], [3, 2, 4]])
c = np.array([3,2,4])
linalg.solve(b, c)
```

    array([ 2.,  1., -1.])

### 2.12.　行列式

```python
linalg.det(b)
```

    -7.0

### 2.13.　ノルム

```python
linalg.norm(b)
```

    9.643650760992955

### 2.14.　固有値，固有ベクトル

```python
val, vec = linalg.eig(b)
val
```

    array([ 8.90045165+0.j,  1.59319575+0.j, -0.4936474 +0.j])

```python
vec
```

    array([[-0.49779335, -0.54449017, -0.54183429],
           [-0.65269915,  0.83857094, -0.5716379 ],
           [-0.57112661, -0.01814497,  0.61615397]])

### 2.15.　特異値分解

```python
U, s, V = linalg.svd(b)
U
```

    array([[-0.52488074,  0.64926919, -0.55040869],
           [-0.63715332, -0.72847437, -0.25171561],
           [-0.56438981,  0.21857405,  0.79604618]])

```python
s
```

    array([9.49923625, 1.59740488, 0.46131157])

```python
V
```

    array([[-0.47815616, -0.375306  , -0.79404792],
           [ 0.71777732, -0.68799452, -0.10704792],
           [ 0.5061249 ,  0.62113521, -0.59835495]])