The Dimension of a Numpy Matrix

What is the output of this puzzle?

 

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)

 

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. So learn it now and learn it well.

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.


Are you a master coder?
Test your skills now!

Related Video

Solution

2

 

Leave a Comment