티스토리 뷰

728x90
반응형

Numpy

Numpy Array

  • 한 array 객체에는 모두 동일한 타입의 elements들이 포함됨

dtype

  • array 객체에 포함된 elements의 타입을 설명하는 객체

ndim

차원의 수, 축의 수

import numpy as np

a = np.array([[0, 1], [5, 6]])
print(a.ndim) # 2

shape

  • 각 차원의 크기를 나타내는 integer들의 tuple
  • tuple 객체의 length는 ndim과 동일함
import numpy as np

a = np.array([[0, 1], [5, 6]])
print(a.shape)

size

  • 행렬의 모든 원소 갯수
  • shape의 elements들을 모두 곱한 결과와 같음
import numpy as np

a = np.array([[0, 1, 2], [3, 4, 5]])

print(a)
print(a.ndim)
print(a.shape)
print(a.size)

"""
[[0 1 2]
 [3 4 5]]
2
(2, 3)
6
"""

Creating Arrays

비어 있는 array 생성

  • array 생성 후 shape를 변경하는 것보다 생성한 array의 elements를 변경하는 것이 더 효율적
  • shape를 나타내는 tuple 객체와 dtype (생략 가능)을 arguments로 대입하여 아래의 함수들로 array 생성
  • np.zeros(), np.empty(), np.ones()
  • 다른 array의 shape와 dtype을 이용한 array 생성(np.zero_like(), np.ones_like(), np.empty_like())
b = np.zeros((2, 3), dtype=np.float)
print(b)

c = np.empty((2, 4), dtype=np.float32)
print(c)

d = np.ones((2, 2))
print(d)

e = np.zeros_like(d)
print(e)

"""
[[0. 0. 0.]
 [0. 0. 0.]]
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]]
[[1. 1.]
 [1. 1.]]
[[0. 0.]
 [0. 0.]]
"""

identity 행렬(단위 행렬) 생성

  • np.identity(), np.eye()

np.arange()

  • range 함수와 유사하게 동작, numpy array 객체를 반환
  • arguments로 float 타입 사용 가능

np.linspace

  • start, end, num
  • start와 end arguments를 포함하여 num 횟수만큼의 등간격으로 숫자가 나열된 numpy array 반환
i = np.identity(3)
print(i)
l = np.eye(3, 4, 1)
np.arange(10, 20, 2)
print(l)
lin = np.linspace(0, 1, 11)
print(lin)

"""
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
[[0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
[0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ]
"""

Opertioins Between Arrays and Scalars

Vectorization

  • numpy는 기본적으로 array간의 사칙연산을 지원
  • 행과 열이 같은 배열을 계산하면 값이 같은 위치에 있는 값의 곱이 되는데 이런 현상을 Element-wise operations라 함. shape이 같을 때 발생.

Scalar간의 연산

  • Array와 scalar 간의 연산시에는 모든 element에 대해 각각 연산이 수행.

행렬 곱 연산

  • * 연산자는 element-wise 곱셈이 수행됨
  • 행렬 곱을 수행하기 ㅜ이해서는 dot() 메서드 이용
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.identity(3)

print(a * a)
print(a - a)
print(a.dot(b))

"""
[[ 1  4  9]
 [16 25 36]]
[[0 0 0]
 [0 0 0]]
[[1. 2. 3.]
 [4. 5. 6.]]
"""

기존 객체의 수정

  • np array를 이용한 연산을 수행하면 기존 객체가 아닌 새로운 객체가 생성
a = np.array([[1, 2, 3], [4, 5, 6]])
b = a
a= a*2
print(a)
print(b)
"""
[[ 2  4  6]
 [ 8 10 12]]
[[1 2 3]
 [4 5 6]]
"""
  • *=, += 등의 복합 할당 연산자는 기존 객체를 수정
a = np.array([[1, 2, 3], [4, 5, 6]])
b = a
a*=2
print(a)
print(b)
"""
[[ 2  4  6]
 [ 8 10 12]]
[[ 2  4  6]
 [ 8 10 12]]
"""

Indexing and Slicing

  • 1d array는 list와 유사하게 indexing slicing 적용됨
  • 2d array에서의 indexing, slicing 시에는 각 axis를 comma로 구분하여 적용.
a = np.array([[1, 3, 5, 7], [2, 4, 6, 8], [3, 6, 9, 12]])

print(a)
print(a[1, 3])
print(a[0])
print(a[1:, ::2])
print(a[:, ::-1])
print(a[::2])

[[ 1  3  5  7]
 [ 2  4  6  8]
 [ 3  6  9 12]]
8
[1 3 5 7]
[[2 6]
 [3 9]]
[[ 7  5  3  1]
 [ 8  6  4  2]
 [12  9  6  3]]
[[ 1  3  5  7]
 [ 3  6  9 12]]

Boolean Indexing

  • bool type 값이 얻어지는 연산 역시 element wise 연산이 수행
  • Boolean array를 indexing에 활용 가능. 특정 조건에 부합하는 elements를 수정할 때 용이.

Boolean Arrays

  • numpy array를 이용한 조건문을 통해 Boolean array 생성 시 and, or, not 등을 적용할 수 없음. np.logical_or(), np.logical_and(), np.logical_not() 사용.
  • Boolean array는 indexing 이외에도 any(), all() 메서드를 통해 array 내의 elements를 체크할 수 있음.
C = np.array([[1, 2, 3], [4, 5, 6]])
print(C)
print(C > 2)
print(C[C > 2])
print(C % 3 != 0)
print(C[C % 3 != 0])
C[C % 2 == 0] = 0
print(C)

print(np.logical_or(C  == 0, C>3))
print((C==0).any())
print((C==0).all())
print((C >= 0).all())

[[1 2 3]
 [4 5 6]]
[[False False  True]
 [ True  True  True]]
[3 4 5 6]
[[ True  True False]
 [ True  True False]]
[1 2 4 5]
[[1 0 3]
 [0 5 0]]
[[False  True False]
 [ True  True  True]]
True
False
True

Shape Manipulation

reshape

  • Shape이 바뀐 새로운 array 반환
  • Elements 수가 동일하게 reshape 가능
  • Elements를 1차원으로 나열한 이후에 이를 다시 특정 shape로 가공하는 방식
  • 최대 한 축에 대해서 -1을 대입하여 임의의 shape을 갖고 있는 array를 reshape 할 수 있음
b = np.array([[1, 3, 5, 7], [2, 4, 6, 8], [3, 6, 9, 12]])
print(b.reshape(6, 2))
print(b.reshape(4, -1))
print(b.reshape(-1, 4))

"""
[[ 1  3]
 [ 5  7]
 [ 2  4]
 [ 6  8]
 [ 3  6]
 [ 9 12]]
[[ 1  3  5]
 [ 7  2  4]
 [ 6  8  3]
 [ 6  9 12]]
[[ 1  3  5  7]
 [ 2  4  6  8]
 [ 3  6  9 12]]
"""

transpose

b = np.array([[1, 3, 5, 7], [2, 4, 6, 8], [3, 6, 9, 12]])

print(b.transpose())
print(b.T)

"""
[[ 1  2  3]
 [ 3  4  6]
 [ 5  6  9]
 [ 7  8 12]]
[[ 1  2  3]s
 [ 3  4  6]
 [ 5  6  9]
 [ 7  8 12]]
"""

concatenate

  • 합치려는 두 array의 dimension 수는 같아야 함
  • 이어 붙이려는 axis를 제외한 나머지 axes에 대해서는 dimension의 크기가 같아야 함
a = np.array([1, 2])
b = np.array([3, 4])
print(np.concatenate((a, b)))
a2 = a.reshape((1, 2))
b2 = b.reshape((1, 2))
print(a2)
print(b2)

"""
[1 2 3 4]
[[1 2]]
[[3 4]]
"""
728x90
반응형
댓글
반응형
250x250
글 보관함
최근에 달린 댓글
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Total
Today
Yesterday
링크