OUTTA 1차(Numpy)

서은서·2023년 7월 10일
0

OUTTA

목록 보기
1/4
post-thumbnail

Numpy

  • Numerical Python
  • 대표적인 Python 기반 수치 해석 라이브러리

<특징>

  1. 배열 연산을 수행하는 다양한 함수 제공
  2. 다차원 배열을 효과적으로 처리할 수 있도록 함
  3. Python list보다 속도가 빠름

<기능>

Numpy 배열(ndarray) : 하나의 배열 속 값들은 모두 동일한 자료형

1차원 배열

[1.3 2.3 3.1]
# shape = (3,)

2차원 배열

[[True False True]
 [False True False]]
 # shape = (2,3)

3차원 배열

[[[1 2]
  [3 4]
  [5 6]]
 [[7 8]
  [9 10]
  [11 12]]
 [[13 14]
  [15 16]
  [17 18]]]
 # shape = (3,3,2)

배열 속성 확인

  1. ndarray.dtype : 데이터의 자료형 확인
  2. ndarray.ndim : 데이터의 차원 확인
  3. ndarray.shape : 데이터의 축(axis)별 크기 확인
  4. ndarray.size : 데이터의 전체 요소 개수 확인
arr = [[[ 1, 2, 3, 4],
        [ 5, 6, 7, 8],
        [ 9, 10, 11, 12]],
       [[ 1, 2, 3, 4],
        [ 5, 6, 7, 8],
        [ 9, 10, 11, 12]]]
# arr.dtype : float64
# arr.ndim : 3
# arr.shape : (2,3,4)
# arr.size : 24

배열 생성

  1. np.array(list or tuple) : python의 list or tuple을 numpy로 만듬
np.array([1,2,3,4,5])
  1. np.zeros(shape) : 모든 원소가 0
np.zeroes((2,3))
# array([[0., 0., 0.],
#        [0., 0., 0.]])

np.ones(shape) : 모든 원소가 1

np.ones((2,3)) 
# array([[1., 1., 1.,],
#        [1., 1., 1.,]])

np.full(shape,n) : 모든 원소가 n

np.full((2,3),5)
# array([[5., 5., 5.,],
#        [5., 5., 5.,]])
  1. _np.arange(start,stop,step) : start 이상 stop 미만 , 간격이 step
np.arange(1,9,2)
# array([1,3,5,7])

np.linspace(start,stop,n등분) : start 이상 stop 이하, n등분_

np.linspace(1,9,5)
# array([1,3,5,7,9])
  1. np.random.choice(data,shape) : shape에 data중 랜덤 원소로 채움
np.random.choice([1,2,3,4,5],(2,3))
# array([[5,5,2],
#   	[2,4,2]])

np.random.shuffle(data) : data를 셔플(섞음)

np.random.shuffle([1,2,3,4,5])
# array([3,2,1,5,4])
  1. np.random.rand(shape) : [0,1)에서 각 구간의 난수 수가 균등분포를 따르도록 샘플링
    np.random.randn(shape) : 난수가 표준정규분포를 따르도록 샘플링
    np.random.randint(start,stop,shape) : start 이상 stop 미만의 정수 샘플링

배열 연산

한번에 계산 가능

#ex) 덧셈
arr1 = np.array([1,2,3,4,5])
arr2 = np.array([1,2,3,4,5])
print(arr1 + arr2)
# array([2,4,6,8,10])

+) 행렬의 곱(앞 행렬의 열 개수 == 뒤 행렬의 행 개수)
- arr1 @ arr2
  • np.sum(arr)
  • np.mean(arr)
  • np.std(arr), np.var(arr)
  • np.min(arr), np.max(arr)
  • np.argmin(arr), np.argmax(arr) : index 반환
  • np.cumsum(arr) : 누적 합
  • np.cumprod(arr) : 누적 곱
profile
내일의 나는 오늘보다 더 나아지기를 :D

0개의 댓글