Python基礎8 numpy テスト

#1.はじめに
　numpyの基礎をテスト形式で備忘録として残しておく。事前に、**numpy** はインポートされているものとする。

#Question_01
Question

```python
AからBを生成する関数function(A)を記述せよ 

A:
array([[5, 3, 4, 3],
       [7, 6, 7, 4],
       [6, 7, 5, 4]])
B:
array([[False, False, False, False],
       [False, False, False, False],
       [False, False, False, False]])
```
Answer

```python
def function(A):
    return A > 7
```
#Question_02
Question

```python
AからBを生成する関数function(A)を記述せよ

A:
array([4, 5, 4, 5, 4, 3, 2])

B:
array([2, 3, 4, 4, 4, 5, 5])
```
Answer

```python
def function(A):
    return np.sort(A)
```
#Question_03
Question

```python
AからBを生成する関数function(A)を記述せよ 

A:
array([[ 1,  1, 19],
       [ 7,  3,  6],
       [16,  1, 11]])
B:
array([ 1,  7, 16,  1,  3,  1, 19,  6, 11])
```
Answer

```python
def function(A):
    return A.T.flatten()
```
#Question_04
Question

```python
AからBを生成する関数function(A)を記述せよ

A:
array([1, 5, 4, 2, 8, 2, 9])

B:
array([1, 2, 2, 4, 5, 8, 9])
```
Answer

```python
def function(A):
    return np.sort(A)
```
#Question_05
Question

```python
A,BからCを生成する関数function(A, B)を記述せよ

A:
array([[1, 3, 1],
       [3, 3, 2],
       [2, 2, 3]])
B:
array([[2, 3, 1],
       [2, 3, 1],
       [3, 3, 2]])
C:
array([[11, 15,  6],
       [18, 24, 10],
       [17, 21, 10]])
```
Answer

```python
def function(A, B):
    return np.dot(A, B)
```
#Question_06
Question

```python
A,BからCを生成する関数function(A, B)を記述せよ

A:
array([8, 0, 3, 0, 3, 0, 0, 5, 6, 8, 5, 8, 6, 3, 3])

B:
array([3, 1, 8, 4, 5, 4, 5, 5, 2, 5, 4, 0, 7, 3, 7])

C:
array([[24,  0, 24,  0, 15],
       [ 0,  0, 25, 12, 40],
       [20,  0, 42,  9, 21]])
```
Answer

```python
def function(A, B):
    return np.reshape(A*B, (3, 5))
```
#Question_07
Question

```python
AからBを生成する関数function(A)を記述せよ

A:
array([[2, 1, 2],
       [5, 5, 1],
       [5, 1, 1]])
B:
array([[-1, -2, -1],
       [ 2,  2, -2],
       [ 2, -2, -2]])
```
Answer

```python
def function(A):
    return A-3
```
#Question_08
Question

```python
A,BからCを生成する関数function(A, B)を記述せよ

A:
array([[6, 8, 3, 2, 6, 6],
       [3, 2, 2, 7, 1, 0],
       [6, 2, 5, 8, 2, 1],
       [3, 3, 5, 5, 8, 7],
       [3, 6, 6, 8, 4, 6],
       [8, 7, 6, 4, 2, 1]])
B:
array([[2, 6, 2, 6, 0, 0],
       [7, 4, 5, 4, 2, 5],
       [8, 5, 7, 2, 6, 0],
       [6, 4, 3, 1, 3, 2],
       [3, 8, 1, 4, 0, 0],
       [3, 4, 3, 1, 6, 2]])
C:
array([[6, 8, 3, 2, 6, 6, 2, 6, 2, 6, 0, 0],
       [3, 2, 2, 7, 1, 0, 7, 4, 5, 4, 2, 5],
       [6, 2, 5, 8, 2, 1, 8, 5, 7, 2, 6, 0],
       [3, 3, 5, 5, 8, 7, 6, 4, 3, 1, 3, 2],
       [3, 6, 6, 8, 4, 6, 3, 8, 1, 4, 0, 0],
       [8, 7, 6, 4, 2, 1, 3, 4, 3, 1, 6, 2]])
```
Answer

