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 轴沿着列的方向水平延伸。

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值