Skip to content

ones_like

Return a SparseArray of ones with the same shape and type as a.

Parameters:

Name Type Description Default
a array_like

The shape and data-type of the result will match those of a.

required
dtype data - type

Overrides the data type of the result.

None
format str

A format string.

None
compressed_axes iterable

The axes to compress if returning a GCXS array.

required

Returns:

Name Type Description
out SparseArray

Array of ones with the same shape and type as a.

Examples:

>>> x = np.ones((2, 3), dtype="i8")
>>> ones_like(x).todense()
array([[1, 1, 1],
       [1, 1, 1]])
Source code in sparse/numba_backend/_common.py
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
def ones_like(a, dtype=None, shape=None, format=None, *, device=None, **kwargs):
    """Return a SparseArray of ones with the same shape and type as ``a``.

    Parameters
    ----------
    a : array_like
        The shape and data-type of the result will match those of `a`.
    dtype : data-type, optional
        Overrides the data type of the result.
    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 same shape and type as `a`.

    Examples
    --------
    >>> x = np.ones((2, 3), dtype="i8")
    >>> ones_like(x).todense()  # doctest: +NORMALIZE_WHITESPACE
    array([[1, 1, 1],
           [1, 1, 1]])
    """
    return full_like(a, fill_value=1, dtype=dtype, shape=shape, format=format, device=device, **kwargs)