본문 바로가기
Python

[Summary] NumPy(Numerical Python)

by ds31x 2024. 9. 12.
파이썬 생태계에서 과학적 계산의 기본이 되는 라이브러리

 

NumPy는 파이썬에서 과학 계산수치 연산을 효율적으로 처리하기 위한 라이브러리 

  • n-dimensional array(다차원 배열)인 ndarray 객체를 중심으로 고성능 수치 계산을 지원
  • 벡터화 연산을 통해 루프 없이 대량의 데이터를 빠르게 처리할 수 있음.
  • 또한, 선형대수, 통계 등 다양한 수학적 기능을 제공하여 데이터 분석, 머신러닝, 시뮬레이션 등에서 필수적인 도구로 사용됨.
    • openCV, Sickit-image에서도 기본 데이터 타입으로 NumPy의 ndarray를 사용함.

https://numpy.org/

 

NumPy -

Use the interactive shell to try NumPy in the browser

numpy.org

https://dsaint31.tistory.com/456

 

[Programming] Primitive Data Type : C, C++, NumPy, Torch

Primitive Data Type이(Unboxed type)란?C, C++, NumPy, Torch, TensorFlow 등에서 사용되는 numeric data type들은보통 unboxed type이라고도 불리는 primitive data type들이다.unboxed type에서는할당된 메모리 bit들이 해당 numeric

dsaint31.tistory.com

 


기본 사용법

2024.09.09 - [Python] - [NumPy] 생성 및 초기화, 기본 조작 (1)

 

[NumPy] 생성 및 초기화, 기본 조작 (1)

1. ndarray 생성하기 (=tensor생성하기)np.array ( seq [,dtype])list 나 tuple 등의 sequence 객체로부터 ndarray 생성.dtype : data type of element.float64 : default type in the numpy. *uint8 : unsigned int (8bit), the most commonly used for im

ds31x.tistory.com


Indexing

NumPy에서 indexing은 4가지 방식을 따름.

  • scalar를 이용한 indexing ( simple indexing ) : array[0]
  • slicing
  • boolean mask : array[array > 1]
  • fancy indexing : vectorized indexing. index들을 element로 가지는 array를 넘겨줌.
  • combined indexing : 앞서 4가지가 조합된 indexing

simple indexing, slicing, fancy indexing, boolean mask

2024.03.18 - [Python] - [DL] Tensor: Indexing <Simple, Slicing, Fancy, Boolean Mask>

 

[DL] Tensor: Indexing <Simple, Slicing, Fancy, Boolean Mask>

numpy나 pytorch, tensorflow의 텐서들도 파이썬의 list 또는 tubple 에서의 indexing과 slicing이 거의 그대로 사용됨. 2023.07.12 - [Python] - [Python] list (sequence type) : summary [Python] list (sequence type) : summary list는 ordere

ds31x.tistory.com

조건에 의한 indexing (Simple version)

2024.03.19 - [Python] - [ML] where: numpy 의 idx찾기

 

[ML] where: numpy 의 idx찾기

numpy에서 ndarray 인스턴스에서 특정 조건을 만족하는 elements의 위치(index, idx)를 찾는 기능을 numpy 모듈의 where 함수가 제공해줌. 기본적으로 numpy에서 index를 나타내는 방식은 각각의 축마다 해당 축

ds31x.tistory.com

조건에 의한 indexing

https://dsaint31.tistory.com/212

 

NumPy 검색

