2016-12-31 78 views
2

我有3D NP陣列是這樣的:重複numpy的陣列

x= array([[[ 1., 2., 3.], 
    [ 4., 5., 6.], 
    [ 7., 8., 9.], 
    [ 10., 11., 12.]], 

    [[ 13., 14., 15.], 
    [ 16., 17., 18.], 
    [ 19., 20., 21.], 
    [ 22., 23., 24.]]]) 

我想重複我的陣列的n倍(例如,3次)是這樣的:

array([[[ 1., 2., 3.], 
    [ 4., 5., 6.], 
    [ 7., 8., 9.], 
    [ 10., 11., 12.]], 

    [[ 1., 2., 3.], 
    [ 4., 5., 6.], 
    [ 7., 8., 9.], 
    [ 10., 11., 12.]], 

    [[ 1., 2., 3.], 
    [ 4., 5., 6.], 
    [ 7., 8., 9.], 
    [ 10., 11., 12.]], 

    [[ 13., 14., 15.], 
    [ 16., 17., 18.], 
    [ 19., 20., 21.], 
    [ 22., 23., 24.]], 

    [[ 13., 14., 15.], 
    [ 16., 17., 18.], 
    [ 19., 20., 21.], 
    [ 22., 23., 24.]] 

    [[ 13., 14., 15.], 
    [ 16., 17., 18.], 
    [ 19., 20., 21.], 
    [ 22., 23., 24.]]]) 

我試圖像這個:

xx=np.vstack([x]*3) 
print xx.reshape(6,4,3) 


array([[[ 1., 2., 3.], 
    [ 4., 5., 6.], 
    [ 7., 8., 9.], 
    [ 10., 11., 12.]], 

    [[ 13., 14., 15.], 
    [ 16., 17., 18.], 
    [ 19., 20., 21.], 
    [ 22., 23., 24.]], 

    [[ 1., 2., 3.], 
    [ 4., 5., 6.], 
    [ 7., 8., 9.], 
    [ 10., 11., 12.]], 

    [[ 13., 14., 15.], 
    [ 16., 17., 18.], 
    [ 19., 20., 21.], 
    [ 22., 23., 24.]], 

    [[ 1., 2., 3.], 
    [ 4., 5., 6.], 
    [ 7., 8., 9.], 
    [ 10., 11., 12.]], 

    [[ 13., 14., 15.], 
    [ 16., 17., 18.], 
    [ 19., 20., 21.], 
    [ 22., 23., 24.]]]) 

我怎樣才能得到我想要的順序,應該有簡單的方法來做到這一點。預先感謝您的建議。

回答

2

您可以使用np.repeataxis = 0

np.repeat(x, [3, 3], axis = 0) # or more generally np.repeat(x, [n] * len(x), axis = 0) 
           # here n is the repeat times 
Out[514]: 
array([[[ 1., 2., 3.], 
     [ 4., 5., 6.], 
     [ 7., 8., 9.], 
     [ 10., 11., 12.]], 

     [[ 1., 2., 3.], 
     [ 4., 5., 6.], 
     [ 7., 8., 9.], 
     [ 10., 11., 12.]], 

     [[ 1., 2., 3.], 
     [ 4., 5., 6.], 
     [ 7., 8., 9.], 
     [ 10., 11., 12.]], 

     [[ 13., 14., 15.], 
     [ 16., 17., 18.], 
     [ 19., 20., 21.], 
     [ 22., 23., 24.]], 

     [[ 13., 14., 15.], 
     [ 16., 17., 18.], 
     [ 19., 20., 21.], 
     [ 22., 23., 24.]], 

     [[ 13., 14., 15.], 
     [ 16., 17., 18.], 
     [ 19., 20., 21.], 
     [ 22., 23., 24.]]]) 

另一種選擇是將指數作爲:

x[[0,0,0,1,1,1]] 

或以編程方式:

x[[i for i in range(len(x)) for j in range(3)]] 
Out[518]: 
array([[[ 1., 2., 3.], 
     [ 4., 5., 6.], 
     [ 7., 8., 9.], 
     [ 10., 11., 12.]], 

     [[ 1., 2., 3.], 
     [ 4., 5., 6.], 
     [ 7., 8., 9.], 
     [ 10., 11., 12.]], 

     [[ 1., 2., 3.], 
     [ 4., 5., 6.], 
     [ 7., 8., 9.], 
     [ 10., 11., 12.]], 

     [[ 13., 14., 15.], 
     [ 16., 17., 18.], 
     [ 19., 20., 21.], 
     [ 22., 23., 24.]], 

     [[ 13., 14., 15.], 
     [ 16., 17., 18.], 
     [ 19., 20., 21.], 
     [ 22., 23., 24.]], 

     [[ 13., 14., 15.], 
     [ 16., 17., 18.], 
     [ 19., 20., 21.], 
     [ 22., 23., 24.]]]) 
+0

從(2,4,3)開始,你不應該需要重複後重新塑形。 – hpaulj

+0

@hpaulj感謝您的評論。它可以沿着'axis = 0'與'[3,3]'一起工作,而無需重新塑形。 – Psidom

4

後位的審判和錯誤我已經找到了一種方法來做到這一點:

np.tile(x.reshape(2,12), [1,3]).reshape(6,4,3)