nlcpy.average

nlcpy.average(a, axis=None, weights=None, returned=False)

Computes the weighted average along the specified axis.

Parameters
aarray_like

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

axisNone or int, optional

Axis along which to average a. The default, axis=None, will average over all of the elements of the input array. If axis is negative it counts from the last to the first axis. tuple of axis not supported.

weightsarray_like, optional

An array of weights associated with the values in a. Each value in a contributes to the average according to its associated weight. The weights array can either be 1-D (in which case its length must be the 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 a weight equal to one.

returnedbool, 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 of elements over which the average is taken.

Returns
retval, [sum_of_weights]ndarray

Return the average along the specified axis. When returned is True, return a tuple with the average as the first element and the sum of the weights as the second element. sum_of_weights is of the same type as retval. The result dtype follows a general pattern. If weights is None, the result dtype will be that of a , or float64 if a is integral. Otherwise, if weights is not None and a is non-integral, the result type will be the type of lowest precision capable of representing values of both a and weights. If a happens to be integral, the previous rules still applies but the result dtype will at least be float64.

参考

mean

Computes the arithmetic mean along the specified axis.

制限事項

  • If axis is neither a scalar nor None : NotImplementedError occurs.

  • For complex numbers : NotImplementedError occurs.

Examples

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