创建平铺的多维数组,同时删除axis0的第I个索引的子元素? [英] Creating a tiled multi-dimensional array while removing the sub element of the I'th index of axis0?

查看:110
本文介绍了创建平铺的多维数组,同时删除axis0的第I个索引的子元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图平铺每个索引都是多维的数组. 然后,我从每个索引中删除第i个子元素.

I was trying to tile an array where each index is multi-diminsional. I then remove the i'th sub element from each index.

例如,从以下数组开始:

For example, starting with this array:

>>> a = np.array([[    1.     ,     7.     ,     0.     ],
                  [    2.     ,     7.     ,     0.     ],
                  [    3.     ,     7.     ,     0.     ]])
>>> a = np.tile(a, (a.shape[0],1,1))

>>> print a
array([[[    1.     ,     7.     ,     0.     ],
        [    2.     ,     7.     ,     0.     ],
        [    3.     ,     7.     ,     0.     ]],

       [[    1.     ,     7.     ,     0.     ],
        [    2.     ,     7.     ,     0.     ],
        [    3.     ,     7.     ,     0.     ]],

       [[    1.     ,     7.     ,     0.     ],
        [    2.     ,     7.     ,     0.     ],
        [    3.     ,     7.     ,     0.     ]]])

所需的输出:

b = np.array([[[    2.     ,     7.     ,     0.     ],
               [    3.     ,     7.     ,     0.     ]],

              [[    1.     ,     7.     ,     0.     ],
               [    3.     ,     7.     ,     0.     ]],

              [[    1.     ,     7.     ,     0.     ],
               [    2.     ,     7.     ,     0.     ]]])

我想知道是否有一种更有效的方式来生成此输出,而不必先创建大数组然后从中删除呢?

I was wondering if there was a more efficient way to generate this output without having to create a large array first then delete from it?

[更新]

此置换背后的意图是尝试进行向量化,而不是使用python for循环. Divakar提供的答案对完成这项任务有很大的帮助.我还想链接到这篇文章,它显示了这种排列的反面,并且对于重新排列以求和很有用当我完成所有价值时.

The intention behind this permutation was as an attempt to vectorize instead of using python for-loops. The answer provided by Divakar has been a great help in accomplishing this task. I would also like to link to this post which shows the inverse to this permutation, and was useful to rearrange things back for summing over all the values when I was done.

另外,我正尝试在具有Tensorflow的张量上使用相同的置换技术(请参阅此帖子)

Additionally I am attempting to use the same permutation technique on a tensor with Tensorflow (please see this post)

推荐答案

方法#1:这是一种方法,它创建2D索引数组,以便在每个i-th位置跳过这些索引每行,然后将其用于索引到输入数组的第一个轴-

Approach #1 : Here's one approach by creating a 2D array of indices such that those are skipped at each i-th position for each row and then using it for indexing into the first axis of the input array -

def approach1(a):
    n = a.shape[0]
    c = np.nonzero(~np.eye(n,dtype=bool))[1].reshape(n,n-1) # dim0 indices
    return a[c]

样品运行-

In [272]: a
Out[272]: 
array([[56, 95],
       [31, 73],
       [76, 61]])

In [273]: approach1(a)
Out[273]: 
array([[[31, 73],
        [76, 61]],

       [[56, 95],
        [76, 61]],

       [[56, 95],
        [31, 73]]])


方法2::这是使用


Approach #2 : Here's another way using np.broadcast_to that creates an extended view into the input array, which is then masked to get the desired output -

def approach2(a):
    n = a.shape[0]
    mask = ~np.eye(n,dtype=bool)
    return np.broadcast_to(a, (n, n, a.shape[-1]))[mask].reshape(n,n-1,-1)


运行时测试

In [258]: a = np.random.randint(11,99,(200,3))

In [259]: np.allclose(approach1(a), approach2(a))
Out[259]: True

In [260]: %timeit approach1(a)
1000 loops, best of 3: 1.43 ms per loop

In [261]: %timeit approach2(a)
1000 loops, best of 3: 1.56 ms per loop

这篇关于创建平铺的多维数组,同时删除axis0的第I个索引的子元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