python numpy-tile函数

查看help文档

help(numpy.tile)

Help on function tile in module numpy.lib.shape_base:

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]])

tile怎么执行的,有两个要点

(1)比较数组维度d和元组维度reps,如果d<reps,在需要时对数组补中括号 [] 来增加维度。
(2)元组数字从右到左,数组维度从最深维度到最低维度。假设元组为(a,b,c,d,e,f),则数组最深维度重复f次;然后次深维度重复e次;接着次次深维度重复d次;再然后次次次深维度重复c次…… 以此类推,直到对最低维度重复a次,结束,得到结果

例1说明:数组维度d=1,元组维度reps=1,所以对数组的维度进行重复时,不需要补中括号。元组元素从右到左看,第一个元素是2,数组最深维度是1。所以得到

[0, 1, 2, 0, 1, 2]

可以预见,如果只重复1次,会得到原数组本身

[0, 1, 2]

例2

a = array([0, 1, 2])
b = tile(a, (2, 1))
print(b)

//结果为
[[0, 1, 2],  
 [0, 1, 2]]

例2说明:数组只有1个维度,比较数组维度d和元组维度reps,d<reps,所以需要补中括号。元组从右到左看,而数组维度从最深的开始看。对数组a的最深维度重复1次,得到[[0, 1, 2]],然后再对数组a次深维度重复2次,得到[[0, 1, 2], [0, 1, 2]]

现在你应该可以按着上面的思路,得出以下3个函数结果,判断完后再试着在编译器中输出看是否与你预想的一致。

a = array([0, 1, 2])

①tile(a, (2, 2))

②tile(a, (2, 1, 1))

③tile(a, (2, 2, 1))

答案:

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值