NumPy Interview Questions and Answers 2022

Numpy Interview Questions and Answers

NumPy(Numerical Python) is an open-source library of Python. It is designed to perform complex mathematical, image processing, quantum computing, and statistical operations, etc., on matrices and multidimensional arrays. The NumPy N-dimensional arrays are used to perform indexing, slicing, splitting, joining, reshaping, and other complex manipulations.

 

These robust in-built functions make NumPy a great choice for data science projects. As the demand for data scientists and jobs in machine learning is rising, it is a wise choice to learn NumPy and carve a golden career path along with it.

 

Basic NumPy Interview Questions and Answers

  1. Why do developers prefer NumPy to similar tools like Matlab, Yorick?
  2. List features that make NumPy unique?
  3. Share the steps of NumPy installation in Windows?
  4. Why is NumPy Array good compared to Python Lists?
  5. Lists steps to create 1D, 2D, and 3D array.
  6. How can you identify the datatype of a given NumPy array
  7. What does bincount() function?
  8. What is the use of "ndim" attribute in NumPy?
  9. What is the use of "flipud" function in NumPy?
  10. How can you reshape NumPy array?
  11. Name a few use cases where NumPy is useful.
  12. How many dimensions can a NumPy array have?
  13. What is negative indexing in NumPy arrays?
  14. How are NumPy Arrays better than Lists in Python?
  15. Explain the data types supported by  NumPy.

 

Intermediate NumPy Interview Questions and Answers

  1. How does the flatten function differs from the ravel function?
  2. What caused the Overflow error in NumPy?
  3. How does NumPy differ from Pandas?
  4. How do you calculate the moving average?
  5. What is the difference between indexing and slicing in NumPy?
  6. Can you create a plot in NumPy?
  7. Is this possible to calculate the Euclidean distance between two arrays?
  8. Discuss steps to deal with array objects?
  9. Which three types of value are accepted by missing _value argument?
  10. How can you check whether any NumPy Array has elements or is empty?
  11. Explain the operations that can be performed in NumPy.
  12. Why is the shape property used in NumPy?
  13. What is array slicing in NumPy?
  14. How is the seed function used in NumPy?
  15. How to convert the data type of an array in NumPy?
  16. What is the difference between copy and view in NumPy?

 

Advanced NumPy Interview Questions

  1. Discuss vectorisation in NumPy? How to perform visualisation using NumPy?
  2. Discuss uses of vstack() and hstack() functions?
  3. How is vectorisation different from broadcasting?
  4. How can you find peak or local maxima in a 1D array?
  5. What are different ways to convert a Python dictionary to a NumPy array?
  6. What is the best way to create a Histogram?
  7. Can you create strides from a 1D array?
  8. How does NumPy handle numerical exceptions?
  9. What is the biggest challenge while writing extension modules in NumPy?
  10. What is the use of SWIG method in NumPy?

Final Thoughts

 

Let's get into the details and review answer to each of the questions above.

 

Basic NumPy Interview Questions and Answers

 

1. Why do developers prefer NumPy to similar tools like Matlab, Yorick?

Developers prefer to use NumPy over other tools like Matlab for the following reasons:

  • NumPy is an accessible and open-source library. You can easily access it and use it anywhere.
  • It is a high-performing library integrated with multidimensional arrays and matrics. Some popular libraries
    • For Stats and ML: SciPy, Scikit-Learn, SpaCy, Statsmodels
    • Array Manipulation: Dask, PyTorch, TensorFlow
  • Visualisation: Seaborn, Matplotlib, Altair, Bokeh, Plotly
  • These libraries permit developers to quickly perform high-performance and complex mathematical, science, or finance calculations.
  • As a developer, you can take advantage of the general purpose feature of Python as it makes it easy for you to connect with programming languages like C, C++, or Fortran.


Explore our careers section for NumPy Job Openings

 

2. List features that make NumPy unique?

Here are specific features which make NumPy unique:

  • N-dimensional array object, NumPy array allows us to create a fixed size array while creating codes. But, you can change the size at runtime in case of need by deleting the old one and starting a new one. You can create different size elements of the array.
  • Vectorised codes allow you to design and develop concise codes, including explicit looping, indexing, or lengthy codes. It makes the code readable and easy to execute with fewer bugs.
  • It can quickly perform complex functions like broadcasting, and high-level mathematical functions such as statistics, algebra, Fourier transformation, and other scientific calculations.
  • It is compatible to use NumPy in various computing platforms with dissimilar hardware.
  • You can easily use NumPy with multiple sparse and distributed array libraries.

 

