연습

#넘파이

토마스 보젠, 2020년 9월

이 연습은 5장6장을 보완합니다.

1.

‘np’ 별칭으로 numpy를 가져옵니다.

# Your answer here.

2.

다음 배열을 만듭니다.

  1. 5개의 0으로 구성된 배열을 만듭니다.
  2. 10개의 배열을 만듭니다.
  3. 5개의 3.141로 구성된 배열을 만듭니다.
  4. 1부터 20까지의 정수 배열을 만듭니다.
  5. dtype int를 사용하여 1로 구성된 5 x 5 행렬을 만듭니다.
# Your answer here.

3.

numpy를 사용하여 다음을 수행하십시오. 1. 표준 정규 분포에서 추출한 난수로 가득 찬 3 x 3 x 3의 3D 행렬을 만듭니다(힌트: np.random.randn()). 2. 위의 배열을 (27,) 모양으로 바꿉니다.

# Your answer here.

4.

1에서 10 사이의 선형 간격 숫자 20개로 구성된 배열을 만듭니다.

# Your answer here.

5.

다음 코드를 실행하여 4 x 4 모양의 배열을 만든 다음 인덱싱을 사용하여 아래와 같은 출력을 생성합니다.

import numpy as np

a = np.arange(1, 26).reshape(5, -1)

``파이썬 20


