Numpy. section2 : ndarray 만들기. Lec6. 정해진 shape으로 ndarray 만들기

timekeeep·2023년 2월 11일

Numpy

목록 보기
5/28

[1] ndarrays with specific values

  • 어떤 데이터를 초기화할때 많이 사용하는 방법 중 하나이다 (ex. one hot encoding)

  • 덧셈초기화는 0, 곱셈초기화는 1
  • 만약 0,1이 아닌 다른 값으로 초기화 하고 싶은 경우 numpy.full 사용
  • numpy.empty는 이전 메모리에서 사용했던 값을 그대로 출력해줌. empty의 핵심은 초기화가 아니라 위치잡는것
import numpy as np

M = np.zeros(shape = (2,3))

print(M.shape) # (2, 3)
print(M) # [[0. 0. 0.]
         #  [0. 0. 0.]]

M = np.ones(shape = (2,3))

print(M.shape) # (2, 3)
print(M) # [[1. 1. 1.]
         #  [1. 1. 1.]]

M = np.full(shape = (2,3), fill_value = 3.14)

print(M.shape) # (2, 3)
print(M) # [[3.14 3.14 3.14]
         #  [3.14 3.14 3.14]]

M = np.empty(shape = (2,3), fill_value = 3.14)

print(M.shape) # (2, 3)
print(M) # [[3.14 3.14 3.14]
         #  [3.14 3.14 3.14]]

[2] From existing ndarrays : 미리 존재하는 ndarray로 새로운 ndarray를 만드는

  • _like가 붙음 : 어떤 array랑 비슷한 이라는 의미(비슷하다는 관점은 shape)

# From existing ndarrays

import numpy as np 

M = np.full(shape = (2,3), fill_value = 3.14)

zeros_like = np.zeros_like(M)
ones_like = np.ones_like(M)
full_like = np.full_like(M, fill_value = 100)
empty_like = np.empty_like(M)
profile
Those who are wise will shine like the brightness of the heavens, and those who lead many to righteousness, like the stars for ever and ever

0개의 댓글