Top 9 NumPy Interview Questions

Top 9 NumPy Interview Questions

5 mins read2.6K Views Comment
clickHere
Updated on Oct 3, 2023 12:10 IST

In this article, we will discuss basic and intermediate NumPy Interview Questions in most simplified ways with example.

2022_04_NumPy-Interview-Questions.jpg

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

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.

Top 110+ Python Interview Questions and Answers
Top 110+ Python Interview Questions and Answers
Python is a widely-used general-purpose, object-oriented, high-level programming language. It is used to create web applications, and develop websites and GUI applications. The popularity of the language is due to...read more

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 Array
arr1 = np.array([1,2,3])
arr1
Copy code
2022_04_one-dimensional-array.jpg
  • Multi-dimensional array – consists of multiple rows and columns, like an Excel spreadsheet, wherein each column can be considered as a dimension.
 
#2D Array
X = ([[1.8, 2.5, 3.14],
[4, 5, 6]])
arr2 = np.array(X)
arr2
Copy code
2022_04_two-D-array.jpg
 
#3D Array
Y = ([[1,2,3],
[4,5,6],
[7,8,9]])
arr3 = np.array(Y)
arr3
Copy code
2022_04_three-dimensional-array.jpg

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)
Copy code
2022_04_identify-the-data-type-of-an-array.jpg

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:

2022_04_negative-indexing.jpg

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)
Copy code
2022_04_count-the-number-of-time-a-value-appears.jpg

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 zeros
zeros_arr = np.zeros((4,4))
zeros_arr
Copy code
2022_04_create-an-array-with-all-values-as-zeros.jpg
 
#Creating array of zeros
zeros_arr = np.zeros((4,4))
zeros_arr
Copy code
2022_04_create-an-array-with-all-values-as-ones.jpg

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
Copy code
2022_04_an-array-range-between-1-to-35-and-show-7-integer-random-numbers.jpg

Q9. Print the first, third, and last position of a NumPy array?

 
rand_arr = np.random.randint(1,35,7)
rand_arr
Copy code
2022_04_Print-the-first-third-and-last-position-of-a-NumPy-array.jpg

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)
Copy code
2022_04_change-the-data-type-of-a-NumPy-array.jpg

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)
Copy code
2022_04_create-a-3x3-matrix.jpg
  • Now, let’s flatten the array:
 
flat = matrix.flatten()
flat
Copy code
2022_04_flatten-the-array.jpg
  • Let’s reverse the flattened array:
 
flat = matrix.flatten()
flat
Copy code
2022_04_reverse-the-flattened-array.jpg
  • Lastly, let’s reshape the array back to a 3×3 matrix:
 
reverse.reshape(3,3)
Copy code
2022_04_reshape-the-array-back-to-a-3x3-matrix.jpg

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)
2022_04_add-matrices-using-NumPy.jpg

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)
2022_04_use-np.matmul-for-matrix-product.jpg
  • The same result will be obtained when we use np.matmul() for matrix product:
result = np.matmul(arr_A,arr_B)
print(result)
2022_04_use-np.matmul-for-matrix-product.jpg
  • 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)
2022_04_element-wise-matrix-multiplication-of-two-ndarrays.jpg

Q5. How to find the transpose of the matrix using NumPy?

A5.

#Construct a matrix= ([[38,13],
    [4,101],
    [23,99]])
 
#Find the transpose of the matrix
transpose = np.transpose(X)
print(transpose)
2022_04_Transpose-of-a-matrix.jpg

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= np.array([2, 3, 7, 11, 13, 17, 19, 23, 29, 31, 37]) 
x[1:7:2] 
2022_04_array-slicing.jpg

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))
2022_04_stack_hp_stack.jpg
  • np.vstack() method combines the second ndarray as new rows in the first ndarray:
#Stacking Vertically
np.vstack((arr_A, arr_B))
2022_04_stack_vp_stack.jpg

Q8. How to swap two rows/columns in a 2D NumPy array?

  • create an array first:
arr = np.arange(16).reshape(4,4)
arr
2022_04_swap_create-an-array.jpg
  • Let’s swap the first and the second rows:
arr[[1,0,2,3], :]
2022_04_swap_first-second-row.jpg
  • Let’s swap the first and the second columns of the original array:
arr[:, [1,0,2,3]]
2022_04_swaP_first-second-colum.jpg

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= np.array([1,2,3,2,3,4,3,4,5,6])= np.array([1,7,9,2,7,4,3,6,4,6])
 
np.where(== b)
2022_04_image-68.jpg

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)
 
2022_04_image-69.jpg
  • Now, you can create 4 sub-arrays using the split() function:
sub_arr = np.split(arr, 4) 
print(sub_arr)
2022_04_image-70.jpg

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

Getting started with Python Strings
Getting started with Python Strings
Python strings are immutable, i.e., you cannot modify the contents of the object. In this article, we will briefly discuss how to create, assign string to a variable, how to...read more
Array Manipulation Using NumPy
Array Manipulation Using NumPy
NumPy is a foundation library for scientific computations in Python, literally standing for Numerical Python. It contains sophisticated functions and tools for integrating with other programming languages as well. During...read more
Slicing in Python
About the Author

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

Comments