Kodeclik Blog


How to change the dimensions of a numpy array

Let us suppose three friends goto the supermarket and select 8 fruits each. Thus in total they have 24 fruits. Let us create a Python numpy array comprising these 24 fruits:
As expected this will give the output:
What we would really like to do is to take these same fruits and organize them into 3 rows of 8 fruits each, rather than one long single row of 24 fruits. Furthermore if an additional friend joins the group (after the original three have purchased the fruits), we wish to share the 24 fruits, so that each of the 4 friends will now have 6 fruits each. How can we do this in Python?
There are numerous ways! Let us explore them one by one.

Method 1: Use numpy.reshape()

This is a very straightforward approach. As the name indicates, reshape() takes an existing numpy array and new dimensions and reshapes the given array into these dimensions:
Here we are reshaping fruits (which is a 1x24 array) into a 3x8 array. The output will be:
as expected.
We can further reshape it as given in this program:
Note that we first reshaped fruits into newfruits. Then we are reshaping newfruits into finalfruits (we could have reshaped in one go, with the same results). The output will be:
In other words, we get 4 rows of 6 fruits each.
Note that the reshape commands use a row-wise ordering. In other words they start taking elements from the original array and start filling in row-by-row in the new array. You can also adopt a column-wise ordering. Here is how that works:
The output will be:
Notice that you have to now see down a column to understand how the fruits are distributed. The only difference is the “order=F” argument. The “F” here denotes a Fortran-language like ordering which is a column-major ordering. The default ordering uses “order=C” which is a C-language like ordering, or row-wise ordering.
One way to understand these different orderings and how they work is to imagine you have 24 fruits and you have 3 friends. How are you going to distribute these 24 fruits among these 3 friends? The default approach is to give 8 fruits to one friend before moving onto the next friend. This is the “order=C” approach. On the other hand, if the friends are very hungry, you give one fruit to the first person, then one fruit to the second person, then one to the third person, and then return to the first person, and repeat. Viewed in this manner, these orderings are simply different ways to traverse an array or matrix of numbers.

Method 2: Use shape() on the numpy array

In the second approach, we use the shape() method that can be applied to any numpy array. The advantage of using shape() is that it is both assignable and extractable, just like regular variables. Here is how that works:
Here we have our usual array of fruits which is a 1x24 array. We simply assign a new shape to fruits.shape; in this case, three rows of 8 fruits each. The output will be:
as expected.

Method 3: Use numpy.resize()

This approach is very similar to numpy.reshape() that we saw earlier. While reshape() is used to create a new array of the same size (as the original array) but of different desired dimensions, we can use resize() to create an array of larger size than the original array.
First, let us use it in the usual manner:
The output is:
This is as expected and similar in behavior to the previous approach.
But let us try to resize to a larger array:
This time we are resizing it to a much larger array. In fact, we are asking for three additional rows. When numpy runs out of elements, the resize() method goes back to the start of the array and reuses them. Thus the output will be:
On the other hand, if you try this approach with reshape(), you will get this error:
To sum up, we have seen three different ways to change the dimensions of a numpy array. Which one is your favorite?
If you would like to explore more numpy, checkout other Kodeclik blogposts, such as how to convert a Python list into a numpy array.
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.
  • ABOUT

Copyright @ Kodeclik 2024. All rights reserved.