numpy.average的用法

本文详细介绍了NumPy库中的average函数,该函数用于计算指定轴上的加权平均值。文章解释了average函数的参数,包括输入数据数组、权重数组等,并通过实例展示了如何使用这些参数来计算不同情况下的平均值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

numpy.average

numpy. average ( a, axis=None, weights=None, returned=False ) [source]

Compute the weighted average along the specified axis.


Parameters:

 

a : array_like

Array containing data to be averaged. If a is not an array, aconversion is attempted.

axis : int, optional

Axis along which to average a. If None, averaging is done overthe flattened array.

weights : array_like, optional

An array of weights associated with the values in a. Each value ina contributes to the average according to its associated weight.The weights array can either be 1-D (in which case its length must bethe size of a along the given axis) or of the same shape as a.If weights=None, then all data in a are assumed to have aweight equal to one.

returned : bool, optional

Default is False. If True, the tuple (average, sum_of_weights)is returned, otherwise only the average is returned.If weights=None, sum_of_weights is equivalent to the number ofelements over which the average is taken.

Returns:

average, [sum_of_weights] : array_type or double

Return the average along the specified axis. When returned is True,return a tuple with the average as the first element and the sumof the weights as the second element. The return type is Floatif a is of integer type, otherwise it is of the same type as a.sum_of_weights is of the same type as average.

Raises:

ZeroDivisionError

When all weights along axis are zero. See numpy.ma.average for aversion robust to this type of error.

TypeError

When the length of 1D weights is not the same as the shape of aalong axis.

See also

mean

ma.average
average for masked arrays – useful if your data contains “missing” values

Examples

>>>
>>> data = range(1,5)
>>> data
[1, 2, 3, 4]
>>> np.average(data)
2.5
>>> np.average(range(1,11), weights=range(10,0,-1))
4.0
>>>
>>> data = np.arange(6).reshape((3,2))
>>> data
array([[0, 1],
       [2, 3],
       [4, 5]])
>>> np.average(data, axis=1, weights=[1./4, 3./4])
array([ 0.75,  2.75,  4.75])
>>> np.average(data, weights=[1./4, 3./4])
Traceback (most recent call last):
...
TypeError: Axis must be specified when shapes of a and weights differ.

From:https://docs.scipy.org/doc/numpy/reference/generated/numpy.average.html 
### 使用 `numpy.apply_along_axis` 的方法 `numpy.apply_along_axis` 是一种用于沿特定轴应用函数的方法。此函数接受一个多维数组作为输入,并沿着指定的轴逐个元素地调用给定的一元函数[^1]。 #### 参数解释 - **func1d**: 应用于一维切片上的函数。 - **axis**: 沿着该轴操作;如果 axis=0,则应用于每一列;如果是 axis=1,则应用于每一行。 - **arr**: 输入数组。 - **args**: 可选参数,传递给 func1d 函数。 - **kwargs**: 关键字参数,传递给 func1d 函数。 #### 示例代码展示 下面是一个简单的例子,展示了如何利用 `apply_along_axis` 对二维数组中的每行数据计算平均值: ```python import numpy as np def average(row): """ 计算并返回单行的均值 """ return row.mean() data = np.array([[1, 2], [3, 4]]) result = np.apply_along_axis(average, 1, data) print(result) # 输出应为 array([1.5, 3.5]) ``` 这段程序首先定义了一个名为 `average` 的辅助函数,它接收一个向量(即矩阵的一行),然后计算其平均数。接着通过调用 `np.apply_along_axis()` 将这个函数作用于整个二维数组 `data` 上面,在这里选择了按照行的方向来进行处理 (因此设置 `axis=1`)。最终得到的结果就是原数组各自行对应的平均数值组成的新的数组。 对于更复杂的场景,比如想要对三维甚至更高维度的数据集做同样的事情时,只需要调整好相应的 `axis` 参数即可让 `apply_along_axis` 正确工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值