티스토리 뷰

반응형

모든 연산 함수는 np모듈에 포함되어 있다.

 

산술 연산(Arithmetic Operations)

# arange로 1부터 10 미만의 범위에서 1씩 증가하는 배열 생성
# 배열의 shape을 (3, 3)으로 지정
a = np.arange(1, 10).reshape(3, 3)
[[1 2 3]
[4 5 6]
[7 8 9]]

# arange로 9부터 0까지 범위에서 1씩 감소하는 배열 생성
# 배열의 shape을 (3, 3)으로 지정
b = np.arange(9, 0, -1).reshape(3, 3)
[[9 8 7]
[6 5 4]
[3 2 1]]

# 빼기
a - b
array([[-8, -6, -4],
     [-2,  0,  2],
     [ 4,  6,  8]])
     
np.subtract(a, b)
array([[-8, -6, -4],
     [-2,  0,  2],
     [ 4,  6,  8]])
     
# 더하기
a+b
array([[10, 10, 10],
     [10, 10, 10],
     [10, 10, 10]])

np.add(a, b)
array([[10, 10, 10],
     [10, 10, 10],
     [10, 10, 10]])

# 나누기
a/b
np.divide(a, b)
array([[ 0.11111111,  0.25      ,  0.42857143],
     [ 0.66666667,  1.        ,  1.5       ],
     [ 2.33333333,  4.        ,  9.        ]])

# 곱하기
a*b
np.multiply(a, b)
array([[ 9, 16, 21],
     [24, 25, 24],
     [21, 16,  9]])
     
# 지수
np.exp(b)
array([[  8.10308393e+03,   2.98095799e+03,   1.09663316e+03],
     [  4.03428793e+02,   1.48413159e+02,   5.45981500e+01],
     [  2.00855369e+01,   7.38905610e+00,   2.71828183e+00]])
     
# 제곱근
np.sqrt(a)
array([[ 1.        ,  1.41421356,  1.73205081],
     [ 2.        ,  2.23606798,  2.44948974],
     [ 2.64575131,  2.82842712,  3.        ]])
     
# sin
np.sin(a)
array([[ 0.84147098,  0.90929743,  0.14112001],
     [-0.7568025 , -0.95892427, -0.2794155 ],
     [ 0.6569866 ,  0.98935825,  0.41211849]])

# cos
np.cos(a)
array([[ 0.54030231, -0.41614684, -0.9899925 ],
     [-0.65364362,  0.28366219,  0.96017029],
     [ 0.75390225, -0.14550003, -0.91113026]])
     
# tan
np.tan(a)
array([[ 1.55740772, -2.18503986, -0.14254654],
     [ 1.15782128, -3.38051501, -0.29100619],
     [ 0.87144798, -6.79971146, -0.45231566]])
     
# log
np.log(a)
array([[ 0.        ,  0.69314718,  1.09861229],
     [ 1.38629436,  1.60943791,  1.79175947],
     [ 1.94591015,  2.07944154,  2.19722458]])
     
# dot product(내적)
np.dot(a, b)
array([[ 30,  24,  18],
     [ 84,  69,  54],
     [138, 114,  90]])

 

내적 관련은 아래의 페이지에서 공부하자.

https://datascienceschool.net/view-notebook/3f44cfdda2874080a9aa6b034c71d5ec/

 

Data Science School

Data Science School is an open space!

datascienceschool.net

 

 

집계합수(Aggregate Functions)

NumPu의 모든 집계 합수는 AXIS 기준으로 계산된다. 집계함수에서 AXIS를 지정하지 않으면 None이다.

  • axis=None은 전체 행렬을 하나의 배열로 간주하고 집계 함수의 범위를 전체 행렬로 정의한다.
  • axis=0은 행을 기준으로 각 행의 동일 인덱스의 요소를 그룹으로 한다. 각 그룹을 집계 함수의 범위로 정의한.
  • axis=1은 열을 기준으로 각 열의 요소를 그룹으로 한다. 각 그룹을 집계 함수의 범위로 정의한다.
# arange로 1부터 10미만의 범위에서 1씩 증가하는 배열 생성
# 배열의 shape을 (3, 3)으로 지정
a = np.arange(1, 10).reshape(3, 3)
[[1 2 3]
[4 5 6]
[7 8 9]]

 

np.sum()

a.sum(), np.sum(a)
> (45, 45)

a.sum(axis=0), np.sum(a, axis=0)
> (array([12, 15, 18]), array([12, 15, 18]))

a.sum(axis=1), np.sum(a, axis=1)
> (array([ 6, 15, 24]), array([ 6, 15, 24]))

 

np.min(), np.max()

a.min(), np.min(a)
> (1, 1)

a.min(axis=0), np.min(a, axis=0)
> (array([1, 2, 3]), array([1, 2, 3]))

a.min(axis=1), np.min(a, axis=1)
> (array([1, 4, 7]), array([1, 4, 7]))

a.max(), np.max(a)
> (9, 9)

