numpy的函数:np.tile

tile(A, reps)
Construct an array by repeating A the number of times given by reps.
通重复A的次数,次数为reps,重新构造数组。

If reps has length d, the result will have dimension of
max(d, A.ndim).
如果reps的长度为d,结果的维度为: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.

A.ndim < d:A升级为d维,增加一维,比如: (3,) 维度的数组,通过2-D的复制,变为(1,3的数组,或是3-D的复制变为 (1, 1, 3)的数组。

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])
a = np.array([[0, 1, 2],[5,6,7]])

np.tile(a, 2)
Out[19]:
array([[0, 1, 2, 0, 1, 2],
[5, 6, 7, 5, 6, 7]])
a = np.array([[[0, 1, 2],[5,6,7]]])
a.shape
Out[21]:
(1, 2, 3)
np.tile(a, 2)
Out[22]:
array([[[0, 1, 2, 0, 1, 2],
[5, 6, 7, 5, 6, 7]]])
a
Out[23]:
array([[[0, 1, 2],
[5, 6, 7]]])
在元素内部复制个数。

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

reps为元组时,第一维表示纵向个数,第二维表示横向个数。

猜你喜欢

转载自blog.csdn.net/qq_27009517/article/details/107457486