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. ]]])
Desired output:
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?
[UPDATE]
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.
Additionally I am attempting to use the same permutation technique on a tensor with Tensorflow (please see this post)
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 -Sample run -
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 -Runtime test