2_3_Max_min_values

NumPy Master Class

Chapter2 Arithmetic Operations

Notebook3 Max, min values

우리가 data science를 다루면 가장 많이 사용하는 것이 바로 최댓값과 최솟값을 구하는 것이다. 이럴 때 NumPy에서는 ndarray에서 바로 연산이 가능하도록 method를 만들어놨다. 이는 각각

1. ndarray.max()
2. ndarray.min()
3. ndarray.argmax()
4. ndarray.argmin()

이다. 1,2와 3,4를 묶어서 이번 notebook에서는 max, min을 먼저 살펴보도록 하자.


ndarray.max() and ndarray.min()

In [2]:
import numpy as np
In [14]:
test_np = np.random.randint(low = 0, high = 10, size = (10,))
print("test_np:", test_np)

print("test_np.max():", test_np.max())
print("test_np.min():", test_np.min())
test_np: [9 7 9 1 1 0 5 8 3 9]
test_np.max(): 9
test_np.min(): 0

위에서 볼 수 있듯이, ndarray.max()와 ndarray.min()은 ndarray의 최댓값과 최솟값을 구해주는 method()이다.

생각보다 위의 함수는 더 자주 사용되니까 다양한 사용법들을 잘 익혀두도록 하자.

다음은 2차원 ndarray에 적용했을 때의 모습이다.

In [15]:
test_np = np.random.randint(low = 0, high = 10, size = (3,4))
print("test_np:\n", test_np)

print("test_np.max():", test_np.max())
print("test_np.min():", test_np.min())
test_np:
 [[3 1 9 4]
 [6 0 2 0]
 [3 3 3 0]]
test_np.max(): 9
test_np.min(): 0

위와 같이 "ndarray 전체에 대하여" 최댓값과 최솟값을 뽑아주는 것을 볼 수 있다.


ndarray.max(), ndarray.min() with 2-dim ndarray

만약 2차원 ndarray에 대해 각 row-wise로 또는 column-wise로 뽑고 싶으면 어떡해야 할까? 이런 경우의 흔한 경우는 다음과 같다.

In [32]:
import pandas as pd
maths = np.random.randint(low = 30, high = 100, size = (20,))
english = np.random.randint(low = 30, high = 100, size = (20,))
physics = np.random.randint(low = 30, high = 100, size = (20,))

score_table = np.vstack((maths, english, physics)).T
d = {"Math scores": maths, "English scores": english, "Physics scores": physics}
df = pd.DataFrame(data=d)
print(df)
    Math scores  English scores  Physics scores
0            75              94              34
1            95              96              60
2            68              31              78
3            86              63              81
4            82              68              64
5            81              37              60
6            81              73              74
7            79              37              73
8            71              87              58
9            95              72              47
10           80              85              69
11           51              38              58
12           75              66              87
13           42              35              86
14           59              51              59
15           85              97              77
16           31              35              73
17           77              74              39
18           33              81              96
19           76              90              47

위와 같이 20명의 학생들에 대한 수학, 영어, 물리 성적이 있다고 해보자. 그러면 row-wise, column-wise로 max, min을 구하는 것은 다음과 같은 의미를 가진다.

1. row-wise: 개별 학생들이 수학, 영어, 물리 중 가장 높은 점수를 맞은 과목의 점수
2. column-wise: 수학, 영어, 물리과목들에 대하여 가장 높은 점수를 맞은 학생의 점수

각 차원에 대한 max, min을 구하기 위해선 ndarray.max(), ndarray.min()안에 axis를 입력해주면 된다.

먼저 우리가 가지고 있는 data와 shape을 살펴보자.

In [38]:
print("score_table:\n", score_table)
print("score_table.shape:", score_table.shape)
score_table:
 [[75 94 34]
 [95 96 60]
 [68 31 78]
 [86 63 81]
 [82 68 64]
 [81 37 60]
 [81 73 74]
 [79 37 73]
 [71 87 58]
 [95 72 47]
 [80 85 69]
 [51 38 58]
 [75 66 87]
 [42 35 86]
 [59 51 59]
 [85 97 77]
 [31 35 73]
 [77 74 39]
 [33 81 96]
 [76 90 47]]
score_table.shape: (20, 3)

처음 저자가 max, min을 사용했을 때 헷갈렸던 부분은 axis를 어떻게 설정해주면 row-wise, column-wise max 또는 min을 구할 수 있는지였다. 먼저 결과부터 살펴보자.

In [42]:
print("score_table.max(axis=0):", score_table.max(axis=0))
print("score_table.max(axis=1):", score_table.max(axis=1))
score_table.max(axis=0): [95 97 96]
score_table.max(axis=1): [94 96 78 86 82 81 81 79 87 95 85 58 87 86 59 97 73 77 96 90]

axis가 0이면 3개의 값이 나오고 axis가 1이면 20개의 값이 나온다. 이를 통해 독자들은 이렇게 외웠으면 좋겠다.

axis0이면 ndarray.max()의 결과는 0차원을 없앤 결과가 나온다.
axis1이면 ndarray.max()의 결과는 1차원을 없앤 결과가 나온다.

이를 확인하기 위해 다음 코드를 확인해보자.

In [46]:
print("score_table.shape:", score_table.shape)
print("score_table.max(axis=0).shape:", score_table.max(axis=0).shape)
print("score_table.max(axis=1).shape:", score_table.max(axis=1).shape)
score_table.shape: (20, 3)
score_table.max(axis=0).shape: (3,)
score_table.max(axis=1).shape: (20,)

위에서 볼 수 있듯이, axis를 정해주는 차원이 사라진 것을 알 수 있다.

따라서 우리가 가진 score table에서 첫 번째 차원인 20은 학생의 수, 두 번째 차원 3은 과목의 수이므로

axis=0이면 학생의 차원을 없애므로 과목의 최댓값, 최솟값을 구하고
axis=1이면 과목의 차원을 없애므로 각각 학생들의 최댓값, 최솟값을 구할 수 있게 되는 것이다.

따라서 다음과 같이 각 과목의 최댓값, 최솟값을 구할 수 있다.

In [48]:
print("Max scores for subjects:", score_table.max(axis=0))
print("min scores for subjects:", score_table.min(axis=0))
Max scores for subjects: [95 97 96]
min scores for subjects: [31 31 34]

그리고 각각 수학, 영어, 물리 성적의 최댓값, 최솟값이 된다.

그러면 각 학생들이 맞은 점수들의 최댓값, 최솟값을 구하면 다음과 같다.

In [50]:
print("Max scores for each students:", score_table.max(axis=1))
print("min scores for each students:", score_table.min(axis=1))
Max scores for each students: [94 96 78 86 82 81 81 79 87 95 85 58 87 86 59 97 73 77 96 90]
min scores for each students: [34 60 31 63 64 37 73 37 58 47 69 38 66 35 51 77 31 39 33 47]

+ Recent posts