1.tile函数说明
tile(A,reps)函数定义在python的模块numpy.lib.shape_base中,其功能是重复某个对象(比如,数组)
2. 简单用法
>>> import numpy
>>> numpy.tile([1,2,3],2)
array([1, 2, 3, 1, 2, 3]) 将数组中的元素复制2次
>>> numpy.tile([1,2,3],(1,2))
array([[1, 2, 3, 1, 2, 3]]) 将数组中的第一维复制1次(就是本身不不变),然后里面的元素复制2次。得到二维数组
>>> numpy.tile([1,2,3],(2,1))
array([[1, 2, 3],
[1, 2, 3]]) 将数组中第一维复制2此,然后将每一维中的元素复制1次(就是本身不不变)
>>> numpy.tile([1,2,3],(2,2))
array([[1, 2, 3, 1, 2, 3],
[1, 2, 3, 1, 2, 3]]) 将数组中的第一维复制2次,然后将每一维中的元素复制2次
如果A是三维数组,rep是包含3个元素的元组,想想结果会如何?是怎么计算的呢?
以一个实际例子来说明。
numpy.tile([[[111,112,113],[121,122,123],[131,132,133]],[[211,212,213],[221,222,223],[231,232,233]],[[311,312,313],[321,322,323],[331,332,333]]],(2,3,4))
也就是说,
三维数组a
[
[
[111,112,113],
[121,122,123],
[131,132,133]
],
[
[211,212,213],
[221,222,223],
[231,232,233]
],
[
[311,312,313],
[321,322,323],
[331,332,333]
]
]
rep= (2,3,4)
让我们来模拟计算一下:
1.将第一维中元素复制2次(纵向扩展)。第一维3个元素变6个,即三行变6行
[ [
a[0], a[0],
a[1], ---> a[1],
a[2] a[2],
} a[0],
a[1],
a[2]
]
复制后,a变为a1
[
[
[111,112,113],
[121,122,123],
[131,132,133]
],
[
[211,212,213],
[221,222,223],
[231,232,233]
],
[
[311,312,313],
[321,322,323],
[331,332,333]
],
[
[111,112,113],
[121,122,123],
[131,132,133]
],
[
[211,212,213],
[221,222,223],
[231,232,233]
],
[
[311,312,313],
[321,322,323],
[331,332,333]
]
]
2.基于步骤1,接着将第二维复制三次
[
[
[111,112,113],
[121,122,123],
[131,132,133],
[111,112,113],
[121,122,123],
[131,132,133],
[111,112,113],
[121,122,123],
[131,132,133]
],
[
[211,212,213],
[221,222,223],
[231,232,233],
[211,212,213],
[221,222,223],
[231,232,233],
[211,212,213],
[221,222,223],
[231,232,233]
],
[
[311,312,313],
[321,322,323],
[331,332,333],
[311,312,313],
[321,322,323],
[331,332,333],
[311,312,313],
[321,322,323],
[331,332,333]
],
[
[111,112,113],
[121,122,123],
[131,132,133],
[111,112,113],
[121,122,123],
[131,132,133],
[111,112,113],
[121,122,123],
[131,132,133]
],
[
[211,212,213],
[221,222,223],
[231,232,233],
[211,212,213],
[221,222,223],
[231,232,233],
[211,212,213],
[221,222,223],
[231,232,233]
],
[
[311,312,313],
[321,322,323],
[331,332,333],
[311,312,313],
[321,322,323],
[331,332,333],
[311,312,313],
[321,322,323],
[331,332,333]
]
]
3.基于步骤2,将第三维复制4次(横向)
[
[
[111,112,113,111,112,113,111,112,113,111,112,113],
[121,122,123,121,122,123,121,122,123,121,122,123],
[131,132,133,131,132,133,131,132,133,131,132,133],
[111,112,113,111,112,113,111,112,113,111,112,113],
[121,122,123,121,122,123,121,122,123,121,122,123],
[131,132,133,131,132,133,131,132,133,131,132,133],
[111,112,113,111,112,113,111,112,113,111,112,113],
[121,122,123,121,122,123,121,122,123,121,122,123],
[131,132,133,131,132,133,131,132,133,131,132,133]
],
[
[211,212,213,211,212,213,211,212,213,211,212,213],
[221,222,223,221,222,223,221,222,223,221,222,223],
[231,232,233,231,232,233,231,232,233,231,232,233],
[211,212,213,211,212,213,211,212,213,211,212,213],
[221,222,223,221,222,223,221,222,223,221,222,223],
[231,232,233,231,232,233,231,232,233,231,232,233],
[211,212,213,211,212,213,211,212,213,211,212,213],
[221,222,223,221,222,223,221,222,223,221,222,223],
[231,232,233,231,232,233,231,232,233,231,232,233]
],
[
[311,312,313,311,312,313,311,312,313,311,312,313],
[321,322,323,321,322,323,321,322,323,321,322,323],
[331,332,333,331,332,333,331,332,333,331,332,333],
[311,312,313,311,312,313,311,312,313,311,312,313],
[321,322,323,321,322,323,321,322,323,321,322,323],
[331,332,333,331,332,333,331,332,333,331,332,333],
[311,312,313,311,312,313,311,312,313,311,312,313],
[321,322,323,321,322,323,321,322,323,321,322,323],
[331,332,333,331,332,333,331,332,333,331,332,333]
]
[
[111,112,113,111,112,113,111,112,113,111,112,113],
[121,122,123,121,122,123,121,122,123,121,122,123],
[131,132,133,131,132,133,131,132,133,131,132,133],
[111,112,113,111,112,113,111,112,113,111,112,113],
[121,122,123,121,122,123,121,122,123,121,122,123],
[131,132,133,131,132,133,131,132,133,131,132,133],
[111,112,113,111,112,113,111,112,113,111,112,113],
[121,122,123,121,122,123,121,122,123,121,122,123],
[131,132,133,131,132,133,131,132,133,131,132,133]
],
[
[211,212,213,211,212,213,211,212,213,211,212,213],
[221,222,223,221,222,223,221,222,223,221,222,223],
[231,232,233,231,232,233,231,232,233,231,232,233],
[211,212,213,211,212,213,211,212,213,211,212,213],
[221,222,223,221,222,223,221,222,223,221,222,223],
[231,232,233,231,232,233,231,232,233,231,232,233],
[211,212,213,211,212,213,211,212,213,211,212,213],
[221,222,223,221,222,223,221,222,223,221,222,223],
[231,232,233,231,232,233,231,232,233,231,232,233]
],
[
[311,312,313,311,312,313,311,312,313,311,312,313],
[321,322,323,321,322,323,321,322,323,321,322,323],
[331,332,333,331,332,333,331,332,333,331,332,333],
[311,312,313,311,312,313,311,312,313,311,312,313],
[321,322,323,321,322,323,321,322,323,321,322,323],
[331,332,333,331,332,333,331,332,333,331,332,333],
[311,312,313,311,312,313,311,312,313,311,312,313],
[321,322,323,321,322,323,321,322,323,321,322,323],
[331,332,333,331,332,333,331,332,333,331,332,333]
]
]
至此,我们完成了numpy.tile的模拟计算。
是不是很奇妙呢?呵呵