3. Share the steps of NumPy installation in Windows?

Installation of NumPy in Windows is relatively easy. You need to follow the below steps:

  • Download and install Python (You can visit the official page of Python and get the executable installer file)
  • Open and run that executable installer file
  • Install the "pip" library on your Windows
  • Now, to install NumPy in Python, follow the following steps
  • Start the terminal (cmd screen)
  • Type "pip install NumPy"

In this way, you can install NumPy and use it for various purposes.

 

4. Why is NumPy Array good compared to Python Lists?

NumPy is better than Python Lists for two primary reasons:

  • NumPy Array is static and has a fixed size while creating codes. In other cases, Python Lists are dynamic and can grow dynamically.
  • NumPy Array can perform vectorised operations and other advanced calculations, but Python Lists can't do these even after having a large set of functions.

 

5. Lists steps to create 1D, 2D, and 3D array.

You can create an array using "array" function/object class or a regular Python List. It is good to use the "array" function. Let's check the steps for creating a 1D, 2D, and 3D array:

Common Syntax

  • Import "numpy" library
  • Define required array like 1D, 2D, or 3D into any variable
  • Call the function, array() and pass that variable
  • Print and store the generated value

 

1D array Code:

import numpy as np
n =[10,20,30]
n = np.array(n)
print("1D array :",n)

1D array Output:

1D array : [10 20 30]

 

2D array Code:

import numpy as np
m = [[10,20,30], [40, 50, 60]]
m = np.array(m)
print("2D array :", m)

2D array Output:

2D array : [[10 20 30]
 [40 50 60]]

 

3D array Code:

import numpy as np
p = [[10,20,30], [40, 50, 60], [70,80,90]]
p = np.array(m)
print("3D array :", p)

3D array Output:

3D array : [[10 20 30]
 [40 50 60]
 [70 80 90]]

 

6. How can you identify the datatype of a given NumPy array

You can determine the datatype of a given NumPy array using "dtype" property. It tells you what type of values are stored in the array.

Syntax: array object.dtype

 

In the following example

Code:

import numpy as np
p = [[10,20,30], [40, 50, 60], [70,80,90]]
q = 3.4
p = np.array(p)
q = np.array(q)
print("Datatype:", p.dtype)
print("Datatype:", q.dtype)

Output:

Datatype: int64
Datatype: float64

 

It can be seen that the compiler displays "int64" as the datatype for the p that has been initialized with integer values.


Test your NumPy coding skills here

 

7. What does bincount() function?

The main job of the bincount() function is to count the number of times a given value appears in the array of integers. You have to take care of one point that the function takes only positive integers or Boolean expressions as an argument. You can not pass negative integers.

Syntax: array object.bincount()

 

Example:

Code:

import numpy as np
p = [10,20,30]
q = [5,1,7,1,8,13]
s = np.array([0, 2, 3, 0, 3, 3, 3, 0, 0, 4, 2, 1, 7, 5])
p = np.bincount(p)
q = np.bincount(q)
print("Occurrence of each digit in p:", p)
print("\nOccurence of each digit in q:", q)
print("\nOccurence of each digit in q:", np.bincount(s))

Output:

Occurrence of each digit in p: [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1]
Occurence of each digit in q: [0 2 0 0 0 1 0 1 1 0 0 0 0 1]
Occurence of each digit in q: [4 1 2 4 1 1 0 1]

 

Let's take an example of array "q" in which 6 elements have been defined. You can see respective outcome where bincount() function returned [0, 2, 0, 0…]; it means occurrence of digits (0,1,2, 3..) sequentially.

 

8. What is the use of "ndim" attribute in NumPy?

The "ndim" function helps you to get the number of dimensions of the array.

Syntax: array object.ndim(); You have input array and in case if it is not, convert it.

 

Example:

Code:

import numpy as np
p = [[10,20,30], [40, 50, 60], [70,80,90]]
q = [10,20,30]
p = np.array(p)
q = np.array(q)
print("Dimension of array p:", p.ndim)
print("Dimension of array q:", q.ndim)

