Python – Get Matrix Mean


When it is required to get the mean of the matrix elements, the ‘mean’ method from the ‘Numpy’ package is used after it has been imported into the environment.

Example

Below is a demonstration of the same −

import numpy as np

my_matrix = np.matrix('[24, 41; 35, 25]')
print("The matrix is : " )
print(my_matrix)

my_result = my_matrix.mean()

print("The result is : ")
print(my_result)

Output

The matrix is :
[[24 41]
[35 25]]
The result is :
31.25

Explanation

  • The required packages are imported into the environment.

  • A matrix is created using the Numpy package.

  • It is displayed on the console.

  • The mean of the matrix is determined using the ‘mean’ method.

  • This is assigned to a variable.

  • It is displayed as the output on the console.

Updated on: 13-Sep-2021

143 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements