데이터사이언스 기록기📚

[Numpy] ndarray 기본 함수 사용하기(add, subtract, multiply, divide, mean, max, argmax, var, median, std, sum, cumsum, any, all, where) 본문

Data/Data

[Numpy] ndarray 기본 함수 사용하기(add, subtract, multiply, divide, mean, max, argmax, var, median, std, sum, cumsum, any, all, where)

syunze 2022. 6. 16. 16:13

1. 연산 함수

1) add

import numpy as np

x = np.arange(15).reshape(3,5)
y = np.random.rand(15).reshape(3,5)
print(x)
# [[ 0  1  2  3  4]
# [ 5  6  7  8  9]
# [10 11 12 13 14]]

print(y)
# [[0.70289565 0.06326522 0.53205322 0.93712744 0.06747579]
# [0.76030664 0.90283428 0.08326617 0.80178212 0.92180257]
# [0.39573596 0.18643768 0.4006741  0.95827514 0.28076342]]
# 둘 다 출력값 동일
np.add(x,y)
x + y

# array([[ 0.70289565,  1.06326522,  2.53205322,  3.93712744,  4.06747579],
#       [ 5.76030664,  6.90283428,  7.08326617,  8.80178212,  9.92180257],
#       [10.39573596, 11.18643768, 12.4006741 , 13.95827514, 14.28076342]])

 

 2) subtract

# 빼기
np.subtract(x,y)
x - y

# array([[-0.70289565,  0.93673478,  1.46794678,  2.06287256,  3.93252421],
#       [ 4.23969336,  5.09716572,  6.91673383,  7.19821788,  8.07819743],
#       [ 9.60426404, 10.81356232, 11.5993259 , 12.04172486, 13.71923658]])

 

 3) multiply

# 곱하기
np.multiply(x,y)
x * y

# array([[ 0.        ,  0.06326522,  1.06410645,  2.81138232,  0.26990318],
#       [ 3.80153321,  5.41700567,  0.58286316,  6.41425694,  8.29622309],
#       [ 3.95735957,  2.05081443,  4.80808915, 12.45757683,  3.93068786]])

 

 4) divide

# 나누기
np.divide(x,y)
x / y

# array([[ 0.        , 15.80647321,  3.75902243,  3.20127218, 59.28051784],
#       [ 6.57629399,  6.64573792, 84.06776009,  9.97777305,  9.76347901],
#       [25.26937428, 59.00095027, 29.94952786, 13.56604116, 49.86404591]])

2. 통계 함수

 1) 평균

print(y)
# [[0.70289565 0.06326522 0.53205322 0.93712744 0.06747579]
# [0.76030664 0.90283428 0.08326617 0.80178212 0.92180257]
# [0.39573596 0.18643768 0.4006741  0.95827514 0.28076342]]
# 두 함수 값 동일
np.mean(y)
y.mean()

# 0.532979692385892

 

 2) 최대값

   - 최대값

np.max(y)
# 0.9582751407964818

   - 최대값 인덱스 출력 (flatten 형태로 가정하고 값 출력)

np.argmax(y)
# 13

 

 3) 분산, 중앙값, 표준편차

  - 분산

np.var(y) # 0.1096403128937475

  - 중앙값

np.median(y)  # 0.5320532238974515

  - 표준편차

np.std(y)   # 0.33111978632172906

3. 집계 함수

 1) 합계(sum)

y
# array([[0.70289565, 0.06326522, 0.53205322, 0.93712744, 0.06747579],
#       [0.76030664, 0.90283428, 0.08326617, 0.80178212, 0.92180257],
#       [0.39573596, 0.18643768, 0.4006741 , 0.95827514, 0.28076342]])
# 전체의 합
np.sum(y)   # 7.994695385788381

# 축에 따라 합 구하기
np.sum(y, axis = 0)
# array([0.70289565, 0.76616087, 1.2982141 , 2.23534154, 2.30281733,
#       3.06312397, 3.96595825, 4.04922442, 4.85100653, 5.7728091 ,
#       6.16854506, 6.35498273, 6.75565683, 7.71393197, 7.99469539])

 

 2) 누적합계(cumsum)

  - 바로 앞의 원소 + 자신의 원소

np.cumsum(y)
# array([0.70289565, 0.76616087, 1.2982141 , 2.23534154, 2.30281733,
#       3.06312397, 3.96595825, 4.04922442, 4.85100653, 5.7728091 ,
#       6.16854506, 6.35498273, 6.75565683, 7.71393197, 7.99469539])

4. any, all 함수

1) any : 특정 조건을 만족하는 것이 하나라도 있으면 True, 아니면 False

z = np.random.randn(10)
print(z)

# [ 0.84120195  2.66761606  0.68567952 -0.50196533  1.17161333 -1.68854291
#  -0.89068659  0.2459505   1.16936931  0.47754348]
np.any(z > 0)   # True

2) all : 모든 원소가 특정 조건을 만족한다면 True, 아니면 False

np.all(z > 0)    # False

5. where 함수

 - 조건에 따라 선별적으로 값을 선택 가능

 - np.where(조건, 참일 때, 거짓일 때)  형식으로 사용 

z = np.random.randn(10)
print(z)

# [ 0.46612263  0.20375071  1.40553431  0.71995414 -0.4183428  -0.56261078
#  -0.08080628 -1.54597073  2.30450022 -0.14190329]
np.where(z > 0, z, 0)
# array([0.46612263, 0.20375071, 1.40553431, 0.71995414, 0.        ,
#       0.        , 0.        , 0.        , 2.30450022, 0.        ])
728x90
Comments