Output:

Dimension of array p: 2
Dimension of array q: 1

 

The outcome is showing the dimensions of arrays like array p has dimension 2 whereas array q has 1.

 

9. What is the use of "flipud" function in NumPy?

The "flipud" function reverses the array elements row-wise or along axis = 1 and axis = 1 respectively.

Syntax: array object.flipud() or flipud(array)

 

Example:

Code:

import numpy as np
array1 = [[10,20,30], [40, 50, 60], [70,80,90]]
print("Original array1 : \n", array1)

array2 = ['x','y','z']
print("Original array2 : \n", array2)
# flipud : means flip up-down

print("\nFlipped Array Example - array1 : \n", np.flipud(array1))
print("\nFlipped Array Example - array2 : \n", np.flipud(array2))

Output:

Original array1 : 
 [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
Original array2 : 
 ['x', 'y', 'z']

Flipped Array Example - array1 : 
 [[70 80 90]
 [40 50 60]
 [10 20 30]]

Flipped Array Example - array2 : 
 ['z' 'y' 'x']

 

The output shows that both arrays have been reversed, like x,y,z is reversed into z,y,x.

 

10. How can you reshape NumPy array?

You can reshape the NumPy array using the function, reshape(). The function creates a new array rather than changing the original array. The function takes tuples which specify the new dimensions.

Syntax: array object.reshape() or reshape(array)

 

Example:

Code:

import numpy as np
array1 = np.array([10,20,30,40])
print("Original array1 : \n", array1)

array2 = np.array(['x','y','z','d','f','g'])
print("Original array2 : \n", array2)

# reshape : reshape the array like number of array, rows, columns

print("\n Reshape - array1 : \n", array1.reshape(1,2,2))
print("\n Reshape - array2 : \n", array2.reshape(1,2,3))

Output:

Original array1 : 
 [10 20 30 40]
Original array2 : 
 ['x' 'y' 'z' 'd' 'f' 'g']

 Reshape - array1 : 
 [[[10 20]
  [30 40]]]

Reshape - array2 : 
 [[['x' 'y' 'z']
  ['d' 'f' 'g']]]

 

Based on the output, you can see that reshape function creates arrays, rows, and columns out of the data. We have reshaped array1 into one array with two rows and two columns.

 

NumPy is an important library of Python. Read more about Python here.

 

11. Name a few use cases where NumPy is useful.

  • To perform complex mathematical computations on arrays
  • To use multi-dimensional arrays and matrices in operations
  • To execute trigonometric, statistical, and algebraic functions
  • To execute transforms and methods for shape manipulation
  • To generate random numbers
  • To add/ delete/ sort/ split arrays

Read in detail here.

 

12. How many dimensions can a NumPy array have?

In NumPy, an array can have N-dimensions, and is called a ndarray.

  • Ndarray is a multidimensional container which contains elements of the same type and size.
  • The number of dimensions in a ndarray is also known as ‘rank’.
  • A tuple of integers called ‘shape’ defines the size of the array in each dimension.
  • The data type of elements in ndarray is defined by a  ‘dtype’ object.

 

NumPy Array Dimensions

 

Example:

2D ndarray with size 2X3. 

example = np.array([[1, 2, 3], [4, 5, 6]], np.int32) 
type(example) 
<class 'numpy.ndarray'> 
example.shape(2, 3) 
example.dtype 
dtype('int32')

 

13. What is negative indexing in NumPy arrays?

Usually, the indices start from zero(0), continuing to 1,2,3, and so on

 

NumPy Arrays

 

However in NumPy the elements can be accessed starting from the end of the array too. Negative indexing refers to the act of indexing from the end of the list starting at -1.

 

NumPy Arrays Negative Indexing

 

Example: 
Print the last element from the 2nd dimension: 

import numpy as np 

example = np.array([[1,2,3,4,5], [6,7,8,9,10]]) 
print('Last element from 2nd dim: ', example[1, -1]) 

Output
10

 

14. How are NumPy Arrays better than Lists in Python?

Feature NumPy Arrays Python Lists
Element data type Stores homogeneous data i.e., the elements are of the same data type The elements can be of different data types
Flexibility Less flexible, because operations are performed element wise Allows more flexibility in adding, removing data
Declaration for usage Numpy module needs to be imported for using Numpy arrays No import is required to use Python lists
Arithmetic and matrix operations Many vector 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

 

15. Explain the data types supported by  NumPy.

  • numpy.bool_ : bool
  • numpy.byte : signed char
  • numpy.ubyte : unsigned char
  • numpy.short : short
  • numpy.ushort : unsigned short
  • numpy.intc : int
  • numpy.uintc : unsigned int
  • numpy.int_ : long
  • numpy.uint : unsigned long
  • numpy.longlong :  long long
  • numpy.ulonglong : unsigned long long
  • numpy.half / numpy.float16 : Half precision float
  • numpy.single : float
  • numpy.double : double
  • numpy.longdouble : long double
  • numpy.csingle : float complex
  • numpy.cdouble : double complex
  • numpy.clongdouble : long double complex

 

More details and examples are here.


Explore our careers section for NumPy Job Openings

 

Intermediate NumPy Interview Questions and Answers

 

16. How does the flatten function differs from the ravel function?

Flatten function has been used for generating 1D versions of the multi-dimensional array. The ravel function does the same operation, but both have a difference.

 

The flatten() function always returns a copy. The ravel() function returns a view of the original array in most cases. Although you can't see or analyse it in the shown output, if you make changes in the array returned by ravel(), it might change the whole data of the array. This does not happen while using flatten() function.

 

Additionally, you can use the ravel function with any easily parseable object but flatten() function is used with true NumPy arrays.

 

And, last, ravel() is faster than flatten().

 

Syntax: array object.flatten() and array.object.ravel()

 

Example:

Code:

import numpy as np
p = [[10,20,30], [40, 50, 60], [70,80,90]]
p = np.array(p)
print("3D array\n :", p)
print("After applying the flatten() function\n")
print(p.flatten())
print("After applying the ravel() function\n")
print(p.ravel())

Output:

3D array
 : [[10 20 30]
 [40 50 60]
 [70 80 90]]
After applying the flatten() function

[10 20 30 40 50 60 70 80 90]

After applying the ravel() function

[10 20 30 40 50 60 70 80 90]

 

There is no difference in output between the two functions. It proves that it has invisible differences to users.

 

17. What caused the Overflow error in NumPy?

The fixed size of NumPy numeric types causes an overflow error when a value needs more memory than available in the data type. In other words, you use a value too large to fit in the required place.

 

For instance, numpy.power evaluates 100**8 accurately for 64-bit integers but returns 187491924 (incorrect) for a 32-bit integer.

 

The behaviour of NumPy integer types differs significantly for integer overflows and might confuse users.

 

18. How does NumPy differ from Pandas?

NumPy Pandas
It is a fundamental library of Python. It is an open-source and BSD-licensed library written in Python language.
It has a set of tools like multidimensional arrays for performing scientific computation and other complex functions. It provides a set of data analysis tools, time series, to perform multiple operations on data.
It works on numeric data and you can call it, "numpy module". It works on tabular data, and you can call it, "pandas module".
It is also fast but memory efficient. It is fast but consumes more memory.
It supports fewer applications compared to Pandas. It supports broader and large industrial applications.

 

19. How do you calculate the moving average?

Before knowing how to calculate moving average, know what it means. It refers to a series of averages of fixed-size subsets of the total observation sets. You can also call it running average, rolling average, or rolling means.

 

The number of observations and size of windows are required for this calculation. You can calculate the moving average using several methods.

 

Using convolve function is the simplest method which is based on discrete convolution. You have to use a method that calculates discrete convolution to get a rolling mean. You can convolve with a sequence of np.ones of a length equal to the desired sliding window length.

 

Syntax: array object.convolve(data, window size)

 

Example:

Code:

import numpy as np
def moving_average(x, wsize):
    return np.convolve(x, np.ones(wsize), 'valid') / wsize
data = np.array([1,3,2,7,4,8,2])
print("\n Moving Average:")
print(moving_average(data,2))

Output:

Moving Average:
[2.  2.5 4.5 5.5 6.  5. ]

 

Since the window size is 2, thus first moving average would be (1+3/2) i.e. 2. Similarly, the next values would be calculated during the moving average calculation.

 

20. What is the difference between indexing and slicing in NumPy?

The indexing and slicing are both applied to the array. Let’s see some differences below:

  • Indexing creates an index, whereas slicing makes a copy. You need to use an array as an index to execute indexing. You can also index the Numpy array with other arrays or sequences with some tuple exceptions.
  • Slicing returns a view (shallow copy) of the array, whereas indexing returns an original array.
  • Different types of indexing are possible, but there is no slicing category.

 

Example:

Code:

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
print("\nIndexing in array")
print("\nfifth element:", arr[4])
print(arr[5])
print("\nSlicing in array")
print(arr[1:3])

Output:

Indexing in array

fifth element: 5
6

Slicing in array
[2 3]

 

Here, indexing is returning the array value at specific index like array[4] returns fifth element whereas slicing is returning a specific subset.


Test your NumPy coding skills here

 

21. Can you create a plot in NumPy?

Yes, you can create a plot in NumPy. The "matplotlib" is a scientific plotting library in Python. You can use this library with NumPy, which creates an effective and open-source environment for creating plots. Often, function arrange(), and pyplot submodules has been used for making various plots.

 

Check the below example code, which is plotting a line graph. You have import, "matplotlib" compulsory. A line is drawn between two points thus, two arrays have been used as xpoints and ypoints. After that, these values have been passed to the plot() function. You can see a line in the image.

 

Example:

Code:

import sys
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([0,5])
ypoints = np.array([0,150])
plt.plot(xpoints, ypoints)
plt.show()
plt.savefig(sys.stdout.buffer)
sys.stdout.flush()

Output:

create a plot in numpy

 

Can you create a plot in NumPy

Image Credit: W3schools

 

22. Is this possible to calculate the Euclidean distance between two arrays?

Yes, it is possible to calculate the Euclidean distance between two arrays. You can use "linalg.norm()" for this. The main job of this function is to preserve float input values even for scalar input values. That's why it is used for calculating distance.

 

Example:

Code:

import numpy as np
a = np.array([4,2,1,6])
b = np.array([1,3,8,2])
#Calculating distance
dist = np.linalg.norm(a-b)
print("\n Distance: ", dist)

Output:

Distance:  8.660254037844387

 

23. Discuss steps to deal with array objects?

Yes, it is possible to deal with array objects. Follow the steps:

  • First, use a well-defined array with the correct type and dimensions. You can use PyArray_FromAny or macro to convert it from some Python object. And then use PyArray_NewFrom Descr to construct a new ndarray in desired shape and type.
  • Now get the shape of the array and pointer to its actual data
  • Pass this data and shape information onto a subroutine or other section of code which performs the computation
  • In the case of writing an algorithm, use stride information present in the array for accessing array elements.

All these steps would help you to deal any array objects.

 

24. Which three types of value are accepted by missing _value argument?

The missing_value accepts the following three values

  • A single value: It will be the default for all columns
  • A sequence of value: Each entry will be the default for the corresponding column
  • A dictionary: Each key can be a column index or a column name; the corresponding value should be a single object

Note down that you can use a special key, "None" to define a default for all columns.

 

25. How can you check whether any NumPy Array has elements or is empty?

Yes, it is possible to check the emptiness of NumPy Array using multiple methods. But, check the following table to know the two simplest functions to check the emptiness of an array.

numpy.any() An array element is evaluated as True if it lies along any given axis. Simply put, it returns TRUE if the array has elements otherwise, FALSE.
numpy.size() The function returns size of array. If the return value is zero, then the array is empty.

 

Example:

Code:

import numpy as np 
A1 = np.array([12,13,14,15,16,17,18,19])
print("Array with elements, A1:", A1) 
A2 = np.array([])
print("Array without elements:A2", A2) 

#Method 1 -any()
print("Empty Array or not:A1 ", np.any(A1)) 
print("Empty Array or not:A2", np.any(A2)) 
#Method 2 - size()
print("Size function:A1 ", np.size(A1)) 
print("Size function:A2 ", np.size(A2))

Output:

Array with elements, A1: [12 13 14 15 16 17 18 19]
Array without elements:A2 []
Empty Array or not:A1  True
Empty Array or not:A2 False
Size function:A1  8
Size function:A2  0

 

It is visible that A1 array has elements and A2 does not. The function any() returns FALSE for A2 array means it is empty. Similarly, size() functions returns 0 for A2 means again, there is no elements inthe array.

 

You will perform better in your interview if you practice backend development skills like .NET, API, and OOPs concepts. Get sharper at backend development by clicking here.

 

26. Explain the operations that can be performed in NumPy.

Operations on 1-D arrays in NumPy:

  • Add: Adds the elements of two arrays, depicted by ‘+’.
Example: 

array1= np.array([1,2,3,4,5]) 
array2= np.array([1,2,3,4,5]) 
array3= array1 + array2 
array3 

Output - array([1,4,6,8,10])  
  • Multiply: Multiplies the elements with a number, depicted by ‘*’.
  • Power: Square the elements in an array by using ‘**’.
Example: 

array1**2 
array1 

Output - array1([1,4,9,16,10])

 

To use a higher power such as cube, use below syntax.

np.power(array1,3) 

array1 

Output - array1([1,8,27,64,1000])

 

  • Conditional Expressions: Compare the array elements using a conditional expression.
Example: 

array5 = array1 >= 3 

array5 

Output - array([False,False, True, True , True])

 

Operations on 2-D arrays in NumPy:

  • Add: Adds elements of 2-D arrays by position.
Example: 

A = np.array([[3,2],[0,1]]) 

B = np.array([[3,1],[2,1]]) 

A+B gives output as- 

array([[6, 3], 

 [2, 2]])

 

  • Normal multiplication: Multiples the arrays element-wise.
Example: 

A*B gives output as- 

array([[5, 4], 

[2, 3]])  
  • Matrix Multiplication: Uses ‘@’ to perform matrix product/multiplication.
Example: 

A@B gives output as- 

array([[13,  5], 

[ 2,  1]])

 

27. Why is the shape property used in NumPy?

The shape property is used to get the number of elements in each dimension of a  NumPy array.

Example: 

example = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) 

