簡體   English   中英

組合兩個不同維的numpy數組

[英]Combining two numpy arrays of different dimensions

當我閱讀numpy教程時,我會給自己挑戰以建立自己的理解。 當我在上一個示例中看到他們的最終產品修改過的數組不是真正的數組時,我正在閱讀tutorialpoint.com的numpy資源。

頁面底部,廣播迭代示例: https : //www.tutorialspoint.com/numpy/numpy_iterating_over_array.htm

因此,我認為嘗試創建與數組相同的最終產品將是一個不錯的挑戰。 我成功了,但是我無法使用np.nditer,也無法利用廣播,盡管我確信必須有一種利用兩者之一的方法。

這是我的代碼:

a = np.arange(0,60,5) 
a = a.reshape(12,1)
b = np.arange(1,5)
arr = np.zeros((12,2))

counter = 0
for i in range(arr.shape[0]):
    if counter < 4:
        arr[i,:] = np.array([a[i],b[counter]])
        counter += 1
    else:
        counter = 0
        arr[i,:] = np.array([a[i],b[counter]])

print arr

我怎樣才能更有效地做到這一點?

我以前沒有看過特定的nditer教程。
https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.nditer.html

是我用過的那個 而且我一直在告訴人們nditer這個Python界面效率不高。 如最后一個cython示例所示,此頁面對於在C代碼中使用nditer跳板最有用。

使用np.nditer numpy函數並不多(在Python代碼中)。 np.ndindex是少數幾個。 值得閱讀其代碼。 np.einsum使用此迭代器,但使用已編譯的代碼。

稍后,我將花一些時間閱讀並評論所涉及的示例。 學習好用廣播比使用nditer更重要。

In [212]: a=np.arange(0,60,5).reshape(3,4)
In [213]: a
Out[213]: 
array([[ 0,  5, 10, 15],
       [20, 25, 30, 35],
       [40, 45, 50, 55]])
In [214]: b=np.arange(1,5)
In [215]: b
Out[215]: array([1, 2, 3, 4])

In [225]: for x,y in np.nditer([a,b]):
     ...:     print("%d:%d"%(x,y), end=' ')
     ...: print()
0:1 5:2 10:3 15:4 20:1 25:2 30:3 35:4 40:1 45:2 50:3 55:4 

等效的普通Python迭代:

In [231]: for row in a:
     ...:     for x,y in zip(row,b):
     ...:         print("%d:%d"%(x,y), end=' ')
     ...: print()
     ...: 
0:1 5:2 10:3 15:4 20:1 25:2 30:3 35:4 40:1 45:2 50:3 55:4 

np.broadcast將使用(4,)廣播(3,4)數組:

In [234]: np.broadcast(a,b)
Out[234]: <numpy.broadcast at 0x9c2a7f8>
In [235]: list(_)
Out[235]: 
[(0, 1),
 (5, 2),
 (10, 3),
 (15, 4),
 (20, 1),
 (25, 2),
 (30, 3),
 (35, 4),
 (40, 1),
 (45, 2),
 (50, 3),
 (55, 4)]

使用np.array(list(np.broadcast(a,b)))制作(12,2)數組。

或使用相同的打印:

In [237]: for x,y in np.broadcast(a,b):
     ...:     print("%d:%d"%(x,y), end=' ')
     ...: print()
     ...: 
0:1 5:2 10:3 15:4 20:1 25:2 30:3 35:4 40:1 45:2 50:3 55:4

您的迭代:

In [251]: arr = np.zeros((12,2),dtype=int)
     ...: counter = 0
     ...: for i in range(arr.shape[0]):
     ...:     if counter < 4:
     ...:         arr[i,:] = np.array([a.flat[i],b[counter]])
     ...:         counter += 1
     ...:     else:
     ...:         counter = 0
     ...:         arr[i,:] = np.array([a.flat[i],b[counter]])
     ...:         
In [252]: arr
Out[252]: 
array([[ 0,  1],
       [ 5,  2],
       [10,  3],
       [15,  4],
       [20,  1],
       [25,  1],
       [30,  2],
       [35,  3],
       [40,  4],
       [45,  1],
       [50,  1],
       [55,  2]])

糟糕,如果您希望第二列是重復的b ,則看起來有些不對勁。

有很多方法可以將ab組合到這種數組中。

這會將2d a變成1d; tile復制b ,然后將它們與stack連接( column_stack也可以使用):

In [264]: np.stack((a.flat, np.tile(b,3)),1)
Out[264]: 
array([[ 0,  1],
       [ 5,  2],
       [10,  3],
       [15,  4],
       [20,  1],
       [25,  2],
       [30,  3],
       [35,  4],
       [40,  1],
       [45,  2],
       [50,  3],
       [55,  4]])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM