Python | Numpy matrix.sum ()

NumPy | Python Methods and Functions

Using the matrix.sum() method, we can find the sum of the values ​​in a matrix using the same method.

Syntax: matrix.sum ()
Return: Return sum of values ​​in a matrix

Example # 1:
In this example, we can find the sum of the values ​​in a matrix using matrix.sum () .

# import important module in python

import numpy as np

 
# make a matrix with NumPy

gfg = np.matrix ( `[4, 1; 12, 3]` )

 < / code> 
# using the matrix.sum () method

geek = gfg. sum ()

 

print ( geek)

Exit:

 20 

Example # 2:

# import important module in python

import numpy as np

  
# make a matrix with NumPy

gfg = np.matrix ( `[4, 1, 9; 12, 3, 1; 4, 5, 6] ` )

  
# using the matrix.sum () method

geek = gfg. sum (axis = 1 )

  

print (geek)

Exit:

 [[14] [16] [15]] 




Get Solution for free from DataCamp guru