print(example.shape) 

 

Output: 

(2,4)  

 

This means there are 2 dimensions and each dimension has 4 elements.

 

28. What is array slicing in NumPy?

Through slicing a portion of the array is selected, by mentioning the lower and upper limits. Slicing creates views from the actual array and does not copy them.

 

Syntax:

The basic slice syntax is i:j:k where i is the starting index, j is the stopping index, and k is the step (k≠0).

Example 1-D array: 

x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) 

x[1:7:2] 

 

Output 

array([1, 3, 5]) 

Example 2-D array: 

a2 = np.array([[10, 11, 12, 13, 14], 

               [15, 16, 17, 18, 19], 

               [20, 21, 22, 23, 24], 

               [25, 26, 27, 28, 29]]) 

 

print(a2[1:,2:4])

This starts from row 1:(till the end of the array) and columns 2:4(2 and 3).

 

Array Slicing in NumPy
Image credit: pythoninformer.com
 

Example 3-D array: 

a3 = np.array([[[10, 11, 12], [13, 14, 15], [16, 17, 18]], 

               [[20, 21, 22], [23, 24, 25], [26, 27, 28]], 

               [[30, 31, 32], [33, 34, 35], [36, 37, 38]]]) 

 

print(a3[:2,1:,:2])

 

This statement makes a selection as shown below:

  • Planes: first 2 planes
  • Rows:  last 2 rows
  • Columns: first 2 columns

