1

I have the following Numpy array. enter image description here

I want to reshape it to be (3,5,3)

So i must be like:

[
[
[1,6,11],
[2,7,12],
[3,8,13],
[4,9,14],
[5,10,15]
],.......
]

I tried reshape(3,5,3) but it doesn't give the wanted result?

4
  • 1
    "doesn't give the wanted result" is not specific enough. What result did you expect, and which one did you actually get? What code gave you this result? Commented May 12, 2019 at 13:38
  • 2
    aaa.transpose(0,2,1)? Commented May 12, 2019 at 13:39
  • 1
    I guess you're using numpy. so you should give the new shape as a tuple myarray.reshape((3,5,3)) (double brackets) Commented May 12, 2019 at 13:41
  • Thank you @Divakar Commented May 12, 2019 at 13:42

2 Answers 2

1

Your input array is of shape (3, 3, 5) and you want it to be reshaped it to (3, 5, 3). There are many ways of doing this. Below are some, as also mentioned in the comments:

First would be to use numpy.reshape() which accepts newshape as a parameter:

In [77]: arr = np.arange(3*3*5).reshape(3, 3, 5)

# reshape to desired shape
In [78]: arr = arr.reshape((3, 5, 3))

In [79]: arr.shape
Out[79]: (3, 5, 3)

Or you can use numpy.transpose() as in:

In [80]: arr = np.arange(3*3*5).reshape(3, 3, 5)

In [81]: arr.shape
Out[81]: (3, 3, 5)

# now, we want to move the last axis which is 2 to second position
# thus our new shape would be `(3, 5, 3)`
In [82]: arr = np.transpose(arr, (0, 2, 1))

In [83]: arr.shape
Out[83]: (3, 5, 3)

Another way would be to use numpy.moveaxis() :

In [87]: arr = np.arange(3*3*5).reshape(3, 3, 5)

# move the last axis (-1) to 2nd position (1)
In [88]: arr = np.moveaxis(arr, -1, 1)

In [89]: arr.shape
Out[89]: (3, 5, 3)

Yet another way would be to just swap the axes using numpy.swapaxes() :

In [90]: arr = np.arange(3*3*5).reshape(3, 3, 5)

In [91]: arr.shape
Out[91]: (3, 3, 5)

# swap the position of ultimate and penultimate axes
In [92]: arr = np.swapaxes(arr, -1, 1)

In [93]: arr.shape
Out[93]: (3, 5, 3)

Choose whichever is more intuitive to you since all approaches return a new view of the desired shape.


Although all of the above return a view, there are some timing differences. So, the preferred way of doing this (for efficiency) would be:

In [124]: arr = np.arange(3*3*5).reshape(3, 3, 5)

In [125]: %timeit np.swapaxes(arr, -1, 1)
456 ns ± 6.79 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [126]: %timeit np.transpose(arr, (0, 2, 1))
458 ns ± 6.93 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [127]: %timeit np.reshape(arr, (3, 5, 3))
635 ns ± 9.06 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [128]: %timeit np.moveaxis(arr, -1, 1)
3.42 µs ± 79.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

numpy.swapaxes() and numpy.transpose() takes almost the same time, with numpy.reshape() being a bit slower, while numpy.moveaxis being the slowest among all. So, it'd be wise to use either swapaxes or transpose ufunc.

Sign up to request clarification or add additional context in comments.

3 Comments

Which of these would be the most efficient to work with?
@KartikeySingh Added an explanation for this. HTH :)
It depends on data case as here it is better to use transpose but most of the time we need reshape and transpose cannot be used there. Thank you for the explanation :)
0

I found a way of doing it using List comprehension and Numpy transpose.

Code:

import numpy as np
database = [
        [
            [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,28,29,30]
        ],
        [
            [31,32,33,34,35],
            [36,37,38,39,40],
            [41,42,43,44,45]
        ]
    ]

ans = [np.transpose(data) for data in database]
print(ans)

Output:

[array([[ 1,  6, 11],
       [ 2,  7, 12],
       [ 3,  8, 13],
       [ 4,  9, 14],
       [ 5, 10, 15]]), 
 array([[16, 21, 26],
       [17, 22, 27],
       [18, 23, 28],
       [19, 24, 29],
       [20, 25, 30]]), 
 array([[31, 36, 41],
       [32, 37, 42],
       [33, 38, 43],
       [34, 39, 44],
       [35, 40, 45]])]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.