numpy.mean - axis=0 - axis=1

numpy.mean - axis=0 - axis=1

numpy.mean(a, axis=None, dtype=None, out=None, keepdims=<no value>)

算術平均值是沿軸的元素的總和除以元素的數量。

Compute the arithmetic mean along the specified axis.
計算指定軸上的算術平均值。

Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis. float64 intermediate and return values are used for integer inputs.
返回數組元素的平均值。默認情況下,平均值取自展平的數組,否則取自指定的軸。float64 中間值和返回值用於整數輸入。

1. Parameters

a : array_like
Array containing numbers whose mean is desired. If a is not an array, a conversion is attempted.
如果 a 不是數組,則嘗試進行轉換。

axis : None or int or tuple of ints, optional
Axis or axes along which the means are computed. The default is to compute the mean of the flattened array.
計算平均值所依據的一個或多個軸。默認值是計算展平數組的平均值。

If this is a tuple of ints, a mean is performed over multiple axes, instead of a single axis or all the axes as before.
如果這是一個整數元組,那麼將在多個軸上執行均值,而不是像以前那樣在單個軸或所有軸上執行均值。

dtype : data-type, optional
Type to use in computing the mean. For integer inputs, the default is float64; for floating point inputs, it is the same as the input dtype.
用於計算平均值的類型。對於整數輸入,默認值爲 float64,對於浮點輸入,它與輸入 dtype 相同。

out : ndarray, optional
Alternate output array in which to place the result. The default is None; if provided, it must have the same shape as the expected output, but the type will be cast if necessary.
放置結果的備用輸出數組。默認值爲 None。如果提供的話,它的形狀必須與預期的輸出形狀相同,但是如果需要的話,將強制轉換類型。

keepdims : bool, optional
If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
如果將其設置爲 True,則縮小的軸將保留爲維度 1。使用此選項,結果將針對輸入數組正確廣播。

If the default value is passed, then keepdims will not be passed through to the mean method of sub-classes of ndarray, however any non-default value will be. If the sub-class’ method does not implement keepdims any exceptions will be raised.
如果傳遞了默認值,則 keepdims 不會傳遞給 ndarray 的子類的 mean 方法,但是任何非默認值都會傳遞。如果子類的方法未實現 keepdims,則將引發任何異常。

2. Returns

m : ndarray, see dtype parameter above
If out=None, returns a new array containing the mean values, otherwise a reference to the output array is returned.
如果 out=None,則返回一個包含平均值的新數組,否則返回對輸出數組的引用。

3. example

(pt-1.4_py-3.6) yongqiang@yongqiang:~/pytorch_work$ python
Python 3.6.10 |Anaconda, Inc.| (default, May  8 2020, 02:54:21)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>>
>>> data = np.array([[1,2],[3,4],[5,6]])
>>> data
array([[1, 2],
       [3, 4],
       [5, 6]])
>>> data.shape
(3, 2)
>>>
>>> np.mean(data)
3.5
>>>
>>> np.mean(data, axis=0) # 沿軸 0 調用 mean() 函數
array([3., 4.])
>>>
>>> np.mean(data, axis=1) # 沿軸 1 調用 mean() 函數
array([1.5, 3.5, 5.5])
>>>
>>> exit()
(pt-1.4_py-3.6) yongqiang@yongqiang:~/pytorch_work$
(pt-1.4_py-3.6) yongqiang@yongqiang:~/pytorch_work$ python
Python 3.6.10 |Anaconda, Inc.| (default, May  8 2020, 02:54:21)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>>
>>> data = np.array([[1,2,3],[3,4,5],[4,5,6]])
>>> data
array([[1, 2, 3],
       [3, 4, 5],
       [4, 5, 6]])
>>> data.shape
(3, 3)
>>>
>>> np.mean(data)
3.6666666666666665
>>>
>>> np.mean(data, axis=0)
array([2.66666667, 3.66666667, 4.66666667])
>>>
>>> np.mean(data, axis=1)
array([2., 4., 5.])
>>>
>>> exit()
(pt-1.4_py-3.6) yongqiang@yongqiang:~/pytorch_work$

在單精度中,均值可能不準確。用 np.float64 來計算均值更加準確。

(pt-1.4_py-3.6) yongqiang@yongqiang:~/pytorch_work$ python
Python 3.6.10 |Anaconda, Inc.| (default, May  8 2020, 02:54:21)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>>
>>> data = np.zeros((2, 512*512), dtype = np.float32)
>>> data.shape
(2, 262144)
>>>
>>> data[0, :] = 1.0
>>> data[1, :] = 0.1
>>> np.mean(data)
0.54999924
>>>
>>> np.mean(data, dtype = np.float64)
0.5500000007450581
>>>
>>> exit()
(pt-1.4_py-3.6) yongqiang@yongqiang:~/pytorch_work$

4. axis=0 - axis=1

二維矩陣,
axis=0 返回縱軸的平均值,按行方向計算,壓縮行,跨行,求各列的平均值。
axis=1 返回橫軸的平均值,按列方向計算,壓縮列,跨列,求各行的平均值。
axis 不設置值,對全部元素求平均值,返回一個實數。

軸用來爲超過一維的數組定義的屬性,二維數據擁有兩個軸:第 0 軸沿着行的垂直往下,第 1 軸沿着列的方向水平延伸。

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章