```python
def function(A, B):
    return np.hstack([A, B])
```
#Question_09
Question

```python
AからBを生成する関数function(A)を記述せよ

A:
array([[2, 5, 7, 5, 8, 3, 7],
       [8, 3, 0, 0, 7, 5, 0],
       [2, 3, 1, 6, 7, 4, 0],
       [8, 3, 8, 7, 8, 2, 4],
       [3, 7, 5, 1, 6, 5, 2],
       [7, 2, 3, 5, 7, 1, 6],
       [3, 7, 5, 8, 1, 1, 2]])
B:
array([[8],
       [8],
       [7],
       [8],
       [7],
       [7],
       [8]])
```
Answer

```python
def function(A):
    return np.reshape(np.max(A, axis=1), (7,1))
```

#Question_10
Question

```python
AからBを生成する関数function(A)を記述せよ

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

B:
array([[64, 16, 49],
       [64,  4, 64],
       [36,  4,  1],
       [ 9, 25, 49],
       [36,  0,  9]])
```
Answer

```python
def function(A):
    return np.reshape(A**2, (5,3))
```

#Question_11
Question

```python
A,BからCを生成する関数function(A, B)を記述せよ

A:
array([[5, 5, 4, 5, 6, 4],
       [7, 2, 4, 3, 8, 8],
       [4, 7, 4, 2, 5, 8],
       [7, 1, 6, 5, 5, 3],
       [4, 6, 5, 8, 6, 6],
       [2, 2, 7, 5, 8, 1]])
B:
array([[7, 6, 6, 6, 1, 8],
       [3, 5, 5, 1, 6, 5],
       [7, 4, 8, 4, 8, 3],
       [6, 4, 5, 6, 8, 4],
       [5, 4, 1, 5, 4, 6],
       [6, 2, 5, 3, 1, 3]])
C:
array([[35, 30, 24, 30,  6, 32],
       [21, 10, 20,  3, 48, 40],
       [28, 28, 32,  8, 40, 24],
       [42,  4, 30, 30, 40, 12],
       [20, 24,  5, 40, 24, 36],
       [12,  4, 35, 15,  8,  3]])
```
Answer

```python
def function(A, B):
    return A*B
```

#Question_12
Question

```python
AからBを生成する関数function(A)を記述せよ

A:
array([[6, 0, 6, 6, 8, 0, 2],
       [2, 5, 5, 8, 8, 4, 3],
       [5, 1, 0, 4, 0, 4, 6],
       [3, 1, 1, 7, 4, 7, 7],
       [3, 4, 5, 8, 0, 8, 0],
       [2, 5, 6, 8, 4, 6, 0],
       [0, 5, 1, 3, 6, 1, 6]])
B:
array([[21, 21, 24, 44, 30, 30, 24]])
```
Answer

```python
def function(A):
    return np.sum(A, axis=0)
```

#Question_13
Question

```python
A,BからCを生成する関数function(A, B)を記述せよ

A:
array([[60, 40, 70, 30, 70, 50, 40],
       [20, 30, 40, 50, 10, 20, 20],
       [80, 20, 40, 10, 80, 50, 60],
       [90, 70, 40, 60, 80, 80, 80],
       [10, 40, 60, 50, 50, 60, 10],
       [60, 20, 40, 40, 10, 90, 90]])
B:
array([[7, 7, 1, 2, 3, 5, 8]])

C:
array([[67, 47, 71, 32, 73, 55, 48],
       [27, 37, 41, 52, 13, 25, 28],
       [87, 27, 41, 12, 83, 55, 68],
       [97, 77, 41, 62, 83, 85, 88],
       [17, 47, 61, 52, 53, 65, 18],
       [67, 27, 41, 42, 13, 95, 98]])
```
Answer

```python
def function(A, B):
    return A+B
```

#Question_14
Question

