Top 9 NumPy Interview Questions
In this article, we will discuss basic and intermediate NumPy Interview Questions in most simplified ways with example.
Introduction
In this article, we will discuss basic and intermediate NumPy Interview Question.
If you are an aspiring data scientist, you are sure to deal with NumPy array-related questions during interviews given the importance of the library during data analytics.
Often, you will be asked to perform simple and straightforward array operations in Python using NumPy. This article will arm you with all the basics of the NumPy library to prepare for the most common questions that come up during Data Science interviews, through Python codes
Best-suited Python courses for you
Learn Python with these high-rated online courses
Table of Content
Quick Intro to NumPy
NumPy (aka Numerical Python) is a third-party open-source Python library used for scientific computations. It offers powerful functions and tools for performing a variety of arithmetic and logical operations on numerical data.
Let’s discuss the common NumPy-related questions you might come across during your interviews.
Basic NumPy Questions
Q1. What are the most common NumPy data types?
A1. NumPy arrays support a number of numerical data types, the most common amongst them are discussed below –
An N-dimensional array type is known as ndarray. All elements stored in a ndarray should be homogeneous i.e., have the same type.
A few of the most common data types are:
- Boolean
- Integer (signed and unsigned)
- Float
- Complex
Q2. How to create NumPy arrays?
A2.
- One-dimensional array – consists of a single row.
import numpy as np #1D Arrayarr1 = np.array([1,2,3])arr1
- Multi-dimensional array – consists of multiple rows and columns, like an Excel spreadsheet, wherein each column can be considered as a dimension.
#2D ArrayX = ([[1.8, 2.5, 3.14], [4, 5, 6]])arr2 = np.array(X)arr2
#3D ArrayY = ([[1,2,3], [4,5,6], [7,8,9]])arr3 = np.array(Y)arr3
Q3. How do you identify the data type of an array?
A3. Through the following command:
print('data type arr1 ', arr1.dtype) print('data type arr2 ', arr2.dtype) print('data type arr3 ', arr3.dtype)
Q4. List the advantages of NumPy over Python lists.
A4. Traditional Python lists have several limitations when compared to NumPy arrays –
Feature | NumPy Arrays | Python Lists |
Data type | Stores homogenous data i.e., data having the same types | Stores heterogenous data i.e., elements can be of different data types |
Flexibility | Less flexible, because operations are performed element-wise |
Allows more flexibility in adding and removing data |
Arithmetic and matrix operations | Many vectors and matrix operations are in-built | Complex statistical and analytical libraries are not available |
Loops | Explicit loop is required to run through the elements | Explicit loop is not required to run through the elements |
Declaration for usage | NumPy module needs to be imported for using NumPy arrays |
No import is required to use Python lists |
Q5. What is negative indexing in a NumPy array?
A5. In a NumPy array, the elements can be accessed in any order as shown:
As you can see, negative indexing refers to indexing from the end of the list starting at -1.
Q6. How do you count the number of times a value appears in a NumPy array of integers?
A6. For this, we will use the bincount() method that accepts only positive integers or Boolean expressions as its arguments.
arr = np.array([1, 7, 1, 1, 7, 6, 5, 1, 3, 3, 2, 0, 0])np.bincount(arr)
Q7. How do you create an array with all values as zeros or ones?
A7. For this, we will use the np.zeros() and np.ones() methods:
#Creating array of zeroszeros_arr = np.zeros((4,4))zeros_arr
#Creating array of zeroszeros_arr = np.zeros((4,4))zeros_arr
Q8. Print an array range between 1 to 35 and show 7 integer random numbers.
rand_arr = np.random.randint(1,35,7)rand_arr
Q9. Print the first, third, and last position of a NumPy array?
rand_arr = np.random.randint(1,35,7)rand_arr
Must Read: What is Python?
Intermediate NumPy Questions
Q1. How to change the data type of a NumPy array?
A1. You can use the astype() method for this:
arr = np.array([2.7, 3.14, 5.89, 7]) new_arr = arr.astype('i') print(new_arr) print(new_arr.dtype)
Q2. How to reverse a 3×3 matrix?
A2.
- First, let’s create a 3×3 matrix:
matrix = np.arange(30,39).reshape(3,3)print(matrix)
- Now, let’s flatten the array:
flat = matrix.flatten()flat
- Let’s reverse the flattened array:
flat = matrix.flatten()flat
- Lastly, let’s reshape the array back to a 3×3 matrix:
reverse.reshape(3,3)
Q3. How to add matrices using NumPy?
A3.
- Let’s first create two matrices:
#Construct two matrices A = ([[1,2,3], [4,5,6,], [7,8,9]]) B = ([[2,4,6], [8,10,12], [14,16,18]])
- Now, just perform simple addition between the two ndarrays:
arr_A = np.array(A) arr_B = np.array(B) result = arr_A + arr_B print(result)
Q4. How to multiply matrices using NumPy?
A4.
- Now, let’s find the dot product of the two ndarrays we created in the above question:
#Dot product of two matrices result = np.dot(arr_A,arr_B) print(result)
- The same result will be obtained when we use np.matmul() for matrix product:
result = np.matmul(arr_A,arr_B) print(result)
- You can also use np.multiply() to return the element-wise matrix multiplication of two ndarrays:
result = np.multiply(arr_A,arr_B) print(result)
Q5. How to find the transpose of the matrix using NumPy?
A5.
#Construct a matrix X = ([[38,13], [4,101], [23,99]]) #Find the transpose of the matrix transpose = np.transpose(X) print(transpose)
Q6. What is array slicing and how do you do it in NumPy?
A6. Slicing means extracting a portion of the given array to generate a new view of the same array without copying it. This is done by specifying the following:
- start: specifies where to start slicing from aka the lower limit
- stop: specifies where to stop slicing aka the upper limit
- step: determines the increment between each index for slicing
#Slicing a 1D array x = np.array([2, 3, 7, 11, 13, 17, 19, 23, 29, 31, 37]) x[1:7:2]
Q7. How do you stack matrices?
A7. NumPy arrays/matrices can be stacked either horizontally or vertically through the following methods:
- np.hstack() method adds the second ndarray to the columns of the first ndarray:
#Construct two matrices A = ([[1,2,3], [4,5,6,], [7,8,9]]) B = ([[2,4,6], [8,10,12], [14,16,18]]) arr_A = np.array(A) arr_B = np.array(B) #Stacking Horizontally np.hstack((arr_A, arr_B))
- np.vstack() method combines the second ndarray as new rows in the first ndarray:
#Stacking Vertically np.vstack((arr_A, arr_B))
Q8. How to swap two rows/columns in a 2D NumPy array?
- create an array first:
arr = np.arange(16).reshape(4,4) arr
- Let’s swap the first and the second rows:
arr[[1,0,2,3], :]
- Let’s swap the first and the second columns of the original array:
arr[:, [1,0,2,3]]
Q9. Create a new array consisting of common values from two given arrays.
We need to find the positions where the elements of the two arrays match:
#Create two 1D arrays a = np.array([1,2,3,2,3,4,3,4,5,6]) b = np.array([1,7,9,2,7,4,3,6,4,6]) np.where(a == b)
Split a given array into four equal-sized sub-arrays.
- For this, let’s create an 8×3 integer array:
arr = np.arange(20,44).reshape(8,3) print(arr)
- Now, you can create 4 sub-arrays using the split() function:
sub_arr = np.split(arr, 4) print(sub_arr)
Conclusion
Hope this article will be helpful for your Data Science interview preparations. You can also find a similar article on Pandas Interview Questions.
Top Trending Articles:
Data Analyst Interview Questions | Data Science Interview Questions | Machine Learning Applications | Big Data vs Machine Learning | Data Scientist vs Data Analyst | How to Become a Data Analyst | Data Science vs. Big Data vs. Data Analytics | What is Data Science | What is a Data Scientist | What is Data Analyst
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio