coords.Coords.average#

Coords.average(wts=None, axis=None)#

Returns a (weighted) average of the Coords.

The average of a Coords is a Coords that is obtained by averaging the points along some or all axes. Weights can be specified to get a weighted average.

Parameters#

wts: float array_like, optional

Weight to be attributed to the points. If provided, and axis is an int, wts should be 1-dim with the same length as the specified axis. Else, it has a shape equal to self.shape or self.shape[:-1].

axis: int or tuple of ints, optional

If provided, the average is computed along the specified axis/axes only. Else, the average is taken over all the points, thus over all the axes of the array except the last.

Notes#

Averaging over the last (-1) axis does not make much sense.

Examples#

>>> X = Coords(np.arange(6).reshape(3,2,1))
>>> X
Coords([[[0., 0., 0.],
         [1., 0., 0.]],

        [[2., 0., 0.],
         [3., 0., 0.]],

        [[4., 0., 0.],
         [5., 0., 0.]]])
>>> print(X.average())
[2.5  0.   0. ]
>>> print(X.average(axis=0))
[[2. 0. 0.]
 [3. 0. 0.]]
>>> print(X.average(axis=1))
[[0.5  0.   0. ]
 [2.5  0.   0. ]
 [4.5  0.   0. ]]
>>> print(X.average(wts=[0.5,0.25,0.25],axis=0))
[[1.5  0.   0. ]
 [2.5  0.   0. ]]
>>> print(X.average(wts=[3,1],axis=1))
[[0.25  0.    0.  ]
 [2.25  0.    0.  ]
 [4.25  0.    0.  ]]
>>> print(X.average(wts=[[3,1],[3,1],[3,1]]))
[2.25  0.    0.  ]