PYTHON-numpy.mean

1. Definition:

numpy.mean (a, axis = None, dtype = None, out = None, keepdims = <no value> )
 # a: array (if it is not an array, turn it into an array) 
# axis: optional (unselected is the average of all value) 0 columns find the average value for a respective row find average 
# DTYPE data type, optionally, for calculating the average type. For integer input, the default is float64; for floating-point input, it is the same as the input dtype. 
# ndarray, optional, place an alternate output array of results. The default value is None; if provided, its shape must be the same as the expected output shape, but if necessary, the type will be cast. 
# Output: If out = None, a new array that contains the average returns, otherwise it returns a reference to the output of the array.

2. Examples:

2.1 Array:

>>> a = np.array([[1,2],[3,4]])
>>> a
array([[1, 2],
       [3, 4]])
>>> np.mean(a)
2.5
>>> np.mean(a,axis = 0)
array([2., 3.])
>>> np.shape(np.mean(a,axis = 0))
(2,)
>>> np.mean(a,axis = 1)
array([1.5, 3.5])
>>> np.shape(np.mean(a,axis = 1))
(2,)
>>> np.shape(a)
(2, 2)

  >>> type(np.mean(a,axis = 1))
  <class 'numpy.ndarray'>

For arrays, directly return 1 * n array (array)

2.2 Matrix:

>>> 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]])
>>> type(num2)
<class 'numpy.matrix'>
>>> num3 = np.asmatrix(num1)
>>> num3
matrix([[1, 2, 3],
        [2, 3, 4],
        [3, 4, 5],
        [4, 5, 6]])
>>> type(num3)
<class 'numpy.matrix'>
>>> np.mean(num2,axis = 0)
matrix([[2.5, 3.5, 4.5]])
>>> np.mean(num2,axis = 0)
matrix([[2.5, 3.5, 4.5]])
>>> np.mean(num2,axis = 1)
matrix([[2.],
        [3.],
        [4.],
        [5.]])
# Description: 
# MAT conversion matrix asmatrix == 
# matrix, then: 
# Axis = 0, the mean calculated column, n-return *. 1 
# Axis =. 1, calculating a traveling system, return m * 1

 

3. Reference code:

Official website: https://docs.scipy.org/doc/numpy/reference/generated/numpy.mean.html

np.mat():https://www.jb51.net/article/161915.htm

https://blog.csdn.net/yeler082/article/details/90342438

np.mean:https://blog.csdn.net/lilong117194/article/details/78397329

Guess you like

Origin www.cnblogs.com/xiao-yu-/p/12722217.html