a.max(axis=0), np.max(a, axis=0)
> (array([7, 8, 9]), array([7, 8, 9]))

a.max(axis=1), np.max(a, axis=1)
> (array([3, 6, 9]), array([3, 6, 9]))

 

np.cumsum(): 누적 합계

a.cumsum(), np.cumsum(a)
> (array([ 1,  3,  6, 10, 15, 21, 28, 36, 45]),
 array([ 1,  3,  6, 10, 15, 21, 28, 36, 45]))

a.cumsum(axis=0), np.cumsum(a, axis=0)
(array([[ 1,  2,  3],
      [ 5,  7,  9],
      [12, 15, 18]]), 
 array([[ 1,  2,  3],
      [ 5,  7,  9],
      [12, 15, 18]]))
      
a.cumsum(axis=1), np.cumsum(a, axis=1)
> (array([[ 1,  3,  6],
      [ 4,  9, 15],
      [ 7, 15, 24]]), 
  array([[ 1,  3,  6],
      [ 4,  9, 15],
      [ 7, 15, 24]]))

 

np.mean() : 평균

a.mean(), np.mean(a)
> (5.0, 5.0)

a.mean(axis=0), np.mean(a, axis=0)
> (array([ 4.,  5.,  6.]), array([ 4.,  5.,  6.]))

a.mean(axis=1), np.mean(a, axis=1)
> (array([ 2.,  5.,  8.]), array([ 2.,  5.,  8.]))

 

np.mean(): 중앙값

np.median(a)
> 5.0

 

np.corrcodf() : 상관계수(Correlation coeficient)

np.corrcoef(a)
array([[ 1.,  1.,  1.],
     [ 1.,  1.,  1.],
     [ 1.,  1.,  1.]])

 

np.std() : 표준편차

a.std(), np.std(a)
> (2.5819888974716112, 2.5819888974716112)

a.std(axis=0), np.std(a, axis=0)
> (array([ 2.44948974,  2.44948974,  2.44948974]),
 array([ 2.44948974,  2.44948974,  2.44948974]))
 
a.std(axis=1), np.std(a, axis=1)
>(array([ 0.81649658,  0.81649658,  0.81649658]),
 array([ 0.81649658,  0.81649658,  0.81649658]))

 

 

브로드캐스팅(Broadcasting)

두 배열 간의 Shape이 다를 경우 두 배열 간의 형상을 맞추는 브로드캐스팅 과정을 거친다.

https://mathematica.stackexchange.com/questions/99171/how-to-implement-the-general-array-broadcasting-method-from-numpy In [2]:

# 배열 생성
a = np.arange(1, 25).reshape(4, 6)
[[ 1  2  3  4  5  6]
[ 7  8  9 10 11 12]
[13 14 15 16 17 18]
[19 20 21 22 23 24]]

b = np.arange(25, 49).reshape(4, 6)
[[25 26 27 28 29 30]
[31 32 33 34 35 36]
[37 38 39 40 41 42]
[43 44 45 46 47 48]]

 

같은 shape 끼리의 이항 연산

a+b
array([[26, 28, 30, 32, 34, 36],
     [38, 40, 42, 44, 46, 48],
     [50, 52, 54, 56, 58, 60],
     [62, 64, 66, 68, 70, 72]])

 

다른 shape 벼열의 연산

Case 1: 배열과 스칼라

배열과 스칼라 사이의 이항 연산 시 스칼라를 배열로 변형한다.

# 데모 배열 생성
a = np.arange(1, 25).reshape(4, 6)
[[ 1  2  3  4  5  6]
[ 7  8  9 10 11 12]
[13 14 15 16 17 18]
[19 20 21 22 23 24]]

a+100
array([[101, 102, 103, 104, 105, 106],
     [107, 108, 109, 110, 111, 112],
     [113, 114, 115, 116, 117, 118],
     [119, 120, 121, 122, 123, 124]])

a+100의 처리 과정은 아래와 같다.

# step 1: 스칼라 배열 변경
new_arr = np.full_like(a, 100)
[[100 100 100 100 100 100]
[100 100 100 100 100 100]
[100 100 100 100 100 100]
[100 100 100 100 100 100]]

# step 2: 배열 이항 연산
a+new_arr
array([[101, 102, 103, 104, 105, 106],
     [107, 108, 109, 110, 111, 112],
     [113, 114, 115, 116, 117, 118],
     [119, 120, 121, 122, 123, 124]])

 

Case 2: Shaep이 다른 배열들의 연산

# 데모 배열 생성
a = np.arange(5).reshape((1, 5))
[[0 1 2 3 4]]

b = np.arange(5).reshape((5, 1))
[[0]
[1]
[2]
[3]
[4]]

a+b
array([[0, 1, 2, 3, 4],
     [1, 2, 3, 4, 5],
     [2, 3, 4, 5, 6],
     [3, 4, 5, 6, 7],
     [4, 5, 6, 7, 8]])
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
26 27 28 29 30 31
글 보관함