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]: aOut[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::这是使用 np.broadcast_to ,它将在输入数组中创建一个扩展视图,然后对其进行屏蔽以获取所需的输出-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]: TrueIn [260]: %timeit approach1(a)1000 loops, best of 3: 1.43 ms per loopIn [261]: %timeit approach2(a)1000 loops, best of 3: 1.56 ms per loop 这篇关于创建平铺的多维数组,同时删除axis0的第I个索引的子元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!