numpy.sum(arr, axis, dtype, out): Aquesta funció retorna la suma dels elements de la matriu sobre l'eix especificat.
Paràmetres:
arr: matriu d'entrada.
eix: eix al llarg del qual volem calcular el valor de la suma. En cas contrari, considerarà que arr està aplanat (funciona en tots els eixos). eix = 0 significa al llarg de la columna i eix = 1 significa treballar al llarg de la fila.
fora: Matriu diferent en què volem col·locar el resultat. La matriu ha de tenir les mateixes dimensions que la sortida esperada. El valor predeterminat és Cap.
inicial: [escalar, opcional] Valor inicial de la suma.
Tornada: Suma dels elements de la matriu (un valor escalar si l'eix no és cap) o matriu amb valors de suma al llarg de l'eix especificat.
Codi #1:
comanda estirament d'autocad
Python 3
tipus d'aprenentatge automàtic
# 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>)> |
>
>
Sortida:
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>
Codi #2:
Python 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>)> |
conversió int a cadena
>
valor java de la cadena
>
Sortida:
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>
Codi #3:
Python 3
arp una ordre
# 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>))> |
>
>
Sortida:
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]]>