ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 파이썬 데이터사이언스 핸드북 2 장 - 배열 연산: 브로드캐스팅
    데이터 분석/NumPy 2020. 5. 31. 09:01

     

    NumPy 는 Vectorization 벡터화된 연산으로 2가지 방식을 제공한다. 

    - (Python 내장함수보다 빠른) NumPy 의 유니버셜 함수 Ufuncs 사용  [Ufuncs 포스트]

    - (피연산자를 동일한 형태로 맞추는) NumPy 의 브로드캐스팅 Broadcasting 사용

     


    브로드캐스팅 소개

     

    같은 크기의 배열에서 이항 연산은 배열의 요소 단위로 수행된다.

    다른 크기의 배열에서 이항 연산은 브로드캐스팅 하여 같은 크기의 배열로 맞춘 후, 배열의 요소 단위로 연산한다. 

     

    브로드캐스팅 규칙

    1. 두 배열의 차원 수가 다르면, 더 작은 수의 차원을 가진 배열 형상의 앞쪽(왼쪽) 을 1로 채운다.

    2. 두 배열의 형상이 어떤 차원에서도 일치하지 않는다면, 해당 차원의 형상이 1인 배열이 다른 형상과 일치하도록 늘어난다.

    3. 임의의 차원에서 크기가 일치하지 않고 1도 아니라면 오류가 발생한다. 

     

    이항연산 - 두 피연산자에 대한 브로드캐스팅

     

     

     

    실전 브로드캐스팅

    브로드캐스팅 연산은 이 책 전체에서 보게 될 수많은 예제의 핵심이다. ufunc 을 사용하면 NumPy 사용자가 느린 파이썬 루프를 명시적으로 작성하지 않아도 된다!  브로드캐스팅의 실제 쓰임새를 알아본다. 

     

    배열을 중앙 정렬하기

    데이터 배열을 중앙정렬하는 것이다. X_centered

     

    평균, 오차(평균-관측치), 오차의 평균

    오차의 평균은 0 에 수렴할 수록 정밀하다. 기계 정밀도 내에서 평균값이 0 이다. 

     

    e 는 과학적 표기법으로, 매우 큰 수나 매우 작은 수를 표기할 때 사용된다.  [설명 링크]

    -1.11022302e-17 = -1.11022302 * (10^(-17))   /    4.5e6 = 4.5 * (10^6)

     

     

    2차원 함수 플로팅하기 plotting

    브로드캐스팅은 2차원 함수를 기반으로 이미지를 그릴 때에도 유용하다.

    x: 0 이상 5 이하에서 50 개의 숫자를 나열한다.

    y: x 의숫 자를 축만 다르게한다. 

    x+y : 0이상 10 이하의 숫자에 해당하는 색을 칠한다. (!)
    아직 plt.imshow 에 대한 것을 잘 모르지만, x축과 y축이 대각선 우상향으로 색이 그라디언트 된 것을 볼 수 있다. 

     

     


    Vectorization 

    Vectorization 은 한번에 값 세트에 대해서 동시에 연산을 수행하는 것이다. 데이터를 그룹핑하고 핸들링하는데에 최적화하는 방식이다. 

    Vectorization, Array Programming, Vector Process 에 대하여 더 자세한 정보는 더보기 클릭

    더보기

    Vectorization at Computing 에는 다음 3가지 개념이 소개되고 있다. [Wikipedia - Vectorization]

    • Array programming, a style of computer programming where operations are applied to whole arrays instead of individual elements
    • Automatic vectorization, a compiler optimization that transforms loops to vector operations
    • Image tracing, the creation of vector from raster graphics

     

    여기서 우리 맥락에 가장 근접한 Array Programing [Wikipedia - Array programming]

    In computer science, array programming refers to solutions which allow the application of operations to an entire set of values at once. Such solutions are commonly used in scientific and engineering settings. (...) In these languages, an operation that operates on entire arrays can be called a vectorized operation,[1] regardless of whether it is executed on a vector processor (which implements vector instructions) or not. 

    한번에 값 세트에 대해서 연산을 수행한다.  (...) 전체 배열 값에 대해서 벡터화된 연산을 수행하는데, 벡터 프로세스인지 여부와는 다른 개념이다.

    The fundamental idea behind array programming is that operations apply at once to an entire set of values. (...) The basis behind array programming and thinking is to find and exploit the properties of data where individual elements are similar or adjacent. Unlike object orientation which implicitly breaks down data to its constituent parts (or scalar quantities), array orientation looks to group data and apply a uniform handling.

    Array programming 의 기본 개념은 전체 값 세트에 한번에 연산을 적용한다는 점이다. (...) 각 데이터 요소가 유사하거나 메모리적으로 인접한 특성을 발견해내고 활용한다. 객체 지향은 데이터를 (객체의) 구성성분으로 나누어 놓는 것에 반해, 배열 지향은 데이터를 그룹핑하고, 일제히 핸들링 (연산 및 처리)를 한다는 점이다. 

     

    그렇다면 Vector Process  는 무엇인지? [Wikipedia - Vector process]

    In computing, a vector processor or array processor is a central processing unit (CPU) that implements an instruction set containing instructions that operate on one-dimensional arrays of data called vectors, compared to the scalar processors, whose instructions operate on single data items. Vector processors can greatly improve performance on certain workloads, notably numerical simulation and similar tasks.

    Vector Processor 는 CPU 가 1차원 배열 데이터 (벡터) 에 대해서 연산을 수행하는 것이다. (하나의 데이터 요소에 대해서 연산을 수행하는 scalar processor 와 반대되는 개념이다.) Vector Processor 는 특히 산술 연산이나 유사한 작업에 대해서 성능을 크게 개선시킬 수 있다. (배열에 대해서 반복 loop 처리를 한다는 Automatic vectorization 과 맞닿아있는듯!) 

    댓글

Designed by Tistory.