SiLaure's Data

[Numpy] 05. Array Functions(함수) 본문

Records of/Learning

[Numpy] 05. Array Functions(함수)

data_soin 2021. 7. 25. 16:24

- Numpy Methods

jupyter notebook에서 글자 + Shift + Tab을 누르면 설명이 나온다.

 

import numpy as np
# 표준정규분포에서 random sampling을 한 원소를 가지는 5x3 행렬을 만든다.
mat1 = np.random.randn(5,3)
mat1
array([[-0.92644426,  0.45063478,  0.61315517],
       [-0.32615381,  1.10159801, -0.91424833],
       [-0.69560478,  0.01941608,  0.18662921],
       [ 1.30146264, -1.1894387 , -0.11196524],
       [ 0.23942379, -0.21262613, -0.11753845]])

 

# mat1에 절대값 씌우기
np.abs(mat1)

# mat1에 절대값 씌우기
np.abs(mat1)

# mat1의 square root(제곱근) 구하기
np.sqrt(mat1)
<ipython-input-6-42ee691bc19e>:2: RuntimeWarning: invalid value encountered in sqrt
  np.sqrt(mat1)
array([[       nan, 0.67129337, 0.78304225],
       [       nan, 1.04957039,        nan],
       [       nan, 0.1393416 , 0.43200603],
       [1.14081665,        nan,        nan],
       [0.48930951,        nan,        nan]])

 

# warning을 무시하고 허수를 나타내고 싶을 때
comp1 = np.array(mat1, dtype=complex)
print(np.sqrt(comp1))
# imagnary numbers = nan
[[0.        +0.96251974j 0.67129337+0.j         0.78304225+0.j        ]
 [0.        +0.57109878j 1.04957039+0.j         0.        +0.95616334j]
 [0.        +0.83402924j 0.1393416 +0.j         0.43200603+0.j        ]
 [1.14081665+0.j         0.        +1.09061391j 0.        +0.33461207j]
 [0.48930951+0.j         0.        +0.46111401j 0.        +0.34283882j]]
# mat1 제곱하기
np.square(mat1)

# mat1의 지수값 구하기
np.exp(mat1)

# mat의 log값(자연로그) 구하기
# 자연 로그 : 밑이 e인 로그, 밑이 음수가 될 수 없는 로그.
np.log(mat1)

# 상용로그
np.log10(mat1)

# 이진로그
np.log2(mat1)

# 부호찾기
np.sign(mat1)

# 올림
np.ceil(mat1)

# 내림
np.floor(mat1)

# 존재하지 않는 값이 있는지 없는지 # nan = not a number
np.isnan(mat1)

np.isnan(np.log(mat1))

np.isinf(mat1) # 무한대의 경우 --e.g.0으로 나눈 경우

np.sin(mat1)
np.cos(mat1)
np.tan(mat1)
no.tanh(mat1)

mat2 = np.random.randn(5, 3)
mat2

# 두 행렬 중 최대값인 원소만 출력
np.maximum(mat1, mat2)
array([[ 0.36643037,  0.45063478,  0.61315517],
       [-0.32615381,  1.10159801,  0.05987979],
       [ 1.79730603,  0.01941608,  0.96141945],
       [ 1.30146264,  0.39007018,  1.09685715],
       [ 0.50593645,  0.72023992,  0.21779964]])

 

 

 

- Concatenation of arrays

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr1과 arr2를 합친다?
 1. 원소 별 연산을 한다.
 2. list를 합쳐서 나타낸다.

 

1. 원소 별 연산을 한다.

arr1 + arr2
array([5, 7, 9])

 

 

2. list를 합쳐서 나타낸다. --concat

array([1, 2, 3, 4, 5, 6])으로 만들고 싶다면 concatenate([list]) 를 사용하면 된다.
np.concatenate([arr1, arr2])
array([1, 2, 3, 4, 5, 6])

 

# stacking vertically
# vstack([list]) --종방향(세로방향, 열방향)으로 붙이기
np.vstack([arr1, arr2])
array([[1, 2, 3],
       [4, 5, 6]])

 

# stacking horizontally
# hstack([list]) --횡방향(가로방향, 행방향, 일렬)으로 붙이기
np.hstack([arr1, arr2])
array([1, 2, 3, 4, 5, 6])

 

 

이처럼 Numpy에서는 concat이 조금 더 복잡하다.
list나 tuple에서는 + 연산으로 가능하기 때문.
 하지만 list, tuple에서 element-wise(원소 별 연산)를 하려면 for문을 사용해야 한다.

 

'Records of > Learning' 카테고리의 다른 글

[Numpy] Performance Test  (0) 2021.07.25
[Numpy] 06. Array Aggregation functions  (0) 2021.07.25
[Numpy] 03. Array Operation(Broadcast, Universal Function)  (0) 2021.07.23
[Numpy] 02. Array 만들어보기  (0) 2021.07.23
[Numpy] 01. Numpy란?  (0) 2021.07.23
Comments