【问题标题】:Insertion of non aligned elements in 3-dimensional matrices in numpy在 numpy 的 3 维矩阵中插入非对齐元素
【发布时间】:2014-12-02 14:37:19
【问题描述】:

我正在使用 numpy 1.9 和 python 2.7.5 处理 3 维矩阵。 这是一个例子:

>>> A
array([[[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]],

       [[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]]])

>>> B
array([[[-1., -1., -1.],
        [99., 100., 101.],
        [-1., -1., -1.],
        [-1., -1., -1.],
        [-1., -1., -1.]],

       [[-1., -1., -1.],
        [-1., -1., -1.],
        [102., 103., 104.],
        [-1., -1., -1.],
        [-1., -1., -1.]]])

>>> C
array([1, 2])

根据 C,我想在 A 中插入 B 中的所有元素。 示例:c[0] = 1 => After A[0, 1, :] has to be inserted B[0, 1, :]

这是预期结果的示例

>>> D
array([[[1.,  1.,  1.],
        [1.,  1.,  1.],
        [99., 100., 101.],
        [1.,  1.,  1.],
        [1.,  1.,  1.],
        [1.,  1.,  1.]],

       [[1.,  1.,  1.],
        [1.,  1.,  1.],
        [1.,  1.,  1.],
        [102., 103., 104.]
        [1.,  1.,  1.],
        [1.,  1.,  1.]]])

我发现this stackoverflow question 与我的非常相似,只是该解决方案仅适用于二维矩阵,而我正在使用 3 维。

这是我的解决方案,但结果不正确:

C2 = np.repeat(C, 3)
r1 = np.repeat(np.arange(A.shape[0]), 3)
r2 = np.tile(np.arange(3), A.shape[0])
index_map = np.ravel_multi_index((r1, C2, r2), A.shape) + 1
np.insert(A.ravel(), index_map, B.ravel()[index_map]).reshape(A.shape[0], A.shape[1] + 1, A.shape[2])

这是一个使用 for 循环的正确但缓慢的解决方案:

A_2 = np.zeros((A.shape[0], A.shape[1] + 1, A.shape[2]))
for j in xrange(np.size(C, 0)):
  i = C[j]
  A_2[j, :, :] = np.concatenate((A[j, 0:i + 1, :], [B[j, i, :]], A[j, i + 1:, :]))

有什么想法吗?

谢谢!

【问题讨论】:

  • “我得到了不好的结果”是什么意思?不正确?正确但太慢?
  • @WarrenWeckesser 我得到了不正确的结果,因为我将结果与使用 for 循环获得的正确结果进行比较。现在我编辑我的答案,发布正确的 for 循环
  • 我建议实际显示所需的结果和不正确的结果——也许使用更小的数组(比如 4x3x2)。
  • 您的 for 循环示例对我来说失败了,因为 concatenate 给出了 6 行,但 A2 只需要 5 行。那是怎么回事?它真的像你上面写的那样在你的系统上工作吗?
  • 现在我得到IndexError: index 3 is out of bounds for axis 0 with size 3 的例子。

标签: python performance numpy matrix multidimensional-array


【解决方案1】:

你的代码的问题是当你需要插入几个 元素顺序,您需要将它们插入到相同的位置。 比较:

In [139]: x = np.ones(5); x
Out[139]: array([ 1.,  1.,  1.,  1.,  1.])

In [140]: np.insert(x, [1,2,3], 100)
Out[140]: array([   1.,  100.,    1.,  100.,    1.,  100.,    1.,    1.])

In [141]: np.insert(x, [1,1,1], 100)
Out[141]: array([   1.,  100.,  100.,  100.,    1.,    1.,    1.,    1.])

编辑:最初的答案包括完全分解/重塑 回来,但在 3d 中,你需要非常小心才能做到这一点。有一个 考虑到np.insert 和 np.take 接受“轴”参数并允许多值插入。 这仍然需要一些重塑,但它不会诉诸于 np.选择。另外,请注意 mi+1 参数到 np.insert 插入 在之后,而不是在所选行之前:

In [50]: mi = np.ravel_multi_index([np.arange(A.shape[0]), C], A.shape[:2]); mi
Out[50]: array([1, 7])

In [51]: bvals = np.take(B.reshape(-1, B.shape[-1]), mi, axis=0); bvals
Out[51]: 
array([[  99.,  100.,  101.],
       [ 102.,  103.,  104.]])

In [52]: result = (np.insert(A.reshape(-1, A.shape[2]), mi + 1, bvals, axis=0)
                   .reshape(A.shape[0], -1, A.shape[2])); result
Out[52]: 
array([[[   1.,    1.,    1.],
        [   1.,    1.,    1.],
        [  99.,  100.,  101.],
        [   1.,    1.,    1.],
        [   1.,    1.,    1.],
        [   1.,    1.,    1.]],

       [[   1.,    1.,    1.],
        [   1.,    1.,    1.],
        [   1.,    1.,    1.],
        [ 102.,  103.,  104.],
        [   1.,    1.,    1.],
        [   1.,    1.,    1.]]])

这是原来的答案:

In [18]: ixs = np.repeat(np.array([np.arange(A.shape[0]),
                                    C+1,
                                    np.zeros(A.shape[0], dtype=np.int_)]),
                          A.shape[2], axis=1); ixs
   ....: 
Out[18]: 
array([[0, 0, 0, 1, 1, 1],
       [2, 2, 2, 3, 3, 3],
       [0, 0, 0, 0, 0, 0]])

In [19]: mi = np.ravel_multi_index(ixs, A.shape); mi
Out[19]: array([ 6,  6,  6, 24, 24, 24])

In [20]: result = (np.insert(A.ravel(), mi, bvals)
                    .reshape(A.shape[0], A.shape[1] +1, A.shape[2])); result
   ....: 
Out[20]: 
array([[[   1.,    1.,    1.],
        [   1.,    1.,    1.],
        [  99.,  100.,  101.],
        [   1.,    1.,    1.],
        [   1.,    1.,    1.],
        [   1.,    1.,    1.]],

       [[   1.,    1.,    1.],
        [   1.,    1.,    1.],
        [   1.,    1.,    1.],
        [ 102.,  103.,  104.],
        [   1.,    1.,    1.],
        [   1.,    1.,    1.]]])

In [21]: result = (np.insert(A.ravel(), mi, bvals)
                    .reshape(A.shape[0], A.shape[1] +1, A.shape[2])); result
   ....: 
Out[21]: 
array([[[   1.,    1.,    1.],
        [   1.,    1.,    1.],
        [  99.,  100.,  101.],
        [   1.,    1.,    1.],
        [   1.,    1.,    1.],
        [   1.,    1.,    1.]],

       [[   1.,    1.,    1.],
        [   1.,    1.,    1.],
        [   1.,    1.,    1.],
        [ 102.,  103.,  104.],
        [   1.,    1.,    1.],
        [   1.,    1.,    1.]]])

【讨论】:

  • 我认为缺少一些东西:与我在问题中发布的正确结果相比,您的结果上移了 1 行;)
  • 是的,我的错。您将在 之后 插入那些行,而不是之前。在计算 ixs 时,您需要增加 C。我将更新答案以反映这一点。
  • 另外,请参阅更新的答案以获得更简单的解决方案。
  • 与原始循环相比,我没有看到任何加速。可能有一些尺寸组合更好,但不是我尝试过的那些。
【解决方案2】:

这似乎可以替代您(非工作)矢量化解决方案的最后一行:

linear = np.insert(A.ravel(), index_map + r2[::-1], B.ravel()[index_map - 1])
linear.reshape(A.shape[0], A.shape[1] + 1, A.shape[2])

这就像您的矢量化解决方案,但需要进行一些调整才能正确建立索引。第一个关键是要意识到我需要“撤消”您在 index_map 中添加的 1。下一个顿悟是,当您插入linear 时,您需要偏移每行中的索引,因为当您插入元素时,后续的元素会被移回。所以虽然index_map 是[4,5,6,22,23,24],但我们实际上需要[6,6,6,24,24,24],我只是为了这个目的重用/滥用了r2[::-1]。

似乎B.ravel()[index_map - 1] 也可以简化为B[r1,C2,r2]。为了消除r2[::-1] 的怪异减法,再进行一点简化:

C2 = np.repeat(C, 3)
r1 = np.repeat(np.arange(A.shape[0]), 3)
r2 = np.repeat(2, A.shape[0] * A.shape[2])
index_map = np.ravel_multi_index((r1, C2, r2), A.shape) + 1
linear = np.insert(A.ravel(), index_map, B[r1,C2,r2])
linear.reshape(A.shape[0], A.shape[1] + 1, A.shape[2])

【讨论】:

  • 您的解决方案几乎没问题,它适用于我之前发布的示例,但不使用更通用的矩阵:我发现您的方法总是获取 B 的最后一个元素并将其复制到结果中。在新示例之后,我得到 `array([[[ 1., 1., 1.], [ 1., 1., 1.], [ 101., 101., 101.], [ 1., 1. , 1.], [ 1., 1., 1.], [ 1., 1., 1.]] [...]])
猜你喜欢
  • 2015-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-01
  • 1970-01-01
  • 2021-02-28
相关资源
最近更新 更多