从两个多维数组中同步选择样本

2024-04-19 21:03:28 发布

您现在位置:Python中文网/ 问答频道 /正文

有两个成对的多维数组,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[?,:,:,:]

这是正确的方法吗?有没有更好或更有效的方法


Tags: 方法元素a1npzerosrandom数组b1
1条回答
网友
1楼 · 发布于 2024-04-19 21:03:28

我将使用这个单行例程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))

其中Samam辩诉和sampleB当然相互对应:

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

相关问题 更多 >