美文网首页
tile函数的使用(python、numpy)

tile函数的使用(python、numpy)

作者: 张潇_64df | 来源:发表于2018-01-09 10:18 被阅读0次

python的英文解释和实现源码如下:

def tile(A, reps):
    """
    Construct an array by repeating A the number of times given by reps.

    If `reps` has length ``d``, the result will have dimension of
    ``max(d, A.ndim)``.

    If ``A.ndim < d``, `A` is promoted to be d-dimensional by prepending new
    axes. So a shape (3,) array is promoted to (1, 3) for 2-D replication,
    or shape (1, 1, 3) for 3-D replication. If this is not the desired
    behavior, promote `A` to d-dimensions manually before calling this
    function.

    If ``A.ndim > d``, `reps` is promoted to `A`.ndim by pre-pending 1's to it.
    Thus for an `A` of shape (2, 3, 4, 5), a `reps` of (2, 2) is treated as
    (1, 1, 2, 2).

    Note : Although tile may be used for broadcasting, it is strongly
    recommended to use numpy's broadcasting operations and functions.

    Parameters
    ----------
    A : array_like
        The input array.
    reps : array_like
        The number of repetitions of `A` along each axis.

    Returns
    -------
    c : ndarray
        The tiled output array.

    See Also
    --------
    repeat : Repeat elements of an array.
    broadcast_to : Broadcast an array to a new shape

    Examples
    --------
    >>> a = np.array([0, 1, 2])
    >>> np.tile(a, 2)
    array([0, 1, 2, 0, 1, 2])
    >>> np.tile(a, (2, 2))
    array([[0, 1, 2, 0, 1, 2],
           [0, 1, 2, 0, 1, 2]])
    >>> np.tile(a, (2, 1, 2))
    array([[[0, 1, 2, 0, 1, 2]],
           [[0, 1, 2, 0, 1, 2]]])

    >>> b = np.array([[1, 2], [3, 4]])
    >>> np.tile(b, 2)
    array([[1, 2, 1, 2],
           [3, 4, 3, 4]])
    >>> np.tile(b, (2, 1))
    array([[1, 2],
           [3, 4],
           [1, 2],
           [3, 4]])

    >>> c = np.array([1,2,3,4])
    >>> np.tile(c,(4,1))
    array([[1, 2, 3, 4],
           [1, 2, 3, 4],
           [1, 2, 3, 4],
           [1, 2, 3, 4]])
    """
    try:
        tup = tuple(reps)
    except TypeError:
        tup = (reps,)
    d = len(tup)
    if all(x == 1 for x in tup) and isinstance(A, _nx.ndarray):
        # Fixes the problem that the function does not make a copy if A is a
        # numpy array and the repetitions are 1 in all dimensions
        return _nx.array(A, copy=True, subok=True, ndmin=d)
    else:
        # Note that no copy of zero-sized arrays is made. However since they
        # have no data there is no risk of an inadvertent overwrite.
        c = _nx.array(A, copy=False, subok=True, ndmin=d)
    if (d < c.ndim):
        tup = (1,)*(c.ndim-d) + tup
    shape_out = tuple(s*t for s, t in zip(c.shape, tup))
    n = c.size
    if n > 0:
        for dim_in, nrep in zip(c.shape, tup):
            if nrep != 1:
                c = c.reshape(-1, n).repeat(nrep, 0)
            n //= dim_in
    return c.reshape(shape_out)

具体说明

  • 如果要使用这个函数,首先要对numpy中的array的维数或者说轴(axes)有一定的了解。如果维数不超过三维的话,跟我们生活中的认知是一样(也可以参考行列的概念)

  • tile的难点主要是在与缺省值的理解,就是在A的维度和reps长度不一致的情况。

    • 如果A的维度较小,需要对A先升维。
    • 同样如果reps的长度较小,需要对reps补前导1
      使二者的reps的长度和A的维数保持一致
  • 先注意这一些,然后去看所给的Example就会好一点了,对array_like的对象进行操作之前,最好先查看一下ndim属性。

相关文章

  • python实现K近邻算法

    * 关于函数numpy.tile()的用法,可以参考:Numpy中tile()函数简单理解

  • tile函数的使用(python、numpy)

    python的英文解释和实现源码如下: 具体说明 如果要使用这个函数,首先要对numpy中的array的维数或者说...

  • Python-Numpy函数-tile函数

    转载来源:https://www.cnblogs.com/GDUT-xiang/p/5700928.html b ...

  • python numpy-tile函数

    本文所有代码均可在Pycharm编译运行 Python版本:3.6.2 俗话说,新手看博客,高手看文档,那我们先按...

  • Python numpy.tile()函数

    简单来说,tile函数的作用是让某个数组,以某种方式重复,构造出新的数组,所以返回值也是个数组。举例: 当第二个参...

  • Numpy

    1.numpy.tile(A,B)函数,实例验证 快速入门 Numpy[https://mp.weixin.qq....

  • 图解Numpy的tile函数

    Numpy的 tile() 函数,就是将原矩阵横向、纵向地复制。tile 是瓷砖的意思,顾名思义,这个函数就是把数...

  • 图解Numpy的tile函数

    Numpy的 tile() 函数,就是将原矩阵横向、纵向地复制。tile 是瓷砖的意思,顾名思义,这个函数就是把数...

  • python-numpy之tile函数

    在机器学习算法KNN讲解中出现np.tile(A,B),其中A为array,B为tuple,例如:(3,2)具体使...

  • Numpy中tile()函数简单理解

    Numpy模块中的tile()函数用于对类型为ndarray的多维张量的扩展下面用一个例子说明tile()函数的作...

网友评论

      本文标题:tile函数的使用(python、numpy)

      本文链接:https://www.haomeiwen.com/subject/ubcwnxtx.html