Skip to content

zeros

Return a SparseArray of given shape and type, filled with zeros.

Parameters:

Name Type Description Default
shape int or tuple of ints

Shape of the new array, e.g., (2, 3) or 2.

required
dtype data - type

The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64.

float
format str

A format string.

'coo'
compressed_axes iterable

The axes to compress if returning a GCXS array.

required

Returns:

Name Type Description
out SparseArray

Array of zeros with the given shape and dtype.

Examples:

>>> zeros(5).todense()
array([0., 0., 0., 0., 0.])
>>> zeros((2, 2), dtype=int).todense()
array([[0, 0],
       [0, 0]])
Source code in sparse/numba_backend/_common.py
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
def zeros(shape, dtype=float, format="coo", *, device=None, **kwargs):
    """Return a SparseArray of given shape and type, filled with zeros.

    Parameters
    ----------
    shape : int or tuple of ints
        Shape of the new array, e.g., ``(2, 3)`` or ``2``.
    dtype : data-type, optional
        The desired data-type for the array, e.g., `numpy.int8`.  Default is
        `numpy.float64`.
    format : str, optional
        A format string.
    compressed_axes : iterable, optional
        The axes to compress if returning a GCXS array.

    Returns
    -------
    out : SparseArray
        Array of zeros with the given shape and dtype.

    Examples
    --------
    >>> zeros(5).todense()  # doctest: +SKIP
    array([0., 0., 0., 0., 0.])

    >>> zeros((2, 2), dtype=int).todense()  # doctest: +NORMALIZE_WHITESPACE
    array([[0, 0],
           [0, 0]])
    """
    return full(shape, fill_value=0, dtype=np.dtype(dtype), format=format, device=device, **kwargs)