Mathematical operators

The mathematical operators of NumPy can mainly support trigonometric operations, arithmetic operations, and exponential and logarithmic operations.

A class of these operators, such as prod, sum, and so on, perform computations within the array and serve to reduce the matrix. For example, the sum function calculates the sum along a given axis. The output will be the sum of elements along the axis. These functions can be called as a numpy.function or as an ndarray.method:

# Sum of all elements in an array
In [62]: np.array([[1, 2, 3], [4, 5, 6]]).sum()
Out[62]: 21

# Column sum of elements
In [63]: np.array([[1, 2, 3], [4, 5, 6]]).sum(axis = 0)
Out[63]: array([5, 7, 9])

# Cumulative sum of elements along axis 0
In [64]: np.array([[1, 2, 3], [4, 5, 6]]).cumsum(axis = 0)
Out[64]:
array([[1, 2, 3],
[5, 7, 9]], dtype=int32)

# Cumulative sum of all elements in the array
In [65]: np.array([[1, 2, 3], [4, 5, 6]]).cumsum()
Out[65]: array([ 1, 3, 6, 10, 15, 21], dtype=int32)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset