np.shape()

This tutorial explains NumPy’s shape() function.

numpy.shape(a)

Return the shape of an array or array_like object a.

Argument Data Type Description
a array_like NumPy array or Python list for which the shape should be returned. If it is a NumPy array, it returns the attribute a.shape. If it is a Python list, it returns a tuple of integer values defining the number of elements in each dimension if you would’ve created a NumPy array from it.

Return Value: shape — a tuple of integers that are set to the lengths of the corresponding array dimensions.

Examples

The straightforward example is when applied to a NumPy array:

>>> import numpy as np
>>> a = np.array([[1, 2], [3, 4]])
>>> np.shape(a)
(2, 2)

You import the NumPy library and create a two-dimensional array from a list of lists. If you pass the NumPy array into the shape function, it returns a tuple with two values (=dimensions). Each dimension stores the number of elements in this dimension (=axis). As it is a 2×2 quadratic matrix, the result is (2,2).

The following shape is another example of a multi-dimensional array:

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

The shape is now (2, 4) with two rows and four columns.

np.shape() vs array.shape

Note that the result of np.shape(b) and b.shape is the same if b is a NumPy array. If b isn’t a NumPy array but a list, you cannot use b.shape as lists don’t have the shape attribute. Let’s have a look at this example:

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

The np.shape() function returns the same shape tuple—even if you pass a nested list into the function instead of a NumPy array.

But if you try to access the list.shape attribute, NumPy throws the following error:

>>> b.shape
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    b.shape
AttributeError: 'list' object has no attribute 'shape'

So, the difference between np.shape() and array.shape is that the former can be used for all kinds of array_like objects while the latter can only be used for NumPy arrays with the shape attribute.

Recap NumPy Shape

The shape attribute always returns a tuple that tells us the length of each dimension. The one-dimensional array is a row vector and its shape is a single value iterable followed by a comma. One-dimensional arrays don’t have rows and columns, so the shape attribute returns a single value tuple.

Let’s look at an example:

import numpy as np

#an one-dimensional NumPy array
arr = np.arange(10)

#print an array
print(arr)
# [0 1 2 3 4 5 6 7 8 9]

print(arr.shape)
# (10, )

The code snippet also uses the NumPy arange function to create an initial array of subsequent values between 0 and 9. Please find a detailed discussion of the NumPy arange function in this Finxter blog article: https://blog.finxter.com/numpy-arange/.

The shape attribute of a two-dimensional array (also called a matrix) gives us a tuple. The shape returns the number of elements along each dimension, which is the number of rows and columns in the two-dimensional array.

# A two-dimensional NumPy array
import numpy as np

arr = np.array([[1,2,3,4,5], [5,4,3,2,1]])
print(arr.shape)
# (2, 5)

The following example is for the shape of three-dimensional arrays.

# A three-dimensional array
import numpy as np

arr = np.array([ [ [0, 11, 15, 16], [3, 7, 10, 34], [44, 99, 5, 67] ],[ [52, 8, 11, 13], [0, 4, 5, 6], [4, 4, 4, 4] ] ])
print(arr.shape)
# (2, 3, 4)

It takes some practice to understand the shape tuple for multidimensional arrays. The dimensions represented by a tuple are read from the outside-in. If you observe the brackets, the outmost bracket is a part of the basic syntax for the whole array. In the shape tuple 2 represents the second set of brackets. If you count them you will see that there are 2 elements in this dimension.

1st element [ [0, 11, 15, 16], [3, 7, 10, 34], [44, 99, 5, 67] ]

2nd element [ [52, 8, 11, 13], [0, 4, 5, 6], [4, 4, 4, 4] ]

Each element contains 3 more elements in the second dimension. If you
think about nested lists, you can draw the analogy. These elements are:

1st element [0, 11, 15, 16]

2nd element [3, 7, 10, 34]

3rd element [44, 99, 5, 67]

Finally, number 4 represents the number of elements in the third dimension. Those are the innermost elements. For example 0, 11, 15 and 16.


Do you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.)

Coffee Break NumPy

References

Author: Shantun Parmar

11 thoughts on “np.shape()

  1. I am commenting to let you know of the great encounter my friend’s daughter found reading your web page. She figured out numerous issues, not to mention what it’s like to have a wonderful giving mood to have men and women really easily have an understanding of selected hard to do subject areas. You really exceeded our own expectations. Many thanks for imparting the interesting, safe, explanatory and even easy thoughts on the topic to Janet.

  2. Thank you for each of your hard work on this web page. My aunt really likes making time for investigations and it’s really obvious why. Most of us learn all of the lively method you offer precious suggestions through this web site and even increase participation from some other people on the matter so our favorite girl is now learning a lot of things. Take advantage of the rest of the year. You are always doing a really great job.

  3. I am commenting to let you know of the fantastic encounter my cousin’s daughter experienced reading your site. She even learned many issues, not to mention what it’s like to have a wonderful giving mindset to have many people just grasp chosen grueling subject areas. You really exceeded our expectations. Many thanks for distributing the informative, healthy, educational and as well as easy thoughts on the topic to Jane.

  4. I’m commenting to let you be aware of of the amazing discovery my child developed browsing the blog. She came to find a good number of details, including how it is like to have a great coaching character to get a number of people clearly completely grasp a number of advanced issues. You actually did more than her desires. I appreciate you for churning out such beneficial, dependable, edifying and cool guidance on that topic to Emily.

  5. I happen to be writing to make you understand what a wonderful experience our princess went through visiting yuor web blog. She realized too many things, with the inclusion of what it is like to possess an incredible teaching style to make the rest without problems understand various very confusing topics. You undoubtedly surpassed visitors’ expected results. Thanks for supplying those warm and helpful, trustworthy, revealing not to mention unique tips on your topic to Tanya.

Leave a Reply

Your email address will not be published.