Kodeclik Logo

Our Programs

Courses

Learn More

Schedule

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:

import numpy as np
fruits = np.array(['apple', 'orange', 'banana', 'pear', 
'strawberry', 'blueberry', 'pineapple', 'cherry', 'mango', 
'watermelon', 'grape', 'kiwi', 'peach', 'plum', 'lemon', 
'lime', 'coconut', 'avocado', 'papaya', 'lychee', 'grapefruit', 
'raspberry', 'blackberry', 'cantaloupe'])
print(fruits)

As expected this will give the output:

['apple' 'orange' 'banana' 'pear' 'strawberry' 'blueberry' 
'pineapple' 'cherry' 'mango' 'watermelon' 'grape' 'kiwi' 
'peach' 'plum' 'lemon' 'lime' 'coconut' 'avocado' 'papaya' 
'lychee' 'grapefruit' 'raspberry' 'blackberry' 'cantaloupe']

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.

How to change the dimensions of a numpy array

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:

import numpy as np
fruits = np.array(['apple', 'orange', 'banana', 'pear', 'strawberry', 
'blueberry', 'pineapple', 'cherry', 'mango', 'watermelon', 'grape', 
'kiwi', 'peach', 'plum', 'lemon', 'lime', 'coconut', 'avocado', 
'papaya', 'lychee', 'grapefruit', 'raspberry', 'blackberry', 
'cantaloupe'])
newfruits = np.reshape(fruits, (3,8))
print(newfruits)

Here we are reshaping fruits (which is a 1x24 array) into a 3x8 array. The output will be:

[['apple' 'orange' 'banana' 'pear' 'strawberry' 'blueberry' 
'pineapple' 'cherry']
['mango' 'watermelon' 'grape' 'kiwi' 'peach' 'plum' 'lemon' 'lime']
['coconut' 'avocado' 'papaya' 'lychee' 'grapefruit' 'raspberry'
'blackberry' 'cantaloupe']]

as expected.

We can further reshape it as given in this program:

import numpy as np
fruits = np.array(['apple', 'orange', 'banana', 'pear', 'strawberry', 
'blueberry', 'pineapple', 'cherry', 'mango', 'watermelon', 'grape', 
'kiwi', 'peach', 'plum', 'lemon', 'lime', 'coconut', 'avocado', 
'papaya', 'lychee', 'grapefruit', 'raspberry', 'blackberry', 
'cantaloupe'])
newfruits = np.reshape(fruits, (3,8))
finalfruits = np.reshape(newfruits, (4,6))
print(finalfruits)

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:

[['apple' 'orange' 'banana' 'pear' 'strawberry' 'blueberry']
['pineapple' 'cherry' 'mango' 'watermelon' 'grape' 'kiwi']
['peach' 'plum' 'lemon' 'lime' 'coconut' 'avocado']
['papaya' 'lychee' 'grapefruit' 'raspberry' 'blackberry' 
'cantaloupe']]

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:

import numpy as np
fruits = np.array(['apple', 'orange', 'banana', 'pear', 'strawberry', 
'blueberry', 'pineapple', 'cherry', 'mango', 'watermelon', 'grape', 
'kiwi', 'peach', 'plum', 'lemon', 'lime', 'coconut', 'avocado', 
'papaya', 'lychee', 'grapefruit', 'raspberry', 'blackberry', 
'cantaloupe'])
newfruits = np.reshape(fruits, (3,8), order='F')
print(newfruits)

The output will be:

[['apple' 'pear' 'pineapple' 'watermelon' 'peach' 'lime' 
'papaya' 'raspberry']
['orange' 'strawberry' 'cherry' 'grape' 'plum' 'coconut' 
'lychee' 'blackberry']
['banana' 'blueberry' 'mango' 'kiwi' 'lemon' 'avocado' 
'grapefruit' 'cantaloupe']]

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:

import numpy as np
fruits = np.array(['apple', 'orange', 'banana', 'pear', 'strawberry', 
'blueberry', 'pineapple', 'cherry', 'mango', 'watermelon', 'grape', 
'kiwi', 'peach', 'plum', 'lemon', 'lime', 'coconut', 'avocado', 
'papaya', 'lychee', 'grapefruit', 'raspberry', 'blackberry', 
'cantaloupe'])
fruits.shape = (3,8)
print(fruits)
print(fruits.shape)

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:

[['apple' 'orange' 'banana' 'pear' 'strawberry' 'blueberry' 
'pineapple' 'cherry']
['mango' 'watermelon' 'grape' 'kiwi' 'peach' 'plum' 'lemon' 'lime']
['coconut' 'avocado' 'papaya' 'lychee' 'grapefruit' 'raspberry'
'blackberry' 'cantaloupe']]
(3, 8)

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:

import numpy as np
fruits = np.array(['apple', 'orange', 'banana', 'pear', 
'strawberry', 'blueberry', 'pineapple', 'cherry', 'mango', 
'watermelon', 'grape', 'kiwi', 'peach', 'plum', 'lemon', 
'lime', 'coconut', 'avocado', 'papaya', 'lychee', 'grapefruit', 
'raspberry', 'blackberry', 'cantaloupe'])
newfruits = np.resize(fruits,(4,6))
print(newfruits)

The output is:

[['apple' 'orange' 'banana' 'pear' 'strawberry' 'blueberry']
['pineapple' 'cherry' 'mango' 'watermelon' 'grape' 'kiwi']
['peach' 'plum' 'lemon' 'lime' 'coconut' 'avocado']
['papaya' 'lychee' 'grapefruit' 'raspberry' 'blackberry' 'cantaloupe']]

This is as expected and similar in behavior to the previous approach.

But let us try to resize to a larger array:

import numpy as np
fruits = np.array(['apple', 'orange', 'banana', 'pear', 'strawberry', 
'blueberry', 'pineapple', 'cherry', 'mango', 'watermelon', 'grape', 
'kiwi', 'peach', 'plum', 'lemon', 'lime', 'coconut', 'avocado', 
'papaya', 'lychee', 'grapefruit', 'raspberry', 'blackberry', 'cantaloupe'])
newfruits = np.resize(fruits,(7,6))
print(newfruits)

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:

[['apple' 'orange' 'banana' 'pear' 'strawberry' 'blueberry']
['pineapple' 'cherry' 'mango' 'watermelon' 'grape' 'kiwi']
['peach' 'plum' 'lemon' 'lime' 'coconut' 'avocado']
['papaya' 'lychee' 'grapefruit' 'raspberry' 'blackberry' 'cantaloupe']
['apple' 'orange' 'banana' 'pear' 'strawberry' 'blueberry']
['pineapple' 'cherry' 'mango' 'watermelon' 'grape' 'kiwi']
['peach' 'plum' 'lemon' 'lime' 'coconut' 'avocado']]

On the other hand, if you try this approach with reshape(), you will get this error:

ValueError: cannot reshape array of size 24 into shape (7,6)

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.

Kodeclik sidebar newsletter

Join our mailing list

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

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.