Numpy is a popular Python library for data science focusing on arrays, vectors, and matrices. If you work with data, you simply cannot avoid NumPy.
Challenge: How to get the number of dimensions of a NumPy array?
Solution: Use the attribute
array.ndim to access the number of dimensions of a NumPy array. Note that this an attribute, not a function.
The one-dimensional array has one dimension:
import numpy as np a = np.array([1, 2, 3]) print(a.ndim) # 1
The two-dimensional array has two dimensions:
import numpy as np a = np.array([[1, 2, 3], [4, 5, 6]]) print(a.ndim) # 2
And the three-dimensional array has three dimensions:
import numpy as np a = np.array([[[1, 2, 3], [4, 5, 6]], [[0, 0, 0], [1, 1, 1]]]) print(a.ndim) # 3
Background: Before we move on, you may ask: What is the definition of dimensions in an array anyways?
Numpy does not simply store a bunch of data values in a loose fashion (you can use lists for that). Instead, NumPy imposes a strict ordering to the data – it creates fixed-sized axes.
Don’t confuse an axis with a dimension. A point in 3D space, e.g.
[1, 2, 3] has three dimensions but only a single axis. You can think of an axis as the depth of your nested data. If you want to know the number of axes in NumPy, count the number of opening brackets
'[' until you reach the first numerical value.
Related Article: NumPy Shape
NumPy Puzzle Dimensionality
Can you solve the following NumPy puzzle that tests what you’ve learned so far?
import numpy as np # salary in ($1000) [2015, 2016, 2017] dataScientist = [133, 132, 137] productManager = [127, 140, 145] designer = [118, 118, 127] softwareEngineer = [129, 131, 137] a = np.array([dataScientist, productManager, designer, softwareEngineer]) print(a.ndim)
Exercise: What is the output of this puzzle?
You can solve it in our interactive puzzle app Finxter.com:
In this puzzle, we use data about the salary of four jobs: data scientists, product managers, designers, and software engineers. We create four lists that store the yearly average salary of the four jobs in thousand dollars for three years 2015, 2016, and 2017.
Then, we merge these four lists into a two-dimensional array (denoted as matrix). You can think about a two-dimensional matrix as a list of lists. A three-dimensional matrix would be a list of lists of lists. You get the idea.
In the puzzle, each salary list of a single job becomes a row of a two-dimensional matrix. Each row has three columns, one for each year. The puzzle prints the dimension of this matrix. As our matrix is two-dimensional, the solution of this puzzle is 2.
Related NumPy Video
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.