2017-01-23 137 views
3

我有一個大小爲(d,N)的矩陣X.換句話說,每個向量都有N個向量。例如,向量化numpy索引並應用函數來構建矩陣

X = [[1,2,3,4],[5,6,7,8]] 

有d = 2維的N = 4個向量。

另外,我有碎布陣列(列表清單)。索引是索引X矩陣中的列。例如,

I = [ [0,1], [1,2,3] ] 

的I [0] =在矩陣X [0,1]索引的列0和1同樣的元件I [1]索引的列1,2和3注意的I族元素是長度不一樣的列表!

我想這樣做,是指數矩陣X列使用我的每個元素,總結向量,並得到一個載體。對I的每個元素重複此操作,從而構建一個新的矩陣Y.矩陣Y應該具有與I數組中的元素一樣多的d維向量。在我的例子中,Y矩陣將有2個2維向量。

在我的例子,該元件I [0]講述從矩陣X薩姆得到列0和1這兩個向量矩陣X的2維矢量,並把該矢量在Y(列0)。然後,元素I [1]告訴對矩陣X的第1,2和3列進行求和,並將這個新的向量置於Y(第1列)。

我可以很容易地使用循環來做到這一點,但如果可能的話,我想向量化這個操作。我的矩陣X有成百上千的列,I索引矩陣有幾萬個元素(每個元素都是一個簡短的索引列表)。

我糊塗代碼:

Y = np.zeros((d,len(I))) 
for i,idx in enumerate(I): 
    Y[:,i] = np.sum(X[:,idx], axis=1) 
+0

分享你的多圈的代碼,如果你已經實現了? – Divakar

+0

@Divakar加我的代碼 –

回答

4

這裏有一個方法 -

# Get a flattened version of indices 
idx0 = np.concatenate(I) 

# Get indices at which we need to do "intervaled-summation" along axis=1 
cut_idx = np.append(0,map(len,I))[:-1].cumsum() 

# Finally index into cols of array with flattend indices & perform summation 
out = np.add.reduceat(X[:,idx0], cut_idx,axis=1) 

步驟一步的奔跑 -

In [67]: X 
Out[67]: 
array([[ 1, 2, 3, 4], 
     [15, 6, 17, 8]]) 

In [68]: I 
Out[68]: array([[0, 2, 3, 1], [2, 3, 1], [2, 3]], dtype=object) 

In [69]: idx0 = np.concatenate(I) 

In [70]: idx0 # Flattened indices 
Out[70]: array([0, 2, 3, 1, 2, 3, 1, 2, 3]) 

In [71]: cut_idx = np.append(0,map(len,I))[:-1].cumsum() 

In [72]: cut_idx # We need to do addition in intervals limited by these indices 
Out[72]: array([0, 4, 7]) 

In [74]: X[:,idx0] # Select all of the indexed columns 
Out[74]: 
array([[ 1, 3, 4, 2, 3, 4, 2, 3, 4], 
     [15, 17, 8, 6, 17, 8, 6, 17, 8]]) 

In [75]: np.add.reduceat(X[:,idx0], cut_idx,axis=1) 
Out[75]: 
array([[10, 9, 7], 
     [46, 31, 25]]) 
+0

謝謝!你介意每一行都做什麼(在我去查找函數之前)? –