COO.var(axis=None, dtype=None, out=None, ddof=0, keepdims=False)

Compute the variance along the given axes. Uses all axes by default.

  • axis (Union[int, Iterable[int]], optional) – The axes along which to compute the variance. Uses all axes by default.

  • dtype (numpy.dtype, optional) – The output datatype.

  • out (SparseArray, optional) – The array to write the output to.

  • ddof (int) – The degrees of freedom.

  • keepdims (bool, optional) – Whether or not to keep the dimensions of the original array.


The reduced output sparse array.

Return type:


See also


Equivalent numpy method.


  • This function internally calls COO.sum_duplicates to bring the array into canonical form.


You can use COO.var to compute the variance of an array across any dimension.

>>> from sparse import COO
>>> x = np.array([[1, 2, 0, 0],
...               [0, 1, 0, 0]], dtype='i8')
>>> s = COO.from_numpy(x)
>>> s2 = s.var(axis=1)
>>> s2.todense()  
array([0.6875, 0.1875])

You can also use the keepdims argument to keep the dimensions after the variance.

>>> s3 = s.var(axis=0, keepdims=True)
>>> s3.shape
(1, 4)

You can pass in an output datatype, if needed.

>>> s4 = s.var(axis=0, dtype=np.float16)
>>> s4.dtype

By default, this reduces the array down to one number, computing the variance along all axes.

>>> s.var()