numpy 3d arrays

 

29. How is the seed function used in NumPy?

  • The seed function sets the seed(provides input) of a pseudo-random number generator in NumPy.
  • By pseudo-random we mean that the numbers appear as randomly generated but actually are predetermined through algorithms.
  • The numpy.random.seed or np.random.seed function can not be used alone and is used together with other functions.
Example: 

# seed random number generator 

seed(1) 

# generate some random numbers 

print(rand(3))

 

30. How to convert the data type of an array in NumPy?

The data type(dtype) of an array can be changed using the function numpy.astype().

Example: Change the array of floats to an array of integers. 

arr = np.array([1.3, 2.2, 3.1]) 

newarr = arr.astype('i') 

print(newarr) 

print(newarr.dtype) 

Output: 

[1 2 3] 

int32

 

31. What is the difference between copy and view in NumPy?

Copy

  • Returns a copy of the original array.
  • Do not share the data or memory location with the original array.
  • Any modifications made in the copy will not get reflected in the original.
Example: 

import numpy as np 

arr = np.array([20,30,50,70]) 

a= arr.copy() 

#changing a value in copy array 

a[0] = 5 

  

print(arr) 

print(a) 

 

Output: 

[20 30 50 70] 

[ 5 30 50 70]

 

View

  • Returns a view of the original array.
  • Does use the data and memory location of the original array.
  • Any modifications made in the copy will get reflected in the original.