::: {#cell-20 .cell execution_count=6}
``` {.python .cell-code}
# Your answer here.
```
:::


``파이썬
배열([[ 9, 10],
       [14, 15],
       [19, 20],
       [24, 25]])
# Your answer here.

``파이썬 배열([ 6, 7, 8, 9, 10])


::: {#cell-24 .cell execution_count=8}
``` {.python .cell-code}
# Your answer here.
```
:::


``파이썬
배열([[11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20]])
# Your answer here.

``파이썬 배열([[ 8, 9], [13, 14]])


::: {#cell-28 .cell execution_count=10}
``` {.python .cell-code}
# Your answer here.
```
:::


### 6.

'a'에 있는 모든 숫자의 합을 계산합니다.

::: {#cell-31 .cell execution_count=11}
``` {.python .cell-code}
# Your answer here.
```
:::


### 7.

`a`의 각 행의 합을 계산합니다.

::: {#cell-34 .cell execution_count=12}
``` {.python .cell-code}
# Your answer here.
```
:::


### 8.

`a`의 평균보다 큰 `a`의 모든 값을 추출합니다(힌트: 부울 마스크 사용).

::: {#cell-37 .cell execution_count=13}
``` {.python .cell-code}
# Your answer here.
```
:::


### 9.

다음 배열 `b`에서 최소값의 위치를 ​​찾습니다.

::: {#cell-40 .cell execution_count=14}
``` {.python .cell-code}
np.random.seed(123)
b = np.random.randn(10)
b
```

::: {.cell-output .cell-output-display execution_count=14}
```
array([-1.0856306 ,  0.99734545,  0.2829785 , -1.50629471, -0.57860025,
        1.65143654, -2.42667924, -0.42891263,  1.26593626, -0.8667404 ])
```
:::
:::


::: {#cell-41 .cell execution_count=15}
``` {.python .cell-code}
# Your answer here.
```
:::


### 10.

다음 2D 배열 `c`에서 최대값의 위치를 ​​찾으세요(힌트: 이 질문을 수행하는 방법은 여러 가지가 있지만 일반적으로 stackoverflow.com에서 빠르게 검색하면 문제에 대한 최적의 솔루션을 찾는 데 도움이 됩니다. 예를 들어 [게시물](https://stackoverflow.com/questions/3584243/get-the-position-of-the-biggest-item-in-a-multi-Dimension-numpy-array) 참조).

::: {#cell-44 .cell execution_count=16}
``` {.python .cell-code}
np.random.seed(123)
c = np.random.randn(3, 2)
c
```

::: {.cell-output .cell-output-display execution_count=16}
```
array([[-1.0856306 ,  0.99734545],
       [ 0.2829785 , -1.50629471],
       [-0.57860025,  1.65143654]])
```
:::
:::


::: {#cell-45 .cell execution_count=17}
``` {.python .cell-code}
# Your answer here.
```
:::


<hr>
<hr>
<hr>

## 솔루션

### 1.

'np' 별칭으로 numpy를 가져옵니다.

::: {#cell-50 .cell execution_count=18}
``` {.python .cell-code}
import numpy as np
```
:::


### 2.

다음 배열을 만듭니다.

1. 5개의 0으로 구성된 배열을 만듭니다.
2. 10개의 배열을 만듭니다.
3. 5개의 3.141로 구성된 배열을 만듭니다.
4. 1부터 20까지의 정수 배열을 만듭니다.
5. dtype `int`를 사용하여 1로 구성된 5 x 5 행렬을 만듭니다.

::: {#cell-53 .cell execution_count=19}
``` {.python .cell-code}
print(np.zeros(5))
print(np.ones(10))
print(np.full(5, 3.141))
print(np.array(range(21)))
print(np.ones((5, 5), dtype=int))
```

::: {.cell-output .cell-output-stdout}
```
[0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[3.141 3.141 3.141 3.141 3.141]
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20]
[[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]]
```
:::
:::


### 3.

numpy를 사용하여 다음을 수행하십시오.
1. 표준 정규 분포에서 추출한 난수로 가득 찬 3 x 3 x 3의 3D 행렬을 만듭니다(힌트: `np.random.randn()`).
2. 위의 배열을 (27,) 모양으로 바꿉니다.

::: {#cell-56 .cell execution_count=20}
``` {.python .cell-code}
x = np.random.randn(3, 3, 3)
x
```

::: {.cell-output .cell-output-display execution_count=20}
```
array([[[-2.42667924, -0.42891263,  1.26593626],
        [-0.8667404 , -0.67888615, -0.09470897],
        [ 1.49138963, -0.638902  , -0.44398196]],

       [[-0.43435128,  2.20593008,  2.18678609],
        [ 1.0040539 ,  0.3861864 ,  0.73736858],
        [ 1.49073203, -0.93583387,  1.17582904]],

       [[-1.25388067, -0.6377515 ,  0.9071052 ],
        [-1.4286807 , -0.14006872, -0.8617549 ],
        [-0.25561937, -2.79858911, -1.7715331 ]]])
```
:::
:::


::: {#cell-57 .cell execution_count=21}
``` {.python .cell-code}
x.reshape(-1)  # or x.reshape(27)
```

::: {.cell-output .cell-output-display execution_count=21}
```
array([-2.42667924, -0.42891263,  1.26593626, -0.8667404 , -0.67888615,
       -0.09470897,  1.49138963, -0.638902  , -0.44398196, -0.43435128,
        2.20593008,  2.18678609,  1.0040539 ,  0.3861864 ,  0.73736858,
        1.49073203, -0.93583387,  1.17582904, -1.25388067, -0.6377515 ,
        0.9071052 , -1.4286807 , -0.14006872, -0.8617549 , -0.25561937,
       -2.79858911, -1.7715331 ])
```
:::
:::


### 4.

1에서 10 사이의 선형 간격 숫자 20개로 구성된 배열을 만듭니다.

::: {#cell-60 .cell execution_count=22}
``` {.python .cell-code}
np.linspace(1, 10, 20)
```

::: {.cell-output .cell-output-display execution_count=22}
```
array([ 1.        ,  1.47368421,  1.94736842,  2.42105263,  2.89473684,
        3.36842105,  3.84210526,  4.31578947,  4.78947368,  5.26315789,
        5.73684211,  6.21052632,  6.68421053,  7.15789474,  7.63157895,
        8.10526316,  8.57894737,  9.05263158,  9.52631579, 10.        ])
```
:::
:::


### 5.

아래에서는 4 x 4 모양의 배열을 정의했습니다. 인덱싱을 사용하여 주어진 출력을 생성합니다.

::: {#cell-63 .cell execution_count=23}
``` {.python .cell-code}
a = np.arange(1, 26).reshape(5, -1)
a
```

::: {.cell-output .cell-output-display execution_count=23}
```
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]])
```
:::
:::


``파이썬
20
a[3, 4]
20

``파이썬 배열([[ 9, 10], [14, 15], [19, 20], [24, 25]])


::: {#cell-67 .cell execution_count=25}
``` {.python .cell-code}
a[1:, 3:]
```

::: {.cell-output .cell-output-display execution_count=25}
```
array([[ 9, 10],
       [14, 15],
       [19, 20],
       [24, 25]])
```
:::
:::


``파이썬
배열([ 6, 7, 8, 9, 10])
a[1, :]
array([ 6,  7,  8,  9, 10])

``파이썬 배열([[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]])


``파이썬
배열([[ 8, 9],
       [13, 14]])
a[1:3, 2:4]
array([[ 8,  9],
       [13, 14]])

6.

’a’에 있는 모든 숫자의 합을 계산합니다.

a.sum()
325

7.

a의 각 행의 합을 계산합니다.

a.sum(axis=1)
array([ 15,  40,  65,  90, 115])

8.

a의 평균보다 큰 a의 모든 값을 추출합니다(힌트: 부울 마스크 사용).

a[a > a.mean()]
array([14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25])

9.

다음 배열 b에서 최소값의 위치를 ​​찾습니다.

np.random.seed(123)
b = np.random.randn(10)
b
array([-1.0856306 ,  0.99734545,  0.2829785 , -1.50629471, -0.57860025,
        1.65143654, -2.42667924, -0.42891263,  1.26593626, -0.8667404 ])
b.argmin()
6

10.

다음 2D 배열 c에서 최대값의 위치를 ​​찾으세요(힌트: 이 질문을 수행하는 방법은 여러 가지가 있지만 일반적으로 stackoverflow.com에서 빠르게 검색하면 문제에 대한 최적의 솔루션을 찾는 데 도움이 됩니다. 예를 들어 게시물 참조).

np.random.seed(123)
c = np.random.randn(3, 2)
c
array([[-1.0856306 ,  0.99734545],
       [ 0.2829785 , -1.50629471],
       [-0.57860025,  1.65143654]])
print(f"Location of maximum: {np.unravel_index(c.argmax(), c.shape)}")
print(f"   Value of maximum: {c.max():.2f}")
Location of maximum: (2, 1)
   Value of maximum: 1.65