Skip to content

ones

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

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 ones with the given shape and dtype.

Examples:

>>> ones(5).todense()
array([1., 1., 1., 1., 1.])
>>> ones((2, 2), dtype=int).todense()
array([[1, 1],
       [1, 1]])
Source code in sparse/numba_backend/_common.py
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
def ones(shape, dtype=float, format="coo", *, device=None, **kwargs):
    """Return a SparseArray of given shape and type, filled with ones.

    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 ones with the given shape and dtype.

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

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