有两个成对的多维数组,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 :(得分: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