Skip to content
Home » numpy Part 4 – The Shape of a numpy Array

numpy Part 4 – The Shape of a numpy Array

Spread the love

Today we’ll be talking about the shape of a numpy array.

shape of a numpy array

The shape of an array is a tuple of the numbers of elements in each dimension. Have a look at the following array:

>>> a = np.array([[1, 2, 3],
...              [4, 5, 6], 
...              [7, 8, 9],
...              [10, 11, 12]])
>>> print(a)
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

This array has 4 rows and 3 columns, so the shape is (4, 3).

Checking the Shape of an Array

We can use the shape method to check the shape of a numpy array:

>>> np.shape(a)
(4, 3)

Now let’s have a look at some arrays with different numbers of dimensions.

Here we have a 3-dimensional array:

>>> b = np.array([[[1], [2]], [[3], [4]], [[5], [6]]])
>>> print(b)
[[[1]
  [2]]

 [[3]
  [4]]

 [[5]
  [6]]]

We pass a list containing nested lists to the array method. There are 3 lists at the first level, each of them contains two sublists and each sublist contains 1 element, hence the shape is (3, 2, 1):

>>> np.shape(b)
(3, 2, 1)

Now, here’s a 1-dimensional array. The shape is returned as a 1-tuple, so we need a comma at the end:

>>> c = np.array([1, 2, 3, 4, 5])
>>> np.shape(c)
(5,)

Finally, a 0-dimensional array, which actually is a scalar. The shape of this array is just an empty tuple:

>>> d = np.array(7)
>>> np.shape(d)
()

Instead of using the shape method, we can use the shape attribute on the array object:

>>> d.shape
()

If we have a look at the shape of an array, we can tell in which order it’s indexed. So, in our first example the shape was (4, 3), so first the rows are indexed, then the columns. This order represents the depth of nesting: 4 – this is the first level of nesting, 3 – this is the second level. The last level always represents the number of elements in the last level of nesting. In this example there’s just one level of nesting, but if the shape is (3, 2, 1), like in our second example, then we have two levels of nesting, represented by the numbers 3 and 2, and 1 represents the number of elements in the last level of nesting.

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Changing the Shape of an Array

Provided we maintain the total size of an array, we can easily change its shape. Let’s have a look at our first example again:

>>> a = np.array([[1, 2, 3],
...              [4, 5, 6], 
...              [7, 8, 9],
...              [10, 11, 12]])
>>> print(a)
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
>>> a.shape
(4, 3)

Here the shape is (4, 3) and there are 12 elements altogether. This shape could be changed to (3, 4), (6, 2) or (2, 6). Let’s see how it works:

Let’s change the shape to 3 rows and 4 columns:

>>> a.shape = (3, 4)
>>> print(a)
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]

Now let’s change the shape to 6 rows and 2 columns:

>>> a.shape = (6, 2)
>>> print(a)
[[ 1  2]
 [ 3  4]
 [ 5  6]
 [ 7  8]
 [ 9 10]
 [11 12]]

And now let’s change the shape to 2 rows and 6 columns:

>>> a.shape = (2, 6)
>>> print(a)
[[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]]

If we try to change the shape so that the total number of elements would differ, we get a value error:

>>> a.shape = (4, 5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: cannot reshape array of size 12 into shape (4,5)

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Changing the Number of Dimensions

We can even change the number of dimensions provided the total number of elements is maintained. Here’s a vector (a 1-dimensional array), which is reshaped to 2-dimensional arrays:

>>> c = np.array([1, 2, 3, 4, 5])
>>> c.shape
(5,)

>>> c.shape = (5, 1)
>>> print(c)
[[1]
 [2]
 [3]
 [4]
 [5]]

>>> c.shape = (1, 5)
>>> print(c)
[[1 2 3 4 5]]

In the next example we see how the number of dimensions can be reduced:

Here’s our original array:

>>> b = np.array([[[1], [2]], [[3], [4]], [[5], [6]]])
>>> print(b)
[[[1]
  [2]]

 [[3]
  [4]]

 [[5]
  [6]]]
>>> b.shape
(3, 2, 1)

Now let’s reduce the array to 2 dimensions:

>>> b.shape = (6, 1)
>>> print(b)
[[1]
 [2]
 [3]
 [4]
 [5]
 [6]]

And even to 1 dimension:

>>> b.shape = (6,)
>>> print(b)
[1 2 3 4 5 6]

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

The reshape Method

Another method we can use to reshape an array is the reshape method. Here’s how we can use it:

>>> a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
>>> print(a)
[ 1  2  3  4  5  6  7  8  9 10 11 12]
>>> b = a.reshape(3, 4)
>>> print(b)
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]


>>> c = np.arange(20).reshape(4, 5)
>>> print(c)
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]

>>> d = np.arange(36).reshape(2, 3, 6)
>>> print(d)
[[[ 0  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]]]

Here you can watch the video version:


Spread the love

Leave a Reply