NumPy : sum, mean, std, and so on

2021. 9. 17. 00:03·Programming/DIP
728x90
728x90

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

 

참고로, 영상 데이터에서는 pixel (or element)의 값이 NaN인 경우가 거의 없으나

다른 matrix나 tensor 데이터의 경우에는 NaN 인 원소를 가질 수 있음.

이 경우, NaN Safe Aggregation Functions를 사용하여 NaN은 무시하고 값을 구할 수 있음.

https://ds31x.tistory.com/223

 

[Tensor] NaN Safe Aggregation Functions

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

ds31x.tistory.com

 


numpy.sum : 총합을 구함.

Sum of array elements over a given axis.

numpy.sum(
	a, 
    axis=None, 
    dtype=None, 
    out=None, 
    keepdims=<no value>, 
    initial=<no value>, 
    where=<no value>
)

 

아래의 경우, 이미지의 전체 pixel intensity를 더한 값을 구함..

import numpy as np

a = np.arange(20).reshape(5,-1)

sum = np.sum(a)
print(sum)

이미지의 각 행(row)에서의 합은 다음과 같이 구할 수 있음.

import numpy as np

a = np.arange(20).reshape(5,-1)

row_sum_array = np.sum(a,axis=1)
print(a)
print(row_sum_array.shape)
print(row_sum_array)

결과는 다음과 같음

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]
 [16 17 18 19]]
(5,)
[ 6 22 38 54 70]
  • 이미지 a는 $5 \times 4$ 이므로, 각 행별로 합을 구하면 5개의 값이 나옴. (5,)

Ref. : NumPy API


numpy.mean : 평균 구하기.

Compute the arithmetic mean along the specified axis.

numpy.mean(a, axis=None, dtype=None, out=None, keepdims=<no value>, *, where=<no value>)

아래의 경우, 이미지의 전체에 대한 평균 pixel intensity를 반환함.

import numpy as np

a = np.arange(20).reshape(5,-1)

mean = np.mean(a)
print(mean)

이미지의 각 열(column)에서의 평균은 다음과 같이 구할 수 있음.

import numpy as np

a = np.arange(20).reshape(5,-1)

col_mean_array = np.mean(a,axis=0)
print(a)
print(col_mean_array.shape )
print(col_mean_array )

결과는 다음과 같음

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]
 [16 17 18 19]]
(4,)
[ 8.  9. 10. 11.]
  • 이미지 a는 $5 \times 4$ 이므로, 각 열별로 평균을 구하면 4개의 값이 나옴. (4,)

Ref. : NumPy API


numpy.std : 표준편차 구하기.

Compute the standard deviation along the specified axis.

numpy.std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=<no value>, *, where=<no value>)

아래의 경우, 이미지의 전체에 대한 표준편차를 구함.

import numpy as np

a = np.arange(20).reshape(5,-1)

std = np.std(a)
print(std)

이미지의 각 열(column)에서의 표준편차는 다음과 같이 구할 수 있음.

import numpy as np

a = np.arange(20).reshape(5,-1)

col_std_array = np.std(a,axis=0)
print(a)
print(col_std_array.shape )
print(col_std_array )

결과는 다음과 같음

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]
 [16 17 18 19]]
(4,)
[5.65685425 5.65685425 5.65685425 5.65685425]
  • 이미지 a는 $5 \times 4$ 이므로, 각 열별로 표준편차를 구하면 4개의 값이 나옴. (4,)

Ref. : NumPy API


기타 중요한 함수들

  • numpy.max : 특정 축을 따라 최대값을 구함.
  • numpy.min : 특정 축을 따라 최소값을 구함.
  • numpy.median : 특정 축을 따라 median을 구함.

같이 읽어보면 좋은 자료들

tensor 와 관련된 NumPy, PyTorch, Tensorflow에서의 다양한 vectorized operatoni을 소개.

https://ds31x.tistory.com/222

 

[Tensor] vectorized op. (or universal func)

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

ds31x.tistory.com

 

'Programming > DIP' 카테고리의 다른 글

PIL과 opencv에서의 image 변환.  (1) 2021.11.22
Conda backup for DIP 2021  (0) 2021.09.18
NumPy 검색  (0) 2021.09.11
NumPy 배열 나누기: 영상 자르기  (0) 2021.09.11
NumPy 배열 병합 : 영상 붙이기  (0) 2021.09.11
'Programming/DIP' 카테고리의 다른 글
  • PIL과 opencv에서의 image 변환.
  • Conda backup for DIP 2021
  • NumPy 검색
  • NumPy 배열 나누기: 영상 자르기
dsaint31x
dsaint31x
    반응형
    250x250
  • dsaint31x
    Dsaint31's blog
    dsaint31x
  • 전체
    오늘
    어제
    • 분류 전체보기 (739)
      • Private Life (13)
      • Programming (56)
        • DIP (104)
        • ML (26)
      • Computer (119)
        • CE (53)
        • ETC (33)
        • CUDA (3)
        • Blog, Markdown, Latex (4)
        • Linux (9)
      • ... (350)
        • Signals and Systems (103)
        • Math (171)
        • Linear Algebra (33)
        • Physics (42)
        • 인성세미나 (1)
      • 정리필요. (54)
        • 의료기기의 이해 (6)
        • PET, MRI and so on. (1)
        • PET Study 2009 (1)
        • 방사선 장해방호 (4)
        • 방사선 생물학 (3)
        • 방사선 계측 (9)
        • 기타 방사능관련 (3)
        • 고시 (9)
        • 정리 (18)
      • RI (0)
      • 원자력,방사능 관련법 (2)
  • 블로그 메뉴

    • Math
    • Programming
    • SS
    • DIP
  • 링크

    • Convex Optimization For All
  • 공지사항

    • Test
    • PET Study 2009
    • 기타 방사능관련.
  • 인기 글

  • 태그

    SIGNAL
    Vector
    signals_and_systems
    signal_and_system
    function
    numpy
    fourier transform
    Term
    Probability
    linear algebra
    Convolution
    SS
    Programming
    Python
    opencv
    Optimization
    검사
    인허가제도
    random
    math
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
dsaint31x
NumPy : sum, mean, std, and so on
상단으로

티스토리툴바