```python
AからBを生成する関数function(A)を記述せよ

A:
array([[2, 3, 3, 8],
       [2, 5, 2, 1],
       [0, 1, 8, 1],
       [3, 7, 0, 1]])
B:
array([[2, 2, 0, 3],
       [3, 5, 1, 7],
       [3, 2, 8, 0],
       [8, 1, 1, 1]])
```
Answer

```python
def function(A):
    return A.T
```

#Question_15
Question

```python
Aを生成する関数function()を記述せよ

A:
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0]])
```
Answer

```python
def function():
    return np.zeros((6, 7))
```
#Question_16
Question

```python
AからBを生成する関数function(A)を記述せよ
 
A:
array([3, 8, 4, 7, 5])

B:
array([7, 5, 3, 8, 4])
```
Answer

```python
def function(A):
    return A[[3, 4, 0, 1, 2 ]]
```
#Question_17
Question

```python
AからBを生成する関数function(A)を記述せよ

A:
array([[5, 7, 4, 6, 3],
       [1, 3, 0, 6, 4],
       [6, 3, 6, 4, 6],
       [2, 6, 3, 5, 0],
       [7, 8, 4, 0, 3],
       [6, 3, 7, 1, 5],
       [2, 7, 1, 4, 7]])
B:
array([[3],
       [0],
       [3],
       [0],
       [0],
       [1],
       [1]])
```
Answer

```python
def function(A):
    return np.reshape(np.min(A, axis=1), (7,1))
```
#Question_18

```python
A,BからCを生成する関数function(A, B)を記述せよ

A:
array([[8, 0, 4, 7, 5, 4],
       [8, 7, 0, 2, 8, 7],
       [3, 2, 3, 6, 6, 0],
       [0, 2, 7, 5, 7, 4],
       [5, 7, 5, 5, 6, 5]])
B:
array([[1, 7, 2, 1, 2, 1],
       [2, 6, 6, 1, 5, 4],
       [8, 8, 7, 1, 4, 3],
       [3, 3, 2, 0, 5, 0],
       [6, 1, 6, 0, 8, 4]])
C:
array([[8, 0, 4, 7, 5, 4],
       [8, 7, 0, 2, 8, 7],
       [3, 2, 3, 6, 6, 0],
       [0, 2, 7, 5, 7, 4],
       [5, 7, 5, 5, 6, 5],
       [1, 7, 2, 1, 2, 1],
       [2, 6, 6, 1, 5, 4],
       [8, 8, 7, 1, 4, 3],
       [3, 3, 2, 0, 5, 0],
       [6, 1, 6, 0, 8, 4]])
```
Answer

```python
def function(A, B):
    return np.vstack([A, B])
```
#Question_19

```python
A,BからCを生成する関数function(A, B)を記述せよ

A:
array([[70, 50, 60, 20, 30, 70, 20],
       [10, 10, 80, 50, 30, 60, 50],
       [60, 40, 90, 40, 10, 20, 40],
       [40, 80, 90, 20, 50, 20, 80],
       [90, 10, 20, 40, 40, 40, 20]])

B:
array([[1, 2, 2, 2, 3, 8, 1]])

C:
array([[ 70, 100, 120,  40,  90, 560,  20],
       [ 10,  20, 160, 100,  90, 480,  50],
       [ 60,  80, 180,  80,  30, 160,  40],
       [ 40, 160, 180,  40, 150, 160,  80],
       [ 90,  20,  40,  80, 120, 320,  20]])
```
Answer

```python
def function(A, B):
    return A*B
```
#Question_20

```python
A,BからCを生成する関数function(A, B)を記述せよ

A:
array([0, 6, 2, 2, 5, 8, 3, 3, 6, 6, 1, 3, 5, 3, 3, 1, 4, 4, 5, 1])

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

C:
array([[-3,  1, -2, -6, -1],
       [ 4, -4,  1,  3,  1],
       [-5, -4,  1, -3, -5],
       [-7, -2,  0,  3, -7]])
```
Answer

```python
def function(A, B):
    return np.reshape(A-B, (4, 5))
```