【Python】用Numpy擴充數組元素之repeat和tile


用repeat和tile擴充數組元素,例如,

In [32]: a=np.arange(10)

In [33]: a

Out[33]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [34]: a.repeat(5)

Out[34]:

array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4,

4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9,

9, 9, 9, 9])

In [40]: np.tile(a,2)

Out[40]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) 

官方例子,

>>> from numpy import *
>>> a=array([10,20])
>>> tile(a,(3,2)) #構造3X2個copy
array([[10, 20, 10, 20],
[10, 20, 10, 20],
[10, 20, 10, 20]])
>>> tile(42.0,(3,2))
array([[ 42., 42.],
[ 42., 42.],
[ 42., 42.]])
>>>
>>> from numpy import *>>> repeat(7.,4)array([ 7.,  7.,  7.,  7.])>>> a=array([10,20])>>> a.repeat([3,2])array([10, 10, 10, 20, 20])>>> repeat(a,[3,2])array([10, 10, 10, 20, 20])>>> a=array([[10,20],[30,40]])>>> a.repeat([3,2],axis=0)array([[10, 20],       [10, 20],       [10, 20],       [30, 40],       [30, 40]])>>> a.repeat([3,2],axis=1)array([[10, 10, 10, 20, 20],       [30, 30, 30, 40, 40]])>>> 




注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com