user3439329 user3439329 - 6 months ago 156
Python Question

numpy array to permutation matrix

np.array([1,2,3])


I've got numpy array. I would like to turn it into a numpy array with tuples of each 1:1 permutation. Like this:

np.array([
[(1,1),(1,2),(1,3)],
[(2,1),(2,2),(2,3)],
[(3,1),(3,2),(3,3)],
])


Any thoughts on how to do this efficiently? I need to do this operation a few million times.

Answer

If you're working with numpy, don't work with tuples. Use its power and add another dimension of size two. My recommendation is:

x = np.array([1,2,3])
np.vstack(([np.vstack((x, x, x))], [np.vstack((x, x, x)).T])).T

or:

im = np.vstack((x, x, x))
np.vstack(([im], [im.T])).T

And for a general array:

ix = np.vstack([x for _ in range(x.shape[0])])
return np.vstack(([ix], [ix.T])).T

This will produce what you want:

array([[[1, 1],
        [1, 2],
        [1, 3]],

       [[2, 1],
        [2, 2],
        [2, 3]],

       [[3, 1],
        [3, 2],
        [3, 3]]])

But as a 3D matrix, as you can see when looking at its shape:

Out[25]: (3L, 3L, 2L)

This is more efficient than the solution with permutations as the array size get's bigger. Timing my solution against @Kasra's yields 1ms for mine vs. 46ms for the one with permutations for an array of size 100. @AshwiniChaudhary's solution is more efficient though.