python note mean of function to achieve the mean function code

  • 2021-07-09 08:59:00
  • OfStack

Usage: mean (matrix, axis=0) where matrix is a matrix and axis is a parameter

Take the m * n matrix as an example:

No value is set for axis. Average the number of m*n and return 1 real number

axis = 0: Compress the rows, average the columns, and return the 1* n matrix

axis = 1: Compressed columns, averaging rows, returning the m * 1 matrix

Examples:


>>> import numpy as np
 
>>> num1 = np.array([[1,2,3],[2,3,4],[3,4,5],[4,5,6]])
>>> now2 = np.mat(num1)
>>> now2
matrix([[1, 2, 3],
    [2, 3, 4],
    [3, 4, 5],
    [4, 5, 6]])
 
 
>>> np.mean(now2) #  Average all elements 
3.5
 
 
>>> np.mean(now2,0) #  Compress rows and average each column 
matrix([[ 2.5, 3.5, 4.5]])
 
 
>>> np.mean(now2,1) #  Compress columns and average each row 
matrix([[ 2.],
    [ 3.],
    [ 4.],
    [ 5.]])

Related articles: