2016-10-21 129 views
0
同步選擇樣品

有兩個成對的多維數組,A和B兩者,它們的形狀都是[1000,30,30,3]從兩個多維數組

這兩個陣列被對應於第一個陣列中的[i,30,30,3]應該對應於第二個陣列的[i,30,30,3]。

我想從這兩個數組中同步採樣一對兩個元素。此外,我只是想保持過去的三個維度選定的元素,

這是我做過什麼

sampleA = np.zeros(30,30,3) 
sampleB = np.zeros(30,30,3) 

sampleIndex= np.random.randint(0,A.shape[0],1) 

A1 = A[sampleIndex,:,:,:] 
B1 = B[sampleIndex,:,:,:] 

sampleA = A1[?,:,:,:] 
sampleB = B1[?,:,:,:] 

這是正確的做法?有沒有更好或更有效的方法來做到這一點?

+0

你可以用'A1 [0]' ,'np.squeeze(A1)'等等。 – Divakar

回答

0

我會用這1行例行sampleA,sampleB = random.choice(zip(A,B))去:

import numpy as np 
import random 

A= np.random.rand(1000,30,30,3) 
B= np.random.rand(1000,30,30,3)  
sampleA,sampleB = random.choice(zip(A,B)) 

#### check if the indices are the same: 
A_idx = [idx for idx in range(np.shape(A)[0]) if (A[idx,:,:,:]==sampleA).all()] 
B_idx = [idx for idx in range(np.shape(B)[0]) if (B[idx,:,:,:]==sampleB).all()] 

print 'The index of sampled array from A is %s which corresponds to index %s in B'%(str(A_idx),str(B_idx)) 

其中sampleA和sampleB彼此對應課程:

The index of sampled array from A is [919] which corresponds to index [919] in B