Python Numpy and Matrices Questions for Data Scientists
By: Leihua Ye, Ph.D. Researcher
Tags: Python, Interview, Data Science, Programming, Matrix

I’ve been preparing for Data Science interviews for a while, and there is one thing that struck me the most is the lack of preparation for Numpy and Matrices questions.

Often, Data Scientists are asked to perform simple matrix operations in Python, which should be straightforward but, unfortunately, throw a lot of candidates off the bus!

Me included!

One time, I was asked by a FAANG company to perform a multiplication of two matrices, which I didn’t know back in time.

I find the best way of preparing these types of interviews is to find a niche area and write a post on the topic. It’s a win-win situation for me and my fellow readers.

So, here it goes. In this post, I walk through 4 Numpy/Matrices questions that often come up in DS interviews and code it up in Python.

Photo by Brittany Bendabout on Unsplash

Question 1:

Given a 4x4 Numpy matrix, how to reverse the matrix?

# step 0: construct a 4*4 Numpy matrix

[[ 1 2 3 4]

[ 5 6 7 8]

[ 9 10 11 12]

[13 14 15 16]]

A side-note, np.arange(1,17) returns a Numpy array, and reshape(4,4) generates a 4*4 matrix.

# step 1: to flatten the numpy array

array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])

There are two ways to flatten a matrix depending on the data type. For Numpy arrays, we use np.array.flatten() command; for non-array matrices, we use matrix.ravel(). Please try it out.

# step 2: read the elements backward into a new matrix

array([16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1])

After flattening out, we read the matrix backwards starting from the last element.

# step 3: reshape the matrix back into a 4*4 matrix

Last, we reshape the matrix and turn it back into a 4*4 matrix, as shown:

array([[16, 15, 14, 13],

[12, 11, 10, 9],

[ 8, 7, 6, 5],

[ 4, 3, 2, 1]])

The unique attribute about Numpy arrays or matrices is that we have to flatten it out before reversing it, and there are two ways of doing so depends on the data format (Numpy or not).

Photo by Pete Walls on Unsplash

Question 2:

How do you multiply two matrices?

This type of question could be tricky!

Two matrices? Numpy or not?

The solution largely depends on the data type that it refers to. Overall, there are two solutions: the dot method for Numpy arrays and nested for loops for non-arrays.

Solution 1: Numpy arrays

# step 1: numpy array

# A

array([[0, 1, 2],

[3, 4, 5],

[6, 7, 8]]) # B

array([[10, 11, 12],

[13, 14, 15],

[16, 17, 18]]) # Step 2: dot method

[[ 45 48 51]

[162 174 186]

[279 300 321]]

The one-liner dot method easily solves the multiplication question for Numpy arrays, but I doubt interview questions would be so easy.

So, it’s more likely to do multiplication questions for non-arrays.

Solution 2: Non-Arrays

Step 0: construct two matrices.

Python does not have a built-in data type for matrix, and we use a list of a list (nested lists) instead.

# step 1: create a zero matrix to store results

The reason why we create an empty matrix is to store the multiplication results in the following nested for loop.

# step 2: nested for loop

[[14, 20, 31], [21, 30, 46], [21, 30, 44]]

Let’s disentangle the nested for loops:

for i in range(len(X)): returns the number of rows for j in range(len(Y[0])): returns the number of columns of Y. A side-note: we use Y[0] to access matrix columns and Y to access matrix rows. for k in range(len(Y)): to iterate rows of Y Z[i][j] += X[i][k]*Y[k][j]: fill in the values in Z by the sums of element-wise multiplication.

Recall, the sequence of two matrices multiplication: the elements in the first row from X multiply the elements in the first column from Y, and we add the sums up. Repeat the process until the end.

The above nested loop simply follows the same procedure of calculating matrix multiplication as we normally do. A quick refresh of linear algebra here.

Photo by Jonny Gios on Unsplash

Question 3:

How to transpose a matrix?

Again, there are multiple solutions depends on whether you are allowed to use Numpy.

Solution 1: nested for loop (don’t use Numpy)

# step 1: create matrices

# step 2: nested for loop

[12, 1, 3]

[7, 2, 4]

Solution 2: using zip() for a list of tuples (don’t use Numpy)

# step 1: create a list of tuples

[(1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12)]

We use a list of tuples to create a matrix.

# step 2: unzip and zip in one line

(1, 4, 7, 10)

(2, 5, 8, 11)

(3, 6, 9, 12)

# Solution 3: use Numpy array

3.1 np.transpose()

If we can use Numpy, a one-liner can get the job done.

3.2 np.array.T

array([[12, 1, 3],

[ 7, 2, 4]])

Alternatively, we can change the matrix (list) into a Numpy array and use array.T.

Photo by Eduardo Drapier on Unsplash

Question 4:

How to add two Numpy matrices together?

Again, there are two solutions depends on whether we can use Numpy.

Solution 1: np.array()

[[17 15 4]

[10 12 9]

[11 13 18]]

Numpy handles element-wise addition with ease.

Solution 2: nested for loops for ordinary matrix

[17. 15. 4.]

[10. 12. 9.]

[11. 13. 18.]

The code is pretty self-evident, and we have covered them all in the above questions.

On a related note, there are two variations of the question.

4.1 How to stack matrices vertically?

array([[12, 7, 3],

[ 4, 5, 6],

[ 7, 8, 9],

[ 5, 8, 1],

[ 6, 7, 3],

[ 4, 5, 9]])

4.2 How to stack matrices horizontally?