En este artículo, analicemos cómo encontrar la suma y el producto de arrays NumPy.
Suma de la array NumPy
La suma de los elementos de la array NumPy se puede lograr de las siguientes maneras
Método #1: Usar numpy.sum()
Sintaxis: numpy.sum(array_name, axis=Ninguno, dtype=Ninguno, out=Ninguno, keepdims=<sin valor>, initial=<sin valor>, where=<sin valor>)
Ejemplo:
Python3
# importing numpy import numpy as np def main(): # initialising array print('Initialised array') gfg = np.array([[1, 2, 3], [4, 5, 6]]) print(gfg) # sum along row print(np.sum(gfg, axis=1)) # sum along column print(np.sum(gfg, axis=0)) # sum of entire array print(np.sum(gfg)) # use of out # initialise a array with same dimensions # of expected output to use OUT parameter b = np.array([0]) # np.int32)#.shape = 1 print(np.sum(gfg, axis=1, out=b)) # the output is stored in b print(b) # use of keepdim print('with axis parameter') # output array's dimension is same as specified # by the axis print(np.sum(gfg, axis=0, keepdims=True)) # output consist of 3 columns print(np.sum(gfg, axis=1, keepdims=True)) # output consist of 2 rows print('without axis parameter') print(np.sum(gfg, keepdims=True)) # we added 100 to the actual result print('using initial parameter in sum function') print(np.sum(gfg, initial=100)) # False allowed to skip sum operation on column 1 and 2 # that's why output is 0 for them print('using where parameter ') print(np.sum(gfg, axis=0, where=[True, False, False])) if __name__ == "__main__": main()
Producción:
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]
Nota: el uso de numpy.sum en elementos de array que consisten en elementos Not a Number (NaNs) da un error. Para evitar esto, usamos numpy. nansum() los parámetros son similares a los primeros, excepto que este último no es compatible con where e initial.
Método #2: Usar numpy.cumsum()
Devuelve la suma acumulada de los elementos de la array dada.
Sintaxis: numpy.cumsum(array_name, axis=Ninguno, dtype=Ninguno, out=Ninguno)
Ejemplo:
Python3
# importing numpy import numpy as np def main(): # initialising array print('Initialised array') gfg = np.array([[1, 2, 3], [4, 5, 6]]) print('original array') print(gfg) # cumulative sum of the array print(np.cumsum(gfg)) # cumulative sum of the array along # axis 1 print(np.cumsum(gfg, axis=1)) # initialising a 2x3 shape array b = np.array([[None, None, None], [None, None, None]]) # finding cumsum and storing it in array np.cumsum(gfg, axis=1, out=b) # printing resultant array print(b) if __name__ == "__main__": main()
Producción:
Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]
Producto de la array NumPy
El producto de arrays NumPy se puede lograr de las siguientes maneras
Método #1: Usar numpy.prod()
Sintaxis: numpy. prod (array_name, axis=Ninguno, dtype=Ninguno, out=Ninguno, keepdims=<sin valor>, initial=<sin valor>, where=<sin valor>)
Ejemplo:
Python3
# importing numpy import numpy as np def main(): # initialising array print('Initialised array') gfg = np.array([[1, 2, 3], [4, 5, 6]]) print(gfg) # product along row print(np.prod(gfg, axis=1)) # product along column print(np.prod(gfg, axis=0)) # sum of entire array print(np.prod(gfg)) # use of out # initialise a array with same dimensions # of expected output to use OUT parameter b = np.array([0]) # np.int32)#.shape = 1 print(np.prod(gfg, axis=1, out=b)) # the output is stored in b print(b) # use of keepdim print('with axis parameter') # output array's dimension is same as specified # by the axis print(np.prod(gfg, axis=0, keepdims=True)) # output consist of 3 columns print(np.prod(gfg, axis=1, keepdims=True)) # output consist of 2 rows print('without axis parameter') print(np.prod(gfg, keepdims=True)) # we initialise product to a factor of 10 # instead of 1 print('using initial parameter in sum function') print(np.prod(gfg, initial=10)) # False allowed to skip sum operation on column 1 and 2 # that's why output is 1 which is default initial value print('using where parameter ') print(np.prod(gfg, axis=0, where=[True, False, False])) if __name__ == "__main__": main()
Producción:
Initialised array [[1 2 3] [4 5 6]] [ 6 120] [ 4 10 18] 720 [720] [720] with axis parameter [[ 4 10 18]] [[ 6] [120]] without axis parameter [[720]] using initial parameter in sum function 7200 using where parameter [4 1 1]
Método #2: Usar numpy.cumprod()
Devuelve un producto acumulativo de la array.
Sintaxis: numpy.cumsum(array_name, axis=Ninguno, dtype=Ninguno, out=Ninguno)axis = [entero, Opcional]
Python3
# importing numpy import numpy as np def main(): # initialising array print('Initialised array') gfg = np.array([[1, 2, 3], [4, 5, 6]]) print('original array') print(gfg) # cumulative product of the array print(np.cumprod(gfg)) # cumulative product of the array along # axis 1 print(np.cumprod(gfg, axis=1)) # initialising a 2x3 shape array b = np.array([[None, None, None], [None, None, None]]) # finding cumprod and storing it in array np.cumprod(gfg, axis=1, out=b) # printing resultant array print(b) if __name__ == "__main__": main()
Producción:
Initialised array original array [[1 2 3] [4 5 6]] [ 1 2 6 24 120 720] [[ 1 2 6] [ 4 20 120]] [[1 2 6] [4 20 120]]
Publicación traducida automáticamente
Artículo escrito por technikue20 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA