Convert ndarray into 2d array where rows are the elements along the first axis

Question

I asked a similar question here, but since then my criteria have changed a little bit. Now, I'm not sure that this can be done without looping, however I'd like to avoid loops in the case of higher dimensional arrays in hopes that there is a general solution to this.

Say that we have the following 3-dimensional array:

arr = np.array([np.arange(1, 10).reshape((3, 3)) + i/10 for i in range(3)])
arr[0]
Out[36]: 
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.],
       [ 7.,  8.,  9.]])
arr[1]
Out[37]: 
array([[ 1.1,  2.1,  3.1],
       [ 4.1,  5.1,  6.1],
       [ 7.1,  8.1,  9.1]])
arr[2]
Out[38]: 
array([[ 1.2,  2.2,  3.2],
       [ 4.2,  5.2,  6.2],
       [ 7.2,  8.2,  9.2]])

What I'd like to do is convert this into an 2-d array (list of lists works too). Where each row consists of the elements along the first dimension for a given position of the remaining indices.

Here is the general solution that works for higher dimensional arrays as well:

from itertools import product:

X, *Y = arr.shape

result = np.zeros((np.prod(Y), X))

for i, y in enumerate(product(*[range(y) for y in Y])):
    for x in range(X):
        result[i, x] = arr[(x,)+y]

Output:

Out[50]: 
array([[ 1. ,  1.1,  1.2],
       [ 2. ,  2.1,  2.2],
       [ 3. ,  3.1,  3.2],
       [ 4. ,  4.1,  4.2],
       [ 5. ,  5.1,  5.2],
       [ 6. ,  6.1,  6.2],
       [ 7. ,  7.1,  7.2],
       [ 8. ,  8.1,  8.2],
       [ 9. ,  9.1,  9.2]])

And for a 4D array:

arr = np.array([np.arange(1, 28).reshape((3, 3, 3)) + i/10 for i in range(3)])
X, *Y = arr.shape
result = np.zeros((np.prod(Y), X))
for i, y in enumerate(product(*[range(y) for y in Y])):
        for x in range(X):
            result[i, x] = arr[(x,)+y]

result
Out[62]: 
array([[  1. ,   1.1,   1.2],
       [  2. ,   2.1,   2.2],
       [  3. ,   3.1,   3.2],
       [  4. ,   4.1,   4.2],
       [  5. ,   5.1,   5.2],
       [  6. ,   6.1,   6.2],
       [  7. ,   7.1,   7.2],
       [  8. ,   8.1,   8.2],
       [  9. ,   9.1,   9.2],
       [ 10. ,  10.1,  10.2],
       [ 11. ,  11.1,  11.2],
       [ 12. ,  12.1,  12.2],
       [ 13. ,  13.1,  13.2],
       [ 14. ,  14.1,  14.2],
       [ 15. ,  15.1,  15.2],
       [ 16. ,  16.1,  16.2],
       [ 17. ,  17.1,  17.2],
       [ 18. ,  18.1,  18.2],
       [ 19. ,  19.1,  19.2],
       [ 20. ,  20.1,  20.2],
       [ 21. ,  21.1,  21.2],
       [ 22. ,  22.1,  22.2],
       [ 23. ,  23.1,  23.2],
       [ 24. ,  24.1,  24.2],
       [ 25. ,  25.1,  25.2],
       [ 26. ,  26.1,  26.2],
       [ 27. ,  27.1,  27.2]])

Is there anything in numpy that could be used to reshape arrays this way? Preferably without making a copy?


Show source
| python   | arrays   | multidimensional-array   | numpy   | reshape   2016-12-22 00:12 1 Answers

Answers to Convert ndarray into 2d array where rows are the elements along the first axis ( 1 )

  1. 2016-12-22 00:12

    Here is the easier way:

    >>> arr.reshape(3,9).T
    array([[ 1. ,  1.1,  1.2],
           [ 2. ,  2.1,  2.2],
           [ 3. ,  3.1,  3.2],
           [ 4. ,  4.1,  4.2],
           [ 5. ,  5.1,  5.2],
           [ 6. ,  6.1,  6.2],
           [ 7. ,  7.1,  7.2],
           [ 8. ,  8.1,  8.2],
           [ 9. ,  9.1,  9.2]])
    

    Similar for the other case

    >>> arr.reshape(3,12).T
    array([[  1. ,   1.1,   1.2],
           [  2. ,   2.1,   2.2],
           [  3. ,   3.1,   3.2],
           [  4. ,   4.1,   4.2],
           [  5. ,   5.1,   5.2],
           [  6. ,   6.1,   6.2],
           [  7. ,   7.1,   7.2],
           [  8. ,   8.1,   8.2],
           [  9. ,   9.1,   9.2],
           [ 10. ,  10.1,  10.2],
           [ 11. ,  11.1,  11.2],
           [ 12. ,  12.1,  12.2]])
    

Leave a reply to - Convert ndarray into 2d array where rows are the elements along the first axis

◀ Go back