Python Numpy and Matrices Questions for Data Scientists | by Leihua Ye, PhD | Towards Data Science

The complete Python code is available on my Github.
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.
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).
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.
# step 1: numpy array
# Aarray([[0, 1, 2],       [3, 4, 5],       [6, 7, 8]])# Barray([[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.
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 rowsfor 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 YZ[i][j] += X[i][k]*Y[k][j]: fill in the values in Z by the sums of element-wise multiplication.
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 and here.
How to transpose a matrix?
Again, there are multiple solutions depends on whether you are allowed to use Numpy.
# step 1: create matrices
# step 2: nested for loop
[12, 1, 3][7, 2, 4]
# 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)
If we can use Numpy, a one-liner can get the job done.
array([[12,  1,  3],       [ 7,  2,  4]])
Alternatively, we can change the matrix (list) into a Numpy array and use array.T.
How to add two Numpy matrices together?
Again, there are two solutions depends on whether we can use Numpy.
[[17 15  4] [10 12  9] [11 13 18]]
Numpy handles element-wise addition with ease.
[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.
array([[12,  7,  3],       [ 4,  5,  6],       [ 7,  8,  9],       [ 5,  8,  1],       [ 6,  7,  3],       [ 4,  5,  9]])
array([[12,  7,  3,  5,  8,  1],       [ 4,  5,  6,  6,  7,  3],       [ 7,  8,  9,  4,  5,  9]])
The complete Python code is available on my Github.
Good communication during the interview process.
Ask for clarification questions and what constraints are. e.g., can you use Numpy arrays?
To flatten matrix out before trying to reverse it.
Nested for loops are useful. Try to understand how each line works.
Medium recently evolved its Writer Partner Program, which supports ordinary writers like myself. If you are not a subscriber yet and sign up via the following link, I’ll receive a portion of the membership fees.
leihua-ye.medium.com
towardsdatascience.com
towardsdatascience.com
towardsdatascience.com
Please find me on LinkedIn and Youtube.
Also, check my other posts on Artificial Intelligence and Machine Learning.