Movatterモバイル変換


[0]ホーム

URL:


SciPy

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. Ifa is not an array, aconversion is attempted.

axis : None or int or tuple of ints, optional

Axis or axes along which to averagea. 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.

New in version 1.7.0.

If axis is a tuple of ints, averaging is performed on all of the axesspecified in the tuple instead of a single axis or all the axes asbefore.

weights : array_like, optional

An array of weights associated with the values ina. 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 ofa along the given axis) or of the same shape asa.Ifweights=None, then all data ina are assumed to have aweight equal to one.

returned : bool, optional

Default isFalse. IfTrue, the tuple (average,sum_of_weights)is returned, otherwise only the average is returned.Ifweights=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 isTrue,return a tuple with the average as the first element and the sumof the weights as the second element. The return type isFloatifa is of integer type, otherwise it is of the same type asa.sum_of_weights is of the same type asaverage.

Raises:

ZeroDivisionError

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

TypeError

When the length of 1Dweights is not the same as the shape ofaalong 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))>>>dataarray([[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.

Previous topic

numpy.median

Next topic

numpy.mean

  • © Copyright 2008-2009, The Scipy community.
  • Last updated on Jun 10, 2017.
  • Created usingSphinx 1.5.3.

[8]ページ先頭

©2009-2025 Movatter.jp