NumPy Array Unary Operations

Author: Al-mamun Sarkar Date: 2020-03-30 20:55:53

NumPy Array Unary Operations. The following shows various unary operations such as sum, min, max, the cumulative sum of the NumPy array of Python NumPy module. 

 

Code:

import numpy as np

 

Code:

a = np.random.random((2,3))
a

Output:

array([[ 0.25043718,  0.22943833,  0.10952575],
       [ 0.58983798,  0.25721836,  0.67223833]])

 

Code:

a.sum()

Output:

2.1086959391357141

 

Code:

a.min()

Output:

0.10952575367126882

 

Code:

a.max()

Output:

0.67223833106793429

 

Changing the axis of unary operations:

Code:

b = np.arange(12).reshape(3,4)
b

Output:

array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

 

Code:

b.sum(axis=0)

Output:

array([12, 15, 18, 21])

 

Code:

b.sum(axis=1) 

Output:

array([ 6, 22, 38])

 

Code:

b.min(axis=1) 

Output:

array([0, 4, 8])

 

Cumulative sum along each row:

b.cumsum(axis=1)

Output:

array([[ 0,  1,  3,  6],
       [ 4,  9, 15, 22],
       [ 8, 17, 27, 38]])