This tutorial explains NumPy’s
Return the shape of an array or array_like object
|array_like||NumPy array or Python list for which the shape should be returned. If it is a NumPy array, it returns the attribute |
shape — a tuple of integers that are set to the lengths of the corresponding array dimensions.
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 2x2 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
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)
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
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
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
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.
[ [0, 11, 15, 16], [3, 7, 10, 34], [44, 99, 5, 67] ]
[ [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:
[0, 11, 15, 16]
[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.)
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.