[부스트캠프 AI Tech 5기] Pre-Course : (5) numpy part I

araseo·2022년 12월 12일
0
post-thumbnail

📖 코드로 방정식 표현하기

coefficient_matrix = [[2, 2, 1], [2, -1, 2], [1, -1, 2]]
constant_vector = [9,6,5]
  • 다양한 Matrix 계산을 어떻게 만들 것인가?
  • 굉장히 큰 Matrix에 대한 표현
  • 처리 속도 문제 - python은 Interpreter 언어

📖 numpy

  • Numerical Python
  • 파이썬의 고성능 과학 계산용 패키지
  • Matrix와 Vector와 같은 Array 연산의 사실상의 표준
  • 한글로는 넘파이로 주로 통칭
  • 누군가는 넘피/늄파이라고 부르기도 함
  • 일반 List에 비해 빠르고, 메모리 효율적
  • 반복문 없이 데이터 배열에 대한 처리를 지원함
  • 선형대수와 관련된 다양한 기능을 제공함
  • C, C++, 포트란 등의 언어와 통합 가능

📖 numpy install

activate ml
conda install numpy
  • Windows 환경에선 conda로 패키지 관리 필요 (C 패키지 핸들링 등)
  • jupyter 등을 설치한 상태에서는 추가 설치 필요 없음

📖 python import numpy as np

  • numpy의 호출 방법
  • 일반적으로 numpy는 np라는 alias(별칭) 이용해서 호출함
  • 특별한 이유는 없음, 세계적인 약속 같은 것

📖 array creation

test_array = np.array([1, 4, 5, 8], float)
print(test_array)
type(test_array[3])
  • numpy는 np.array 함수를 활용하여 배열을 생성함 ➔ ndarray
  • numpy는 하나의 데이터 type만 배열에 넣을 수 있음
  • List와 가장 큰 차이점 ➔ dynamic typing not supported
  • C의 Array를 사용하여 배열을 생성함
test_array = np.array([1, 4, 5, 8], float)
test_array
array([ 1., 4., 5., 8.])
test([test_array[3]])
numpy.float64

(출처 : https://jakevdp.github.io/blog/2014/05/09/why-python-is-slow/)

(출처 : https://www.slideshare.net/enthought/numpy-talk-at-siam)

  • shape : numpy array의 dimension 구성을 반환함
  • dtype : numpy array의 데이터 type을 반환함
# String Type의 데이터를 입력해도
test_array = np.array([1, 4, 5, "8"], float)
print(test_array)
array([ 1., 4., 5., 8.])
# Float Type으로 자동 형변환을 실시
print(type(test_array[3]))
numpy.float64
# Array(배열) 전체의 데이터 Type을 반환함
print(test_array.dtype)
dtype('float64')
# Array(배열) 의 shape을 반환함
print(test_array.shape)
(4,)

📖 array shape

  • array의 RANK에 따라 불리는 이름이 있음
RankNameExample
0scalar7
1vector[10, 10]
2matrix[[10, 10], [15, 15]]
33-tensor[[[ 1, 5, 9], [ 2, 6, 10]], [[ 3, 7, 11], [ 4, 8, 12]]]
nn-tensor
  • vector
test_array = np.array([1, 4, 5, "8"], float) # shape = (4,)
test_array
array([ 1., 4., 5., 8.])
  • matrix
matrix = [[1,2,5,8],[1,2,5,8],[1,2,5,8]]
np.array(matrix, int).shape
(3, 4)
  • 3rd order tensor
  • ndim - number of dimensions
  • side - data의 개수
tensor = [[[1,2,5,8],[1,2,5,8],[1,2,5,8]],
          [[1,2,5,8],[1,2,5,8],[1,2,5,8]],
          [[1,2,5,8],[1,2,5,8],[1,2,5,8]]]
np.array(tensor, int).shape
(4, 3, 4)

📖 array dtype

  • ndarray의 single element가 가지는 data type
  • 각 element가 차지하는 memory의 크기가 결정됨
# data type을 integer로 선언
np.array([1, 2, 3], [4.5, 5, 6], dtype=int)
array([[1, 2, 3],
	   [4, 5, 6]])
# data type을 float로 선언
np.array([1, 2, 3], [4.5, "5", "6"], dtype=np.float32)
array([[ 1. , 2. , 3. ],
	   [ 4.5, 5. , 6. ]], dtype=float32)
  • C의 data type과 compatible

(출처 : https://www.slideshare.net/enthought/numpy-talk-at-siam)

📖 array nbytes

  • nbytes - ndarray obeject의 메모리 크기를 반환함
# 32bits = 4bytes -> 6 * 4bytes
np.array([[1, 2, 3], [4.5, "5", "6"]], dtype=np.float32).nbytes
24
# 8bits = 1bytes -> 6 * 1bytes
np.array([[1, 2, 3], [4.5, "5", "6"]], dtype=np.int8).nbytes
6
# 64bits = 8bytes -> 6 * 8bytes
np.array([[1, 2, 3], [4.5, "5", "6"]], dtype=np.float64).nbytes
48

<이 게시물은 최성철 교수님의 numpy 강의 자료를 참고하여 작성되었습니다.>

본 포스트의 학습 내용은 [부스트캠프 AI Tech 5기] Pre-Course 강의 내용을 바탕으로 작성되었습니다.
부스트캠프 AI Tech 5기 Pre-Course는 일정 기간 동안에만 운영되는 강의이며,
AI 관련 강의를 학습하고자 하시는 분들은 부스트코스 AI 강좌에서 기간 제한 없이 학습하실 수 있습니다.
(https://www.boostcourse.org/)

profile
AI를 공부하고 있는 학생입니다:)

0개의 댓글