跳到内容

full_like

返回一个与给定数组具有相同形状和类型的完整数组。

参数

名称 类型 描述 默认值
a array_like

结果的形状和数据类型将与 a 的形状和数据类型匹配。

必需
dtype 数据 - 类型

覆盖结果的数据类型。

None
format str

格式字符串。

None
compressed_axes iterable

如果返回 GCXS 数组,要压缩的轴。

必需

返回值

名称 类型 描述
out SparseArray

a 具有相同形状和类型的 fill_value 数组。

示例

>>> x = np.ones((2, 3), dtype="i8")
>>> full_like(x, 9.0).todense()
array([[9, 9, 9],
       [9, 9, 9]])
源代码位于 sparse/numba_backend/_common.py
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
@_check_device
def full_like(a, fill_value, dtype=None, shape=None, format=None, *, device=None, **kwargs):
    """Return a full array with the same shape and type as a given array.

    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 `fill_value` with the same shape and type as `a`.

    Examples
    --------
    >>> x = np.ones((2, 3), dtype="i8")
    >>> full_like(x, 9.0).todense()  # doctest: +NORMALIZE_WHITESPACE
    array([[9, 9, 9],
           [9, 9, 9]])
    """
    if format is None and not isinstance(a, np.ndarray):
        format = type(a).__name__.lower()
    elif format is None:
        format = "coo"

    compressed_axes = kwargs.pop("compressed_axes", None)
    if hasattr(a, "compressed_axes") and compressed_axes is None:
        compressed_axes = a.compressed_axes
    return full(
        a.shape if shape is None else shape,
        fill_value,
        dtype=(a.dtype if dtype is None else dtype),
        format=format,
        **kwargs,
    )