Example: 

import numpy as np 

arr = np.array([20,30,50,70]) 

a= arr.view() 

#changing a value in original array 

arr[0] = 100 

 

print(arr) 

print(a) 

 

Output: 

[100 30 50 70] 

[100 30 50 70]


Explore our careers section for NumPy Job Openings

 

Advanced NumPy Interview Questions

 

32. Discuss vectorisation in NumPy? How to perform visualisation using NumPy?

Vectorisation is a vital feature in NumPy. It says that one function could apply to all array elements. This means it needs only one elemental operation to vectorise any function. Executing some operations like loops on the array is time-consuming in Python because of different data types. You all know that C languages support one specific datatype, making the code more optimised and fast. NumPy arrays support a single datatype, and most of its functions like logical, and arithmetic have optimised code. Thus you can easily vectorise your functions.

Steps:

  • Write the function which will perform the required operation. It must take array elements as parameters.
  • Use vectorise () method available in the NumPy package and vectorise this function.
  • Now, input the array to this vectorised function.

 

33. Discuss uses of vstack() and hstack() functions?

vstack() and hstack() are the vector stacking functions. Both are used to merge NumPy arrays. As the name says

  • vstack() - Vertical stacking means it merges the array vertically.
  • hstack() - Horizontal stacking means it merges the array horizontally.

