2016-10-21 92 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