logo

Python の numpy.sum()

numpy.sum(arr, axis, dtype, out) : この関数は、指定された軸上の配列要素の合計を返します。

パラメーター :
到着: 入力配列。
軸: 合計値を計算する軸。それ以外の場合、arr は平坦化されていると見なされます (すべての軸で動作します)。 axis = 0 は列に沿って作業することを意味し、axis = 1 は行に沿って作業することを意味します。
外 : 結果を配置する別の配列。配列は、予想される出力と同じ次元を持つ必要があります。デフォルトは「なし」です。
イニシャル : [スカラー、オプション] 合計の開始値。
戻る : 配列要素の合計 (軸が存在しない場合はスカラー値)、または指定された軸に沿った合計値を含む配列。



コード #1:

キツネとオオカミの違い

Python3








# 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:

Python3


tostringメソッドJava



# 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>)>

>

jsグローバル変数
>

出力:

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:

Python3




# 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]]>