Top 9 NumPy Interview Questions

# Top 9 NumPy Interview Questions

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.

## 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

## 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
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
• 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

#3D Array
Y = ([[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 –

### 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 zeros
zeros_arr = np.zeros((4,4))
zeros_arr

#Creating array of zeros
zeros_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

## 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= ([[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= 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= 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)

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.

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
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