The mean numpy () function

 

Mean () function is defined: 
numpy.mean (A, Axis, DTYPE, OUT, keepdims)

Mean () Function: Mean obtaining 
parameters as regular operation axis, to m * n matrix Example:

  • axis is not the set value of m * n averaging number, returns a real number
  • axis = 0: compressed row , each column averaging Returns 1 * n matrix
  • axis = 1: compress columns , each row averaging, m * 1 matrix Returns

Examples: 
1. The operation of the array:

>>> a = np.array([[1, 2], [3, 4]])
>>> a
array([[1, 2],
       [3, 4]])
>>> np.mean(a)
2.5
>>> np.mean(a, axis=0) # axis=0,计算每一列的均值 array([ 2., 3.]) >>> np.mean(a, axis=1) # 计算每一行的均值 array([ 1.5, 3.5]) >>> 

 

2. Matrix operations

>>> import numpy as np
>>> num1 = np.array([[1,2,3],[2,3,4],[3,4,5],[4,5,6]])
>>> num1
array([[1, 2, 3],
       [2, 3, 4],
       [3, 4, 5],
       [4, 5, 6]])
>>> num2 = np.mat(num1)
>>> num2
matrix([[1, 2, 3],
        [2, 3, 4],
        [3, 4, 5],
        [4, 5, 6]])
>>> np.mean(num2) # 对所有元素求均值
3.5
>>> np.mean(num2,0) # 压缩行,对各列求均值 matrix([[ 2.5, 3.5, 4.5]]) >>> np.mean(num2,1) # 压缩列,对各行求均值 matrix([[ 2.], [ 3.], [ 4.], [ 5.]]) >>> 

Guess you like

Origin www.cnblogs.com/liuys635/p/11235920.html