np.wherenumpy.where(condition[, x, y]) :Return elements chosen from x or y depending on condition.condition : 검색에 사용될 조건.x : condition이 True에 해당하는 위치에 지정되는 값 또는 array (검색에 사용된 ndarray로 broadcast

dsaint31.tistory.com

* np.argwhere 자료 추가할 것.

 

최대, 최소 값과 index구하기

2024.03.28 - [Python] - [DL] Tensor에서 maximum, minimum 찾기

 

[DL] Tensor에서 maximum, minimum 찾기

NumPy 의 ndarray에서 np.max 와 np.min 함수를 이용하여 최대, 최소인 값을 구함. 특정 축을 axis parameter로 지정하여 구할 수 있음 (결과는 해당 axis가 1이 됨.) np.argmax 와 na.argmin 함수를 이용하여 최대, 최

ds31x.tistory.com


Sorting

https://dsaint31.tistory.com/344

 

[NumPy] sorting: 정렬

Numpy에서 지원하는 sorting method(or function)은Numpy의 특성상 같은 데이터타입의 array이므로,Python에서 제공하는 built-in function들보다 효율성이 높음.copy based sorting컴퓨팅 자원이 충분하다면, 개인적으

dsaint31.tistory.com

https://dsaint31.tistory.com/474

 

[NumPy] searchsorted

np.searchsortedsort가 된 기존의 ndarray A 에 대해,입력으로 주어지는 ndarray B 의 element들의 값을 보고기존의 ndarray A 의 어느 index에 놓이게 될지를 반환한다.반환되는 ndarray의 shape는 query에 해당하는 B

dsaint31.tistory.com

 


축의 순서 변경 및 matrix 나누고 합치기

2024.03.16 - [Python] - [DL] Tensor: Transpose and Permute

 

[DL] Tensor: Transpose and Permute

Transpose Transpose(전치)의 linear algebra에서의 정의는 다음과 같음. Given an m x n matrix A, the transpose of A is the n x m matrix, denoted by AT whose columns are formed from the corresponding rows of A. 간단하게 말하면 행과 열

ds31x.tistory.com

https://dsaint31.tistory.com/208

 

NumPy 배열 병합 : 영상 붙이기

1. numpy.vstack numpy.vstack(tup) : Stack arrays in sequence vertically (rowwise) axis 0로 ndarray들을 붙임 2d image라면 위아래로 붙여지게 됨. Simple example import numpy as np a = np.ones((4,3)) b = np.zeros((4,3)) np.vstack( (a,b) ) Image

dsaint31.tistory.com

https://dsaint31.tistory.com/211

 

NumPy 배열 나누기: 영상 자르기

1. numpy.vsplit numpy.vsplit(array, indices_or_sections) : Split an array into multiple sub-arrays vertically (row-wise) axis 0 (행)로 array를 잘라 나눔. array가 image라면 수직으로 잘려진다. Simple example import numpy as np a = np.arange

dsaint31.tistory.com

 


Vectroized Op. and Aggregating

2024.03.19 - [Python] - [Tensor] vectorized op. (or universal func)

 

[Tensor] vectorized op. (or universal func)

Numpy에서 제공하는 ufunc. (Universal Functions)은 homogeneous and contiguous tensor 에서 모든 elements에 같은 연산을 적용하여 기존의 반복 loop에 기반한 연산에 비해 압도적인 속도를 보이면서 간편한 연산자

ds31x.tistory.com

 

https://dsaint31.tistory.com/216

 

NumPy : sum, mean, std, and so on

영상을 처리할 때, 영상의 각 pixel intensity에 대해 다양한 통계처리가 필요함. NumPy는 자체적으로 다양한 통계처리 함수들 (집계함수, 또는 Aggregation Function 이라고 불림) 을 제공함. 참고로, 영상

dsaint31.tistory.com

 

2024.03.20 - [Python] - [Tensor] NaN Safe Aggregation Functions

 

[Tensor] NaN Safe Aggregation Functions

NaN (Not a Number) 값을 포함하는 Tensor 인스턴스에서 Aggregation Function을 사용할 때, NaN을 무시 또는 특정값으로 처리하는 기능을 제공하는 함수. NumPy 기존의 aggregaton function의 이름에 nan을 앞에 붙인

ds31x.tistory.com


Broadcasting

https://dsaint31.tistory.com/359

 

[NumPy] Broadcasting

ndarray와 scalar를 연산시킬때, scalar를 상대 ndarray와 같은 shape이면서 해당 scalar의 값을 가진 ndarray로 변경시키고나서 이 scalar로부터 만들어진 ndarray와 상대 ndarray를 동작시키는 방식으로 elementwise

dsaint31.tistory.com


기타

2024.03.15 - [Python] - [DL] Tensor 간의 변환: NumPy, PyTorch, TensorFlow

 

[DL] Tensor 간의 변환: NumPy, PyTorch, TensorFlow

pytorch와 numpy의 경우, 텐서를 추상화하고 있는 tensor와 ndarray 를 제공하며, 이 둘은 zero-copy interoperability를 가진다고 말할 수 있을 정도로 상호호환이 가능하다. TensorFlow도 numpy와 상당히 연관되어

ds31x.tistory.com

 

728x90