numpy.tile用法
作者:互联网
先说下在numpy中,个人对array的维度的比较形象的理解:
array的维度就是从最外边的[]出发(可理解为array的声明),一直找到具体数值而经过的[]
的数量(含最后的数值,它是最后一维)
比如:
-
(1) [1,2]的维度是(2,),因为最外层的[]仅仅是声明,穿过该[],直接找到数值,所以是一维的,数量是2.
-
(2)
[[1,2],
[3,4]] 的维度是(2,2),从最外层[]出发,向里走,有[1,2],[3,4],所以第一维的数量是2,继续往里走,遇到'1,2'和'3,4',所以第二维的数量是2 -
(3)
[[[1,2],
[3,4]],
[[5,6],
[7,8]]]
的维度是(2,2,2),从最外层[],有两个[[]],所以第一维度是2,继续往里走有两个[],所以第二维度是2,继续走是两个数值,达到了最后一维,且最后一维的数量也是2.
import numpy as np
help(np.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]])
np.tile(A,reps)
就是根据reps的值,将array A在某些维度上进行复制,而产生新的array.
如果reps的长度等于A.ndim,则根据reps的值对A相应的维度进行复制。如果不相等,最后的结果取A.ndim和reps长度的较大值的维度;
如果A.ndim>len(reps),则先对reps前置若干个'1',使reps长度与ndim匹配,然后再进行各维度的复制;
如果A.ndim<len(reps), 则先对A的维度前置若干个'1',使两者匹配,再进行各维度复制。
- A的维度与reps长度相等:
a=np.array([1,2,3])
a.ndim
1
np.tile(a,2)# 可见,将a在自身的维度下复制2次。
array([1, 2, 3, 1, 2, 3])
b=np.array([[1,2,3],
[4,5,6]])
np.tile(b,(2,3))
array([[1, 2, 3, 1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6, 4, 5, 6],
[1, 2, 3, 1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6, 4, 5, 6]])
可见,是先将最后一维在自己的维度复制3遍,然后在第一维复制2遍
- A的维度与reps长度不相等
A.ndim>len(reps)
np.tile(b,2)
array([[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]])
将reps扩展成与b维度一样的即,等效于np.tile(b,(1,2))!
np.tile(b,(1,2)) #在最后一维复制2遍
array([[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]])
A.ndim<len(reps)
np.tile(b,(3,2,2))
array([[[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6],
[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]],
[[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6],
[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]],
[[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6],
[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]]])
将A扩展成与b维度一样的,即A先升维为(1,2,3)
bb=b[np.newaxis,:]
bb
array([[[1, 2, 3],
[4, 5, 6]]])
bb.shape
(1, 2, 3)
np.tile(bb,(3,2,2))
array([[[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6],
[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]],
[[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6],
[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]],
[[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6],
[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]]])
即先在最后一维复制2次,然后倒数第二维复制2次,最后第一维复制3次。
标签:ndim,维度,reps,用法,np,tile,array,numpy 来源: https://www.cnblogs.com/johnyang/p/13785321.html