numpy 3d array -- flatten --> 1d array --> select one element in 1d --> 如何知道3d中元素的索引?
numpy 3d array -- flatten --> 1d array --> select one element in 1d --> how to know the index of the element in 3d?
numpy 3d array -- flatten --> 1d array --> select one element in 1d
array
--> how to know the index of the element in 3d? (?, ?, ?)
例如,
我在 3d 数组中有 3d 直方图数据 D
。
设 D
为 D = np.random.randint(0,100, size=(4,3,2))
.
那么每个元素的概率就等于P = D / D.sum(0).sum(0).sum(0)
.
让P像P_flat = P.reshape(-1)
一样扁平化。
然后从P_flat
中随机选择n个样本:samples = np.random.choice(len(P_flat), n, p=P_flat)
然后,我希望将1D索引(=samples
)转换为D
的3D索引。
例如,convert(samples = [3, 2, 5]
) -> [[?,?,?], [?,?,?], [?,?,?]
.
你可以使用 np.unravel_index
(doc):
indices = np.array(np.unravel_index(samples, D.shape)).T
numpy 3d array -- flatten --> 1d array --> select one element in 1d array --> how to know the index of the element in 3d? (?, ?, ?)
例如,
我在 3d 数组中有 3d 直方图数据 D
。
设 D
为 D = np.random.randint(0,100, size=(4,3,2))
.
那么每个元素的概率就等于P = D / D.sum(0).sum(0).sum(0)
.
让P像P_flat = P.reshape(-1)
一样扁平化。
然后从P_flat
中随机选择n个样本:samples = np.random.choice(len(P_flat), n, p=P_flat)
然后,我希望将1D索引(=samples
)转换为D
的3D索引。
例如,convert(samples = [3, 2, 5]
) -> [[?,?,?], [?,?,?], [?,?,?]
.
你可以使用 np.unravel_index
(doc):
indices = np.array(np.unravel_index(samples, D.shape)).T