Let’s understand it with the following example.

Example:

Code:

import numpy as np
a = np.array([10,20,30])
b = np.array([40,50,60])
# vstack and hstack functions
vs = np.vstack((a,b))
print("\n Vertical Stacking\n ", vs)
print("-------------")
hs = np.hstack((a,b))
print("\n Horizontal Stacking\n ", hs)

Output:

Vertical Stacking
  [[10 20 30]
 [40 50 60]]
-------------

 Horizontal Stacking
  [10 20 30 40 50 60]

 

34. How is vectorisation different from broadcasting?

Vectorization Broadcasting
Vectorization do vectorize the functions for executing complex operations such as loops. Broadcasting is an extension of vectorization which vectorizes the array operations.
In vectorization, it is compulsory to have array same size. In broadcasting, array could be differ in sizes to perform any operations including addition,
subtraction, etc.
It depends on NumPy operations to optimize the C language functions internally to generate the code quickly. It is itself are separate methods which permits NumPy to execute array-related arithmetic operations.

You must perform broadcasting before vectorization to vectorize array operations of different dimensions.

 

35. How can you find peak or local maxima in a 1D array?

Peak refers to the highest value or point in the graph. Graphically, peaks are surrounded by the smaller value points on either side. It is also known as local maxima.

You can use two methods to calculate peak.

 

First/Simple method

.where() – The simplest method lists all positions and indices where the element value at position X is greater than the element on either side of this. Remember, this function doesn't check for the points with only one neighbour.

 

Second/Complex method

This method is complex as it uses multiple functions (.diff(), .sign(), .where()) for calculating peak.

.diff() – It calculates the difference between each element

.sign() – Use this function to get the sign of difference

.where() – Use this function to get the position or indexes of local maxima

 

36. What are different ways to convert a Python dictionary to a NumPy array?

First method

np.array() – Converts dictionary to nd array

List comprehension – To get all dictionary values as a list and pass it as input to array

 

Second method

np.array() – Converts dictionary to nd array

dictionary_obj.items - To get all dictionary values as a list and pass it as input to an array

 

37. What is the best way to create a Histogram?

The best way to create a Histogram is to use the function histogram(). You can apply it to the array objects, which return a pair of vectors, "the histogram of the array" and "a vector of the bin edges."

 

Note down that numpy.histogram only generates the data while pylab.hist plots the histogram automatically.

 

Example:

Code:

import sys
import matplotlib
matplotlib.use('Agg')

import matplotlib.pyplot as plt
import numpy as np

x = np.random.normal(100, 350, 250)
plt.hist(x)
plt.show()
plt.savefig(sys.stdout.buffer)
sys.stdout.flush()

