NumPy

Python 입문

Hugo Bowne-Anderson

Data Scientist at DataCamp

리스트 요약

  • 강력함

  • 값들의 모음

  • 다양한 타입 보유

  • 변경, 추가, 삭제 가능

  • 데이터 과학에 필요

    • 집합 대한 수학적 연산

    • 속도

Python 입문

예시 살펴보기

height = [1.73, 1.68, 1.71, 1.89, 1.79]
height
[1.73, 1.68, 1.71, 1.89, 1.79]
weight = [65.4, 59.2, 63.6, 88.4, 68.7]
weight
[65.4, 59.2, 63.6, 88.4, 68.7]
weight / height ** 2
TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
Python 입문

해결 방법: NumPy

  • Numeric Python

  • Python 리스트의 대안: NumPy 배열

  • 전체 배열에 대한 계산

  • 쉽고 빠름

  • 설치

    • 터미널에서 pip3 install numpy
Python 입문

NumPy

import numpy as np

np_height = np.array(height) np_height
array([1.73, 1.68, 1.71, 1.89, 1.79])
np_weight = np.array(weight)
np_weight
array([65.4, 59.2, 63.6, 88.4, 68.7])
bmi = np_weight / np_height ** 2
bmi
array([21.85171573, 20.97505669, 21.75028214, 24.7473475 , 21.44127836])
Python 입문

비교

height = [1.73, 1.68, 1.71, 1.89, 1.79]
weight = [65.4, 59.2, 63.6, 88.4, 68.7]
weight / height ** 2
TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
np_height = np.array(height)
np_weight = np.array(weight)
np_weight / np_height ** 2
array([21.85171573, 20.97505669, 21.75028214, 24.7473475 , 21.44127836])
Python 입문

NumPy: 유의사항

np.array([1.0, "is", True])
array(['1.0', 'is', 'True'], dtype='<U32')
  • NumPy 배열: 한 가지 타입만 포함
Python 입문

NumPy: 유의사항

python_list = [1, 2, 3]
numpy_array = np.array([1, 2, 3])
python_list + python_list
[1, 2, 3, 1, 2, 3]
numpy_array + numpy_array
array([2, 4, 6])
  • 다른 타입: 다른 동작!
Python 입문

NumPy 부분 추출

bmi
array([21.85171573, 20.97505669, 21.75028214, 24.7473475 , 21.44127836])
bmi[1]
20.975
bmi > 23
array([False, False, False,  True, False])
bmi[bmi > 23]
array([24.7473475])
Python 입문

연습해봅시다!

Python 입문

Preparing Video For Download...