[Python] NumPy 배열 연산

참조


배열 연산

  • NumPy의 배열 연산은 벡터화 연산을 사용
  • 일반적으로 NumPy의 범용 함수(universal functions) 를 통해 구현
  • 배열 요소에 대한 반복적인 계산을 효율적으로 수행

브로드캐스팅(Broadcasting)

import numpy as np

a1 = np.array([1, 2, 3])
print(a1)
print(a1 + 5)

a2 = np.arange(1, 10).reshape(3, 3)
print(a2)
print(a1 + a2)

b2 = np.array([1, 2, 3]).reshape(3, 1)
print(b2)
print(a1 + b2)
[1 2 3]
[6 7 8]
[[1 2 3]
 [4 5 6]
 [7 8 9]]
[[ 2  4  6]
 [ 5  7  9]
 [ 8 10 12]]
[[1]
 [2]
 [3]]
[[2 3 4]
 [3 4 5]
 [4 5 6]]

산술 연산(Arithmetic Operators)

import numpy as np

a1 = np.arange(1, 10)
print(a1)
print(a1 + 1)
print(np.add(a1, 10))
print(a1 - 2)
print(np.subtract(a1, 10))
print(-a1)
print(np.negative(a1))
print(a1 * 2)
print(np.multiply(a1, 2))
print(a1 / 2)
print(np.divide(a1, 2))
print(a1 // 2)
print(np.floor_divide(a1, 2)) # 나눗셈 내림
print(a1 ** 2)
print(np.power(a1, 2))
print(a1 % 2)
print(np.mod(a1, 2))

b1 = np.random.randint(1, 10, size=9)
print(b1)
print(a1 + b1)
print(a1 - b1)
print(a1 * b1)
print(a1 / b1)
print(a1 // b1)
print(a1 ** b1)
print(a1 % b1)

a2 = np.arange(1, 10).reshape(3, 3)
print(a2)
b2 = np.random.randint(1, 10, size=(3, 3))
print(b2)
print(a2 + b2)
print(a2 - b2)
print(a2 * b2)
print(a2 / b2)
print(a2 // b2)
print(a2 ** b2)
print(a2 % b2)
[1 2 3 4 5 6 7 8 9]
[ 2  3  4  5  6  7  8  9 10]
[11 12 13 14 15 16 17 18 19]
[-1  0  1  2  3  4  5  6  7]
[-9 -8 -7 -6 -5 -4 -3 -2 -1]
[-1 -2 -3 -4 -5 -6 -7 -8 -9]
[-1 -2 -3 -4 -5 -6 -7 -8 -9]
[ 2  4  6  8 10 12 14 16 18]
[ 2  4  6  8 10 12 14 16 18]
[0.5 1.  1.5 2.  2.5 3.  3.5 4.  4.5]
[0.5 1.  1.5 2.  2.5 3.  3.5 4.  4.5]
[0 1 1 2 2 3 3 4 4]
[0 1 1 2 2 3 3 4 4]
[ 1  4  9 16 25 36 49 64 81]
[ 1  4  9 16 25 36 49 64 81]
[1 0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0 1]
[4 8 6 7 1 9 7 2 5]
[ 5 10  9 11  6 15 14 10 14]
[-3 -6 -3 -3  4 -3  0  6  4]
[ 4 16 18 28  5 54 49 16 45]
[0.25       0.25       0.5        0.57142857 5.         0.66666667
 1.         4.         1.8       ]
[0 0 0 0 5 0 1 4 1]
[       1      256      729    16384        5 10077696   823543       64
    59049]
[1 2 3 4 0 6 0 0 4]
[[1 2 3]
 [4 5 6]
 [7 8 9]]
[[1 6 4]
 [4 2 8]
 [5 9 3]]
[[ 2  8  7]
 [ 8  7 14]
 [12 17 12]]
[[ 0 -4 -1]
 [ 0  3 -2]
 [ 2 -1  6]]
[[ 1 12 12]
 [16 10 48]
 [35 72 27]]
[[1.         0.33333333 0.75      ]
 [1.         2.5        0.75      ]
 [1.4        0.88888889 3.        ]]
[[1 0 0]
 [1 2 0]
 [1 0 3]]
[[        1        64        81]
 [      256        25   1679616]
 [    16807 134217728       729]]
[[0 2 3]
 [0 1 6]
 [2 8 0]]

절대값 함수(Absolute Function)

  • absolute(), abs() : 내장된 절대값 함수
import numpy as np

a1 = np.random.randint(-10, 10, size=5)
print(a1)
print(np.absolute(a1))
print(np.abs(a1))
[-6 -9 -5  5  9]
[6 9 5 5 9]
[6 9 5 5 9]

제곱/제곱근 함수

  • square, squr : 제곱, 제곱근 함수
import numpy as np

a1 = np.random.randint(-10, 10, size=5)
print(a1)
print(np.square(a1))
print(np.sqrt(a1))
  print(np.sqrt(a1))
[       nan        nan 2.64575131 2.                nan]

지수와 로그 함수(Exponential and Log Function)

import numpy as np

a1 = np.random.randint(1, 10, size=5)
print(a1)
print(np.exp(a1))
print(np.exp2(a1))
print(np.power(a1, 2))

print(np.log(a1))
print(np.log2(a1))
print(np.log10(a1))
[1 6 7 1 1]
[   2.71828183  403.42879349 1096.63315843    2.71828183    2.71828183]
[  2.  64. 128.   2.   2.]
[ 1 36 49  1  1]
[0.         1.79175947 1.94591015 0.         0.        ]
[0.         2.5849625  2.80735492 0.         0.        ]
[0.         0.77815125 0.84509804 0.         0.        ]

삼각함수(Trigonometrical Function)

import numpy as np

t = np.linspace(0, np.pi, 3)
print(t)
print(np.sin(t))
print(np.cos(t))
print(np.tan(t))

x = [-1, 0, 1]
print(x)
print(np.arcsin(x))
print(np.arccos(x))
print(np.arctan(x))
[0.0000000e+00 1.0000000e+00 1.2246468e-16]
[ 1.000000e+00  6.123234e-17 -1.000000e+00]
[ 0.00000000e+00  1.63312394e+16 -1.22464680e-16]

[-1, 0, 1]
[-1.57079633  0.          1.57079633]
[3.14159265 1.57079633 0.        ]
[-0.78539816  0.          0.78539816]

집계함수(Aggregate Functions)

sum() : 합 계산

import numpy as np

a2 = np.random.randint(1, 10, size=(3, 3))
print(a2)
print(a2.sum(), np.sum(a2))
print(a2.sum(axis=0), np.sum(a2, axis=0))
print(a2.sum(axis=1), np.sum(a2, axis=1))
[[1 5 6]
 [1 8 7]
 [5 4 6]]
43 43
[ 7 17 19] [ 7 17 19]
[12 16 15] [12 16 15]

cumsum(): 누적합 계산

import numpy as np

a2 = np.random.randint(1, 10, size=(3, 3))
print(a2)
print(np.cumsum(a2))
print(np.cumsum(a2, axis=0))
print(np.cumsum(a2, axis=1))
[[1 4 2]
 [6 8 1]
 [2 2 5]]
[ 1  5  7 13 21 22 24 26 31]
[[ 1  4  2]
 [ 7 12  3]
 [ 9 14  8]]
[[ 1  5  7]
 [ 6 14 15]
 [ 2  4  9]]

diff(): 차분 계산

import numpy as np

a2 = np.random.randint(1, 10, size=(3, 3))
print(a2)
print(np.diff(a2))
print(np.diff(a2, axis=0))
print(np.diff(a2, axis=1))
[[2 2 1]
 [3 1 5]
 [7 7 7]]
[[ 0 -1]
 [-2  4]
 [ 0  0]]
[[ 1 -1  4]
 [ 4  6  2]]
[[ 0 -1]
 [-2  4]
 [ 0  0]]

prod(): 곱 계산

import numpy as np

a2 = np.random.randint(1, 10, size=(3, 3))
print(a2)
print(np.prod(a2))
print(np.prod(a2, axis=0))
print(np.prod(a2, axis=1))
[[1 2 4]
 [2 6 9]
 [2 7 2]]
24192
[ 4 84 72]
[  8 108  28]

cumprod(): 누적곱 계산

import numpy as np

a2 = np.random.randint(1, 10, size=(3, 3))
print(a2)
print(np.cumprod(a2))
print(np.cumprod(a2, axis=0))
print(np.cumprod(a2, axis=1))
[[4 9 8]
 [2 5 1]
 [2 6 2]]
[    4    36   288   576  2880  2880  5760 34560 69120]
[[  4   9   8]
 [  8  45   8]
 [ 16 270  16]]
[[  4  36 288]
 [  2  10  10]
 [  2  12  24]]

dot()/matmul(): 점곱/행렬곱 계산

import numpy as np

a2 = np.random.randint(1, 10, size=(3, 3))
print(a2)
b2 = np.ones_like(a2)
print(b2)
print(np.dot(a2, b2))
print(np.matmul(a2, b2))
[[1 3 5]
 [1 7 5]
 [9 8 9]]
[[1 1 1]
 [1 1 1]
 [1 1 1]]
[[ 9  9  9]
 [13 13 13]
 [26 26 26]]
[[ 9  9  9]
 [13 13 13]
 [26 26 26]]

tensordot(): 텐서곱 계산

import numpy as np

a2 = np.random.randint(1, 10, size=(3, 3))
print(a2)
b2 = np.ones_like(a2)
print(b2)
print(np.tensordot(a2, b2))
print(np.tensordot(a2, b2, axes=0))
print(np.tensordot(a2, b2, axes=1))
[[6 9 9]
 [8 6 5]
 [2 1 5]]
[[1 1 1]
 [1 1 1]
 [1 1 1]]
51
[[[[6 6 6]
   [6 6 6]
   [6 6 6]]

  [[9 9 9]
   [9 9 9]
   [9 9 9]]

  [[9 9 9]
   [9 9 9]
   [9 9 9]]]


 [[[8 8 8]
   [8 8 8]
   [8 8 8]]

  [[6 6 6]
   [6 6 6]
   [6 6 6]]

  [[5 5 5]
   [5 5 5]
   [5 5 5]]]


 [[[2 2 2]
   [2 2 2]
   [2 2 2]]

  [[1 1 1]
   [1 1 1]
   [1 1 1]]

  [[5 5 5]
   [5 5 5]
   [5 5 5]]]]
[[24 24 24]
 [19 19 19]
 [ 8  8  8]]

cross(): 벡터곱

import numpy as np

x = [1, 2, 3]
y = [4, 5, 6]
print(np.cross(x, y))
[-3  6 -3]

inner()/outer(): 내적/외적

import numpy as np

a2 = np.random.randint(1, 10, size=(3, 3))
print(a2)
b2 = np.ones_like(a2)
print(b2)
print(np.inner(a2, b2))
print(np.outer(a2, b2))
[[6 6 8]
 [9 3 9]
 [7 7 8]]
[[1 1 1]
 [1 1 1]
 [1 1 1]]
[[20 20 20]
 [21 21 21]
 [22 22 22]]
[[6 6 6 6 6 6 6 6 6]
 [6 6 6 6 6 6 6 6 6]
 [8 8 8 8 8 8 8 8 8]
 [9 9 9 9 9 9 9 9 9]
 [3 3 3 3 3 3 3 3 3]
 [9 9 9 9 9 9 9 9 9]
 [7 7 7 7 7 7 7 7 7]
 [7 7 7 7 7 7 7 7 7]
 [8 8 8 8 8 8 8 8 8]]

mean(): 평균 계산

import numpy as np

a2 = np.random.randint(1, 10, size=(3, 3))
print(a2)
b2 = np.ones_like(a2)
print(b2)
print(np.mean(a2))
print(np.mean(a2, axis=0))
print(np.mean(a2, axis=1))
[[1 4 9]
 [1 2 6]
 [3 3 7]]
[[1 1 1]
 [1 1 1]
 [1 1 1]]
4.0
[1.66666667 3.         7.33333333]
[4.66666667 3.         4.33333333]

std() : 표준 편차 계산

import numpy as np

a2 = np.random.randint(1, 10, size=(3, 3))
print(a2)
b2 = np.ones_like(a2)
print(b2)
print(np.std(a2))
print(np.std(a2, axis=0))
print(np.std(a2, axis=1))
[[4 8 2]
 [5 5 8]
 [4 4 8]]
[[1 1 1]
 [1 1 1]
 [1 1 1]]
2.0548046676563256
[0.47140452 1.69967317 2.82842712]
[2.49443826 1.41421356 1.88561808]

var(): 분산 계산

import numpy as np

a2 = np.random.randint(1, 10, size=(3, 3))
print(a2)
b2 = np.ones_like(a2)
print(b2)
print(np.var(a2))
print(np.var(a2, axis=0))
print(np.var(a2, axis=1))
[[1 3 1]
 [8 8 8]
 [1 3 6]]
[[1 1 1]
 [1 1 1]
 [1 1 1]]
8.88888888888889
[10.88888889  5.55555556  8.66666667]
[0.88888889 0.         4.22222222]

min(): 최소값

import numpy as np

a2 = np.random.randint(1, 10, size=(3, 3))
print(a2)
b2 = np.ones_like(a2)
print(b2)
print(np.min(a2))
print(np.min(a2, axis=0))
print(np.min(a2, axis=1))
[[2 6 7]
 [4 7 4]
 [6 4 1]]
[[1 1 1]
 [1 1 1]
 [1 1 1]]
1
[2 4 1]
[2 4 1]

max(): 최대값

import numpy as np

a2 = np.random.randint(1, 10, size=(3, 3))
print(a2)
b2 = np.ones_like(a2)
print(b2)
print(np.max(a2))
print(np.max(a2, axis=0))
print(np.max(a2, axis=1))
[[1 5 9]
 [5 1 3]
 [9 5 1]]
[[1 1 1]
 [1 1 1]
 [1 1 1]]
9
[9 5 9]
[9 5 9]

argmin(): 최소값 인덱스

import numpy as np

a2 = np.random.randint(1, 10, size=(3, 3))
print(a2)
b2 = np.ones_like(a2)
print(b2)
print(np.argmin(a2))
print(np.argmin(a2, axis=0))
print(np.argmin(a2, axis=1))
[[4 3 1]
 [4 1 4]
 [4 3 9]]
[[1 1 1]
 [1 1 1]
 [1 1 1]]
2
[0 1 0]
[2 1 1]

argmax(): 최대값 인덱스

import numpy as np

a2 = np.random.randint(1, 10, size=(3, 3))
print(a2)
b2 = np.ones_like(a2)
print(b2)
print(np.argmax(a2))
print(np.argmax(a2, axis=0))
print(np.argmax(a2, axis=1))
[[3 2 6]
 [9 7 4]
 [1 2 5]]
[[1 1 1]
 [1 1 1]
 [1 1 1]]
3
[1 1 0]
[2 0 2]

median(): 중앙값

import numpy as np

a2 = np.random.randint(1, 10, size=(3, 3))
print(a2)
b2 = np.ones_like(a2)
print(b2)
print(np.median(a2))
print(np.median(a2, axis=0))
print(np.median(a2, axis=1))

728x90

이 글을 공유하기

댓글

Designed by JB FACTORY