Output:

What is the best way to create a Histogram Output

 

38. Can you create strides from a 1D array?

Yes, you can create strides from a 1D array. Strides refer to the tuple of integer values, and each byte has been indicated by a specific dimension. Strides say how many bytes you must skip in memory for moving to the next position along with a specific axis. Like, you want to skip 4 bytes (1 value) to move to the next column, but you need 20 bytes ( 5 values) to get to the same position in the next row.

Steps:

  • Import libraries and take the same data
  • Create Stride

Syntax: array object.strided(array, new_array size, stride_steps in bytes)

 

Example:

Code:

import numpy as np
from numpy.lib.stride_tricks import as_strided
sa1 = np.array([1,3,2,5,4,7,8,1], dtype = "int32")
print("This is a Sample 1D array:", sa1)
Result = np.lib.stride_tricks.as_strided(sa1,(3,2),(12,4))
print("Array after stride","\n",Result, "\n")
print("This is the shape of original array which is an 1D array:","\n",sa1.shape,"\n")
print("This is the shape of our Result which is an 2D array:","\n",Result.shape)

Output:

This is a Sample 1D array: [1 3 2 5 4 7 8 1]
Array after stride 
 [[1 3]
 [5 4]
 [8 1]]

This is the shape of original array which is an 1D array: 
 (8,) 
This is the shape of our Result which is an 2D array: 
 (3, 2)

 

We have strided 1D array into new array with 3 rows and 2 columns. You can see array after the stride. And, stride_steps for row is 12 bytes whereas for columns is 4 bytes.


Test your NumPy coding skills here

 

39. How does NumPy handle numerical exceptions?

You can use two methods to handle numerical exceptions. The first is "warn" for invalid and divided numerical exceptions, whereas the second is "ignore" for underflow exceptions.

 

Type of behaviours

  • Ignore – It takes no action when an exception occurs
  • Warn – It prints a RuntimeWarning using warning modules
  • Raise – It raises a FloatingPointError
  • Call – It calls a function specified using a seterrcall function
  • Print – It prints only warning directly to stdout
  • Log – It records the error in a Log object specified by seterrcall

 

Types of errors

  • All – applicable to all numeric exceptions
  • Invalid – When NaNs are generated
  • Divide – Divide by Zero error
  • Overflow – Floating point overflows
  • Underflow – Floating point underflows

 

40. What is the biggest challenge while writing extension modules in NumPy?

Reference counting is the biggest challenge while writing extension modules in NumPy. Mismanagement of reference counting might result in memory leaks and segmentation faults. It is challenging to manage the reference counting. Because you need to understand that every Python variable has a reference count, what does each function do to implement the reference count of the objects? After that, only you can use DECREF/INCREF variables appropriately.

 

41. What is the use of SWIG method in NumPy?

SWIG, or Simple Wrapper and Interface Generator, is one of the powerful tools. It generates wrapper code for interfacing with a diverse range of scripting languages. The tool can quickly parse header files. It can also create an interface to the target language using a code prototype.

 

Features:

  • Support multiple scripting languages
  • Good choice to wrap large C libraries and functions
  • Long time availability
  • C++ support

 

Drawbacks:

  • It generates large code between C code and Python
  • Performance issues which are unable to optimise
  • Difficult to write codes for interface files
  • It needs APIs and can't avoid reference counting issues

Practice Skill: Practice your coding skills and knowledge on relevant subjects like array, Matlab, here.

 

Final Thoughts

 

Our goal is to provide you with most asked Numpy interview questions. You can enhance your NumPy knowledge by practising these questions. By joining our community, you can learn the latest tech interview questions and answers and tips to prepare for your next interview. Feel free to comment with your query!

 

Other Backend Technology Interview Questions and Answers

System Design Interview Questions | API Interview Questions | PHP Interview Questions | .NET Core Interview Questions | Data Structure Concepts | Node.js Interview Questions | Django Interview Questions | FastAPI Python Web Framework | React Interview Questions | C Programming Interview Questions | Java Collections Interview Questions | Microservices Interview Questions | Key backend development skills | Data Science Interview Questions | Python Interview Questions | Java Exception Handling Interview Questions | Java Spring Framework Interview Questions | OOPs Concepts Java Interview Questions.

HACKERBUCK AWARDED