Python의 numpy.sum()

numpy.sum(arr, 축, dtype, out) : 이 함수는 지정된 축에 대한 배열 요소의 합계를 반환합니다.

매개변수:
도착: 입력 배열.
축 : 합계 값을 계산하려는 축입니다. 그렇지 않으면 arr이 평면화되는 것으로 간주됩니다(모든 축에서 작동함). axis = 0은 열을 따라 작업한다는 의미이고 axis = 1은 행을 따라 작업한다는 의미입니다.
밖으로 : 결과를 배치하려는 다른 배열입니다. 배열은 예상 출력과 동일한 차원을 가져야 합니다. 기본값은 없음입니다.
초기의 : [스칼라, 선택 사항] 합계의 시작 값입니다.
반품 : 배열 요소의 합계(축이 없는 경우 스칼라 값) 또는 지정된 축을 따라 합계 값이 있는 배열입니다.

코드 #1:

파이썬3




# Python Program illustrating> # numpy.sum() method> import> numpy as np> > # 1D array> arr> => [> 20> ,> 2> , .> 2> ,> 10> ,> 4> ]> > print> (' Sum of arr : ', np.> sum> (arr))> > print> ('> Sum> of arr(uint8) : ', np.> sum> (arr, dtype> => np.uint8))> print> ('> Sum> of arr(float32) : ', np.> sum> (arr, dtype> => np.float32))> > print> (' Is np.> sum> (arr).dtype> => => np.uint : ',> > np.> sum> (arr).dtype> => => np.uint)> print> ('Is np.> sum> (arr).dtype> => => np.> float> : ',> > np.> sum> (arr).dtype> => => np.> float> )>

산출:

Sum of arr : 36.2 Sum of arr(uint8) : 36 Sum of arr(float32) : 36.2 Is np.sum(arr).dtype == np.uint : False Is np.sum(arr).dtype == np.float : True 

코드 #2:

파이썬3




# Python Program illustrating> # numpy.sum() method> import> numpy as np> > # 2D array> arr> => [[> 14> ,> 17> ,> 12> ,> 33> ,> 44> ],> > [> 15> ,> 6> ,> 27> ,> 8> ,> 19> ],> > [> 23> ,> 2> ,> 54> ,> 1> ,> 4> ,]]> > print> (' Sum of arr : ', np.> sum> (arr))> > print> ('> Sum> of arr(uint8) : ', np.> sum> (arr, dtype> => np.uint8))> print> ('> Sum> of arr(float32) : ', np.> sum> (arr, dtype> => np.float32))> > print> (' Is np.> sum> (arr).dtype> => => np.uint : ',> > np.> sum> (arr).dtype> => => np.uint)> print> ('Is np.> sum> (arr).dtype> => => np.> float> : ',> > np.> sum> (arr).dtype> => => np.> float> )>

산출:

Sum of arr : 279 Sum of arr(uint8) : 23 Sum of arr(float32) : 279.0 Is np.sum(arr).dtype == np.uint : False Is np.sum(arr).dtype == np.float : False 

코드 #3:

파이썬3




# Python Program illustrating> # numpy.sum() method> > import> numpy as np> > # 2D array> arr> => [[> 14> ,> 17> ,> 12> ,> 33> ,> 44> ],> > [> 15> ,> 6> ,> 27> ,> 8> ,> 19> ],> > [> 23> ,> 2> ,> 54> ,> 1> ,> 4> ,]]> > print> (' Sum of arr : ', np.> sum> (arr))> print> ('> Sum> of arr(axis> => 0> ) : ', np.> sum> (arr, axis> => 0> ))> print> ('> Sum> of arr(axis> => 1> ) : ', np.> sum> (arr, axis> => 1> ))> print> (' Sum of arr (keepdimension> is> True> ): ',> > np.> sum> (arr, axis> => 1> , keepdims> => True> ))>

산출:

Sum of arr : 279 Sum of arr(axis = 0) : [52 25 93 42 67] Sum of arr(axis = 1) : [120 75 84] Sum of arr (keepdimension is True): [[120] [ 75] [ 84]]