Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transposing arrays in an array

Tags:

python

numpy

I have a 2D array of shape (M*N,N) which in fact consists of M, N*N arrays. I would like to transpose all of these elements(N*N matrices) in a vectorized fashion. As an example,

import numpy as np
A=np.arange(1,28).reshape((9,3))
print "A before transposing:\n", A
for i in range(3):
    A[i*3:(i+1)*3,:]=A[i*3:(i+1)*3,:].T
print "A after transposing:\n", A

This code generates the following output:

A before transposing: 
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]
 [13 14 15]
 [16 17 18]
 [19 20 21]
 [22 23 24]
 [25 26 27]]
A after transposing: 
 [[ 1  4  7]
 [ 2  5  8]
 [ 3  6  9]
 [10 13 16]
 [11 14 17]
 [12 15 18]
 [19 22 25]
 [20 23 26]
 [21 24 27]]

Which I expect. But I want the vectorized version.

like image 249
Cupitor Avatar asked Apr 25 '14 12:04

Cupitor


People also ask

How do I transpose an array in R?

Rotating or transposing R objects You can rotate the data. frame so that the rows become the columns and the columns become the rows. That is, you transpose the rows and columns. You simply use the t() command.

Can you transpose a 1D array?

The transpose() method can transpose the 2D arrays; on the other hand, it does not affect 1D arrays. Thus, the transpose() method transposes the 2D numpy array.


2 Answers

Here's a nasty way to do it in one line!

A.reshape((-1, 3, 3)).swapaxes(-1, 1).reshape(A.shape)

Step by step. Reshape to (3, 3, 3)

>>> A.reshape((-1, 3, 3))
array([[[ 1,  2,  3],
        [ 4,  5,  6],
        [ 7,  8,  9]],

       [[10, 11, 12],
        [13, 14, 15],
        [16, 17, 18]],

       [[19, 20, 21],
        [22, 23, 24],
        [25, 26, 27]]])

Then perform a transpose-like operation swapaxes on each sub-array

>>> A.reshape((-1, 3, 3)).swapaxes(-1, 1)
array([[[ 1,  4,  7],
        [ 2,  5,  8],
        [ 3,  6,  9]],

       [[10, 13, 16],
        [11, 14, 17],
        [12, 15, 18]],

       [[19, 22, 25],
        [20, 23, 26],
        [21, 24, 27]]])

Finally reshape to (9, 3).

>>> A.reshape((-1, 3, 3)).swapaxes(-1, 1).reshape(A.shape)
array([[ 1,  4,  7],
       [ 2,  5,  8],
       [ 3,  6,  9],
       [10, 13, 16],
       [11, 14, 17],
       [12, 15, 18],
       [19, 22, 25],
       [20, 23, 26],
       [21, 24, 27]])
>>> 

I think that with any method, data must be copied since there's no 2d strides/shape that can generate the result from:

array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19, 20, 21, 22, 23, 24, 25, 26, 27])

(is there?) In my version I think data is copied in the final reshape step

like image 54
YXD Avatar answered Oct 10 '22 15:10

YXD


In [42]: x = np.arange(1,28).reshape((9,3))

In [43]: x
Out[43]: 
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12],
       [13, 14, 15],
       [16, 17, 18],
       [19, 20, 21],
       [22, 23, 24],
       [25, 26, 27]])


In [31]: r,c = x.shape
In [39]: z = np.vstack(np.hsplit(x.T,r/c))

In [45]: z
Out[45]: 
array([[ 1,  4,  7],
       [ 2,  5,  8],
       [ 3,  6,  9],
       [10, 13, 16],
       [11, 14, 17],
       [12, 15, 18],
       [19, 22, 25],
       [20, 23, 26],
       [21, 24, 27]])
like image 27
Abid Rahman K Avatar answered Oct 10 '22 14:10

Abid Rahman K