scipy.stats.contingency.

margins#

scipy.stats.contingency.margins(a)[source]#

Return a list of the marginal sums of the array a.

Parameters:
andarray

The array for which to compute the marginal sums.

Returns:
margsumslist of ndarrays

A list of length a.ndim. margsums[k] is the result of summing a over all axes except k; it has the same number of dimensions as a, but the length of each axis except axis k will be 1.

Notes

Array API Standard Support

margins has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.

Library

CPU

GPU

NumPy

n/a

CuPy

n/a

PyTorch

JAX

Dask

n/a

See Support for the array API standard for more information.

Examples

>>> import numpy as np
>>> from scipy.stats.contingency import margins
>>> a = np.arange(12).reshape(2, 6)
>>> a
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11]])
>>> m0, m1 = margins(a)
>>> m0
array([[15],
       [51]])
>>> m1
array([[ 6,  8, 10, 12, 14, 16]])
>>> b = np.arange(24).reshape(2,3,4)
>>> m0, m1, m2 = margins(b)
>>> m0
array([[[ 66]],
       [[210]]])
>>> m1
array([[[ 60],
        [ 92],
        [124]]])
>>> m2
array([[[60, 66, 72, 78]]])