【问题标题】: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 --> 在 1d 中选择一个元素 --> 如何知道元素在 3d 中的索引?
【发布时间】:2021-04-16 14:23:14
【问题描述】:

numpy 3d 数组 -- 展平 --> 1d 数组 --> 选择 1d 中的一个元素 大批 --> 如何知道元素在3d中的索引? (?, ?, ?)

例如,

我在 3d 数组 D 中有 3d 直方图数据。 让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-sample:samples = np.random.choice(len(P_flat), n, p=P_flat)

然后,我希望将 1D 索引 (=samples) 转换为 D 的 3D 索引。

例如转换(samples = [3, 2, 5]) -> [[?,?,?], [?,?,?], [?,?,?]

【问题讨论】:

    标签: python numpy multidimensional-array indexing sampling


    【解决方案1】:

    您可以为此使用np.unravel_index (doc):

    indices = np.array(np.unravel_index(samples, D.shape)).T
    

    【讨论】:

      猜你喜欢
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 2020-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-19
      相关资源
      最近更新 更多