Blog

Blog

Top 50 NumPY Interview Questions

NumPY Interview Questions:

NumPY Interview Questions

1. What is NumPy? Why should we use it?

NumPy is a Python library for working with large, multi-dimensional arrays and matrices of numerical data. It provides a high-performance multidimensional array object, and tools for working with these arrays. NumPy is designed to be efficient with large arrays, and to allow you to work with them in a natural, intuitive way.

NumPy is widely used in scientific computing, data analysis, and machine learning, and is an essential library for these tasks. One of the main reasons to use NumPy is that it is much faster and more efficient than working with large arrays and matrices in pure Python. NumPy arrays are optimized for arithmetic operations, and use much less memory than Python lists. This makes NumPy a good choice for numerical computing tasks that require high performance, such as analyzing large datasets or implementing machine learning algorithms.

NumPy also provides many functions for working with arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, etc. By using these functions, you can perform various operations on arrays without having to write low-level code.

In summary, NumPy is a powerful library for working with large, multi-dimensional arrays and matrices of numerical data in Python. It provides efficient, high-performance operations on arrays, and a wide range of functions for manipulating and analyzing them, making it an essential tool for scientific computing, data analysis, and machine learning.

2. Why is NumPy preferred over Matlab, Octave, Idl or Yorick?

NumPy is an open-source, high-performing library that allows complex mathematical and scientific computational capabilities. It makes use of Python language which is a high-level, easy-to-learn, general-purpose programming language. It supports the following:

  • Powerful functions for performing complex mathematical operations on multi-dimensional matrices and arrays. The operations on ndarrays of NumPy are approximately up to 50% faster when compared to operations on native lists using loops. This efficiency is very much useful when the arrays have millions of elements.
  • Provides indexing syntax to access portions of data easily in a large array.
  • Provides built-in functions which help to easily perform operations related to linear algebra and statistics.
  • It takes only a few lines of code to achieve complex computations using NumPy.

3. How are NumPy arrays better than Python’s lists?

NumPy arrays are generally more efficient and more suited to numerical computing tasks than Python lists. Some of the reasons why NumPy arrays are better than lists are:

  1. Performance: NumPy arrays are much faster and more efficient than Python lists when it comes to performing element-wise operations, such as arithmetic or statistical operations. This is because NumPy arrays are implemented in C, which is much faster than Python.
  2. Memory efficiency: NumPy arrays use much less memory than Python lists, because they store data more efficiently. NumPy arrays store data in a contiguous block of memory, whereas lists store data in a series of pointers, which can be scattered throughout memory. This makes NumPy arrays more memory-efficient than lists.
  3. Convenience: NumPy arrays provide many convenient functions for performing operations on arrays, such as mathematical and statistical functions, shape manipulation, sorting, and selecting. This makes it easier to perform operations on arrays without having to write low-level code.
  4. Compatibility: NumPy arrays are compatible with many other libraries that expect numerical data to be stored in a contiguous block of memory, such as SciPy, Scikit-learn, and Matplotlib. This makes it easier to use NumPy arrays with these libraries.

In summary, NumPy arrays are faster, more memory-efficient, and more convenient than Python lists, which makes them a better choice for numerical computing tasks.


4. What are ndarrays in NumPy?

ndarray object is the core of the NumPy package. It consists of n-dimensional arrays storing elements of the same data types and also has many operations that are done in compiled code for optimised performance. These arrays have fixed sizes defined at the time of creation. Following are some of the properties of ndarrays:

  • When the size of ndarrays is changed, it results in a new array and the original array is deleted.
  • The ndarrays are bound to store homogeneous data.
  • They provide functions to perform advanced mathematical operations in an efficient manner.

5. What are ways of creating 1D, 2D and 3D arrays in NumPy?

Consider you have a normal python list. From this, we can create NumPy arrays by making use of the array function as follows:

  • One-Dimensional array
 import numpy as np
 
arr = [1,2,3,4]        #python list
numpy_arr = np.array(arr)    #numpy array
  • Two-Dimensional array
 import numpy as np


arr = [[1,2,3,4],[4,5,6,7]]
numpy_arr = np.array(arr)
  • Three-Dimensional array
 import numpy as np


arr = [[[1,2,3,4],[4,5,6,7],[7,8,9,10]]]
numpy_arr = np.array(arr)

Using the np.array() function, we can create NumPy arrays of any dimensions.

6. How do you find the data type of the elements stored in the NumPy arrays?

NumPy supports the following datatypes:

  • i – integer
  • S – string
  • b – boolean
  • f – float
  • u – unsigned integer
  • c – complex float
  • m – timedelta
  • M – datetime
  • O – object
  • U – unicode string
  • V – fixed memory chunk for types such as void
  • We can make use of the dtype property that returns the type of the elements stored in the NumPy array. Let us consider the below code snippet. We create some sample arrays and we see what the data types of these arrays are.
import numpy as np


arr1 = np.array([1, 2, 3, 4])
arr2 = np.array(['I', 'love', 'Interviewbit'])    # Stored as Unicode characters with length of characters ranging from 1 to 12
arr3 = np.array([1, 2, 3, 4], dtype='S')        # Creating numpy array of defined type stringprint(arr1.dtype)
print(arr2.dtype)
print(arr3.dtype)

The output will be:

int64
<U12
|S1

7. How can you reverse a NumPy array?

There are two ways of reversing a NumPy array.

  • Method 1: Using the slicing method: We can make use of [::-1] for reversing the array. The following example demonstrates this:
import numpy as np


# create numpy array
arr = np.array([1, 2, 4, 6])


# To reverse array
reverse_arr = arr[::-1]
print(reverse_arr

Output:

[6 4 2 1]
  • Method 2: flipud function: This function is provided by NumPy to reverse the NumPy array. Let us see the below example about its usage.
import numpy as np


# create numpy array
arr = np.array([1, 2, 4, 5, 6])


#flipud method for reversing
reverse_arr = np.flipud(arr)
print(reverse_arr

Output:

[6 5 4 2 1]

8. How is np.mean() different from np.average() in NumPy?

In NumPy, np.mean() and np.average() are very similar functions that can be used to compute the mean of a given array.

The main difference between np.mean() and np.average() is that np.average() allows you to specify weights for each element in the array. This can be useful if you want to give some elements in the array more weight than others when calculating the mean.

Here is an example of how you might use np.average() with weights:

Copy codeimport numpy as np

# Suppose we have an array of values
values = [1, 2, 3, 4]

# And we want to give the values at index 1 and 3 more weight when calculating the mean
weights = [0.5, 1.5, 0.5, 1.5]

mean = np.average(values, weights=weights)
print(mean)  # Output: 2.6666666666666665

In this example, the mean is calculated as follows:

Copy code(1 * 0.5 + 2 * 1.5 + 3 * 0.5 + 4 * 1.5) / (0.5 + 1.5 + 0.5 + 1.5) = 2.6666666666666665

Without the weights, np.mean() and np.average() would give the same result.

Copy codeimport numpy as np

# Suppose we have an array of values
values = [1, 2, 3, 4]

mean = np.mean(values)
print(mean)  # Output: 2.5

mean = np.average(values)
print(mean)  # Output: 2.5

9. How do you count the frequency of a given positive value appearing in the NumPy array?

We can make use of the bincount() function to compute the number of times a given value is there in the array. This function accepts only positive integers and boolean expressions as the arguments.

import numpy as np
arr = np.array([1, 2, 1, 3, 5, 0, 0, 0, 2, 3])
result = np.bincount(arr)
print(result)

The result is:

[3 2 2 2 0 1]

It has to be noted here that each element represents the count of the corresponding index value present in the original array. This is demonstrated in the below image:

10. How do we check for an empty array (or zero elements array)?

We can check for the emptiness of a NumPy array by making use of the size attribute.

Let us consider the below example. We have NumPy array arr filled with zeros. If the size element returns zero, that means the array is empty or it only consists of zeros.

import numpy as np
arr = np.zeros((1,0))    #returns empty array
print(arr.size)          #returns 0

This return 0

11. How is arr[:,0] different from arr[:,[0]]

arr[:,0] – Returns 0th index elements of all rows. In other words, return the first column elements.

 import numpy as np


arr = np.array([[1,2,3,4],[5,6,7,8]])
new_arr =arr[:,0]
print(new_arr)

Output:

 [1 5]

arr[:,[0]] – This returns the elements of the first column by adding extra dimension to it.

import numpy as np


arr = np.array([[1,2,3,4],[5,6,7,8]])
new_arr =arr[:,[0]]
print(new_arr)

Output:

[[1]
[5]]

12. How do you multiply 2 NumPy array matrices?

We can make use of the dot() for multiplying matrices represented as NumPy arrays. This is represented in the code snippet below:

import numpy as np


# NumPy matrices
A = np.arange(15,24).reshape(3,3)
B = np.arange(20,29).reshape(3,3)
print("A: ",A)
print("B: ",B)


# Multiply A and B
result = A.dot(B)
print("Result: ", result

Output

A:  [[15 16 17]
 [18 19 20]
 [21 22 23]]
B:  [[20 21 22]
 [23 24 25]
 [26 27 28]]
Result:  [[1110 1158 1206]
 [1317 1374 1431]
 [1524 1590 1656]]

13. How do you concatenate 2 NumPy arrays?

Concatenating 2 arrays by adding elements to the end can be achieved by making use of the concatenate() method of the NumPy package. Syntax:

np.concatenate((a1, a2, ...), axis=0, out=None) 

where,

  • a1,a2: arrays of the same shape
  • axis: Represents the axis along which the arrays are joined. The default value is 0.
  • out: If mentioned, it specifies the destination for placing the result.

For example:

import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])


# Concatenate with axis 0
c = np.concatenate((a,b), axis=0)
print("With axis 0: \n",c )


# Concatenate with axis 1 (b.T represents transpose matrix)
d = np.concatenate((a,b.T), axis=1)
print("With axis 1: \n",d 

The output would be:

With axis 0:
 [[1 2]
 [3 4]
 [5 6]]
With axis 1:
 [[1 2 5]
 [3 4 6]]

Notice how the arrays are concatenated with different values of the axis.

14. How do you convert Pandas DataFrame to a NumPy array?

The to_numpy() method of the NumPy package can be used to convert Pandas DataFrame, Index and Series objects.

Consider we have a DataFrame df, we can either convert the whole Pandas DataFrame df to NumPy array or even select a subset of Pandas DataFrame to NumPy array by using the to_numpy() method as shown in the example below:

import pandas as pd
import numpy as np
# Pandas DataFrame
df = pd.DataFrame(data={'A': [3, 2, 1], 'B': [6,5,4], 'C': [9, 8, 7]},
                  index=['i', 'j', 'k'])
print("Pandas DataFrame: ")
print(df)


# Convert Pandas DataFrame to NumPy Array
np_arr = df.to_numpy()
print("Pandas DataFrame to NumPy array: ")
print(np_arr)




# Convert specific columns of Pandas DataFrame to NumPy array
arr = df[['B', 'C']].to_numpy()
print("Convert B and C columns of Pandas DataFrame to NumPy Array: ")
print (ar

The output of the above code is

Pandas DataFrame:
   A  B  C
i  3  6  9
j  2  5  8
k  1  4  7
Pandas DataFrame to NumPy array:
[[3 6 9]
 [2 5 8]
 [1 4 7]]
Convert B and C columns of Pandas DataFrame to NumPy Array:
[[6 9]
 [5 8]
 [4 7]]

NumPY Interview Questions for Experienced

15. What do you understand by Vectorization in NumPy?

Function Vectorization technically means that the function is applied to all elements in the array. Typically, certain python functionalities on arrays (such as loops) are slower in nature because python arrays can contain elements of different data types. Since the C program expects a specific datatype, there are chances of compiler optimisation which makes C code run faster. Since NumPy arrays support storing elements of a single datatype, most of the implementations of the functions written in NumPy meant for arithmetic, logical operations etc have optimised C program code under their hood. Additionally, NumPy also helps developers create their own vectorised functions by following the below steps:

  • Write your required function that takes array elements as parameters.
  • Vectorize the function by making use of the vectorize() method of the NumPy package.
  • Give array inputs to the vectorized function.

The below example demonstrates the process of vectorization.

import numpy as np
# Define your functiondef add(arr1, arr2):return (arr1 + arr2)


arr1 = np.array([1,2,3])
arr2 = np.array([4,5,6])


#vectorize add method
vectorized_add = np.vectorize(add)


#call vectorized method
result = vectorized_add(arr1, arr2)


print(resu

The output of above code

 [5 7 9]

16. How is vstack() different from hstack() in NumPy?

Both methods are used for combining the NumPy arrays. The main difference is that the hstack method combines arrays horizontally whereas the vstack method combines arrays vertically.

For example, consider the below code.

import numpy as np
a = np.array([1,2,3])
b = np.array([4,5,6])


# vstack arrays
c = np.vstack((a,b))
print("After vstack: \n",c)
# hstack arrays
d = np.hstack((a,b))
print("After hstack: \n",d)

The output of this code would be:

After vstack:
[[1 2 3]
[4 5 6]]
After hstack:
[1 2 3 4 5 6]

Notice how after the vstack method, the arrays were combined vertically along the column and how after the hstack method, the arrays were combined horizontally along the row.

17. How do you find the local peaks (or maxima) in a 1-D NumPy Array?

Peaks are the points that are surrounded by smaller value points on either side as shown in the image below:

There are two ways of finding local maxima:

Using .where() method: This method lists all positions/indices where the element value at position i is greater than the element on either side of it. This method does not check for the points that have only one neighbour. This is demonstrated in the example below:

import numpy as np
# define NumPy array
arr = np.array([1, 4, 8, 1, 3, 5, 1, 6, 1, -5, -1, 19, 2])




maxima_peaks_positions = np.where((arr[1:-1] > arr[0:-2]) * (arr[1:-1] > arr[2:]))[0] + 1
print(maxima_peaks_positions

Output:

[ 2  5  7 11]]
  • The +1 at the end of the expression is required as it finds the indexes within the slice arr[1:-1] and not the entire array arr.
  • The where() method returns a tuple of arrays where the first element is our required array. Hence we add [0] after the where method.

Using combination of .diff(), .sign() and .where() method:

  • In this method, we calculate the difference between each element using the diff() method of NumPy.
  • Then we use the sign() method on the array to get the sign of difference.
  • The value can be either -1 or +1. This result is then passed on to another diff() method which returns 0, -2 or +2 value. The value 0 indicates that the points are continuously increasing or decreasing, +2 indicates minimum peak and -2 indicates maximum peak (local maxima).
  • We then identify the position or indexes of the local maxima using the where() method. The reason for using +1 at the end of where and [0] after where is the same as the explanation described in Method 1 for finding local maxima.

The following code example demonstrates this:

import numpy as np
# define NumPy array
arr = np.array([1, 4, 8, 1, 3, 5, 1, 6, 1, -5, -1, 19, 2])


all_peaks = np.diff(np.sign(np.diff(arr)))
maxima_peaks_positions = np.where(all_peaks == -2)[0] + 1
print(maxima_peaks_positions)

Output:

[ 2  5  7 11]]

18. How is Vectorization related to Broadcasting in NumPy?

Vectorization is the process of applying a function or operation to a sequence of elements, such as a list or an array, in an element-wise fashion, without using explicit loops. In NumPy, this is often done using the ufunc (universal function) functions, which can operate on arrays and perform element-wise operations.

Broadcasting is a technique used in NumPy to perform operations on arrays of different shapes, by “broadcasting” the smaller array across the larger one. This allows NumPy to perform element-wise operations between arrays of different shapes, as long as they are broadcastable.

For example, suppose you have a 2D array A of shape (m, n) and a 1D array b of shape (n,). You can add b to each row of A using broadcasting, like this:

Copy codeimport numpy as np

A = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([10, 20, 30])

A + b

The result will be a new array C of shape (m, n), with each element C[i, j] equal to A[i, j] + b[j].

Vectorization and broadcasting are both useful techniques for optimizing the performance of numerical computations in NumPy, by avoiding the overhead of explicit loops. They can also make your code more concise and easier to read.

19. What happens when the split() method is used for splitting NumPy arrays?

1. np.split() : Equally splits arrays into multiple sub-arrays. It raises Value Error when the split cannot be equal.

  • Syntax:
np.split(array, sections, axis=0)

where,

  • array – array that needs to be split
  • sections –
    • If we give an integer X, X equal sub-arrays are obtained after dividing the array. If the split is not possible, ValueError is raised.
 import numpy as np
a = np.arange(8)
split_arr = np.split(a, 2)
split_arr

Output

[array([0, 1, 2, 3]), array([4, 5, 6, 7])]
  • If we give a 1-D sorted array then the entries would represent where the array would be split along the axis. For instance if we provide [2:3] and axis as 0, then the result would be
  • [arr[0:2], arr[2:3], arr[3:]]
    • If the provided index exceeds the array dimension along the given axis, then an empty subarray will be returned.
    • For example:
[array([0., 1., 2.]),
 array([3.]),
 array([4.]),
 array([5.]),
 array([], dtype=float64),
 array([], dtype=float64),
 array([], dtype=float64)]

The output would be:

import numpy as np
a = np.arange(6.0)
split_arr = np.split(a, [3, 4, 5, 6, 7,8])
split_arr
  • axis – Along what axis the array has to be split. By default, the value is 0

20. What happens when we use the arrays_split() method for splitting the NumPy array?

The array_split() method is similar to the split() method as it helps in splitting a given array to multiple subarrays. The main difference is that the array_split() allows sections to be an integer which does not result in equal array division. For an array of length L, if we want it to split to N subarrays, then L % N subarrays of size (L//N + 1) and remaining subarrays are of size L//N.

In the above figure, we see there are 5 elements in the array, we want to split the array to 3 subarrays. So L % N = 5%3 = 2 subarrays of size (L//N +1) = (5//3 +1) = 2 are returned and remaining 1 subarray of size L//N = 1 is returned.

Syntax:

np.array_split(array, sections, axis=0)

where,

  • array – Given Input array.
  • sections – List of indices or Number of subarrays to be returned.
  • axis – Axis along which values have to be appended.

The code for the example illustrated above is:

import numpy as np
arr = np.arange(5.0)
split_arrs = np.array_split(arr, 3)
split_arrs

Output:

[array([0., 1.]), array([2., 3.]), array([4.])]

21. How will you implement the moving average for the 1D array in NumPy?

We can make use of the convolve() method. Here, it leverages the way discrete convolution is computed and uses it to find the rolling mean (moving average). Here, the sequence of ones of length equal to the length of the sliding window is convolved with the array.

We first define a calculate_moving_average function which performs the convolution of an array with the sequence of ones of sliding window length w. The mode of the convolve method will be ‘valid’ to generate the points only where the overlapping of the sequence is complete.

import numpy as np
def calculate_moving_average(arr, w):
    return np.convolve(arr, np.ones(w),'valid')/w

The above-defined function can be then used for finding the moving average as shown in the examples below:

arr1 = np.array([4,5,8,9,3,2,4,2,0,2])
print("Moving average of window length 2: ")
av1 = calculate_moving_average(arr1, 2)
print(av1)


print("Moving average of window length 4: ")
av2 = calculate_moving_average(arr1, 4)
print(av2)

Output:

Moving average of window length 2:
[4.5 6.5 8.5 6.  2.5 3.  3.  1.  1. ]
Moving average of window length 4:
[6.5  6.25 5.5  4.5  2.75 2.   2.  ]

22. How is fliplr different from flipud methods in NumPy?

The fliplr() method is used for flipping an array in the left or right direction. The columns are preserved but the order of elements in the columns would be different than before. This has been represented in the image below:

We see that the positions of the elements are flipped left or right than their original position in the result.

Syntax of fliplr:

np.fliplr(arr)

where arr is the array that has to be flipped.

The flipud function also flips the array but in the up or down direction. The rows are preserved in this case but they can appear in a different order in the result. This is represented in the image below:

Here, we see that the numbers 1, 3 and 5 elements are flipped in the up/down direction in the result.

The syntax for flipud:

np.flipud(arr)

where arr is the array that has to be flipped.

NumPy Coding Questions

23. Write a program to convert a string element to uppercase, lowercase, capitalise the first letter, title-case and swapcase of a given NumPy array.

Sample Solution:-

import numpy as np


# Create Sample NumPy array
arr = np.array(['i', 'love', 'NumPy', 'AND', 'interviewbit'], dtype=str)


upper_case_arr = np.char.upper(arr)
lower_case_arr = np.char.lower(arr)
capitalize_case_arr = np.char.capitalize(arr)
titlecase_arr = np.char.title(arr)
swapcase_arr = np.char.swapcase(arr)


print("Upper Conversion: ", upper_case_arr)
print("Lower Conversion: ", lower_case_arr)
print("Capitalize First Letter Conversion: ", capitalize_case_arr)
print("Titlecase Conversion: ", titlecase_arr)
print("Swapcase Conversion: ", swapcase_ar

Output:

Upper Conversion:  ['I' 'LOVE' 'NUMPY' 'AND' 'INTERVIEWBIT']
Lower Conversion:  ['i' 'love' 'numpy' 'and' 'interviewbit']
Capitalize First Letter Conversion:  ['I' 'Love' 'Numpy' 'And' 'Interviewbit']
Titlecase Conversion:  ['I' 'Love' 'Numpy' 'And' 'Interviewbit']
Swapcase Conversion:  ['I' 'LOVE' 'nUMpY' 'and' 'INTERVIEWBIT']

24. Write a program to transform elements of a given string to a numeric string of 10 digits by making all the elements of a given string to a numeric string of 8 digits with zeros on the left.

Sample Solution:-

import numpy as np


# Create Sample NumPy array
arr = np.array(['22', '9', '1234', '567', '89102'], dtype=str)


zeroes_filled_arr = np.char.zfill(arr, 8)
print("Transformed array: ")
print(zeroes_filled_arr

Output:

Transformed array:
['00000022' '00000009' '00001234' '00000567' '00089102']

25. Write a program for inserting space between characters of all elements in a NumPy array.

Sample Solution:-

import numpy as np


# Create Sample NumPy Array
arr = np.array(['i', 'love', 'NumPy', 'AND', 'interviewbit'], dtype=str)


transformed_arr = np.char.join(" ", arr)


print("Transformed Array: ")
print(transformed_ar

Output:

Transformed Array:
['i' 'l o v e' 'N u m P y' 'A N D' 'i n t e r v i e w b i t']

26. Write a program to repeat each of the elements five times for a given array.

Sample Solution:-

import numpy as np
# Create Sample NumPy Array
arr = np.array(['i', 'love', 'NumPy', 'AND', 'interviewbit'], dtype=str)


transformed_array = np.char.multiply(arr, 5)
print("Transformed array:")
print(transformed_array)

Output:

Transformed array:
['iiiii' 'lovelovelovelovelove' 'NumPyNumPyNumPyNumPyNumPy'
 'ANDANDANDANDAND'
 'interviewbitinterviewbitinterviewbitinterviewbitinterviewbit']

27. Write a program for creating an integer array with values belonging to the range 10 and 60

import numpy as np
arr = np.arange(10, 60)
print(arr)

Output:

[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
 58 59]

28. Write a program to add a border of zeros around the existing array.

For example,

If you have the below array:

[[ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]] 

The resultant array should be: (zeros on the border and 1s within it)

[[ 0.  0.  0.  0.  0.  0.]
 [ 0.  1.  1.  1.  1.  0.]
 [ 0.  1.  1.  1.  1.  0.]
 [ 0.  1.  1.  1.  1.  0.]
 [ 0.  0.  0.  0.  0.  0.]]

Solution:-

This can be achieved by using the pad method of the NumPy library.

import numpy as np


# Create NumPy arrays filled with ones
ones_arr = np.ones((4,4))


print("Transformed array:")
transformed_array = np.pad(ones_arr, pad_width=1, mode='constant', constant_values=0)
print(transformed_array

Output:

Transformed array:
[[0. 0. 0. 0. 0. 0.]
 [0. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 0.]
 [0. 0. 0. 0. 0. 0.]]

29. Write a program for changing the dimension of a NumPy array.

We can achieve this by overriding the shape attribute of the NumPy array.

Sample Solution:

import numpy as np


#Create NumPy array
arr = np.array([1,2,3,4,5,6,7,8,9])
print("Original Shape: ", arr.shape)


# Change the shape/dimension of the array
arr.shape = (3, 3)
print("Transformed Matrix :")
print(arr)
print("Transformed Shape: ",arr.shape

Output:

Original Shape:  (9,)
Transformed Matrix :
[[1 2 3]
 [4 5 6]
 [7 8 9]]
Transformed Shape:  (3, 3)

In this approach, care has to be taken w.r.t the number of elements present in the original array before changing the dimensions. Otherwise, it will result in the ValueError as shown below:

import numpy as np


# We have array of 8 elements
arr = np.array([1,2,3,4,5,6,7,8])


# We are trying to convert the 1D array to a 3D array which expects 9 elements
arr.shape = (3, 3)
print(arr

Running this code would result in:

      1 import numpy as np
      2 arr = np.array([1,2,3,4,5,6,7,8])
----> 3 arr.shape = (3, 3)
      4 print(arr)


ValueError: cannot reshape array of size 8 into shape (3,3)

30. Write a program for interchanging two axes of the NumPy array.

This can be achieved by using the swapaxes method of NumPy. The below image illustrates the meaning of swapping axes.

import numpy as np
arr = np.array([[1,2,3]])
print("Original array: ")
print(arr)


#Swap axes
axis_swapped_arr =  np.swapaxes(arr,0,1)
print("Transformed array: ")
print(axis_swapped_arr)

Output:

Original array:
[[1 2 3]]
Transformed array:
[[1]
 [2]
 [3]]

Conclusion

The popularity of the NumPy package has grown immensely among the data science and python developers community ever since it was first introduced in 2005 due to the wide range of high-performing functionalities it offers. This is why it becomes essential to learn and be prepared for the interview questions about this package. In this article, we have seen the most commonly asked NumPy interview questions for freshers and experienced people, along with some questions on writing python programs which make use of NumPy functions.

NumPY MCQ Questions

31. What does NumPy stand for?

Number Python

Numerator Python

Numerical Python

There is no full form, it is a stand-alone word

32. What is the output of the below code snippet?

import numpy as np
arr1 = np.array([7,8,9,10])
arr2 = np.array([1,2,3,4])
arr3 = arr1 + arr2
arr3 = arr3*arr1
print (arr3[2])

21

108

80

12

33. What does the size attribute in numpy use to find?

number of items

shape

date & time

unique items

34. What is the output of the below code snippet?

import numpy as np
 arr = np.array([[4,2,0,5],[1,3,5,7]])
 print (arr.size)

8

4

2

6

35. What method is used for changing the shape of numpy arrays?

shape()

change_shape()

update_shape()

reshape()

36. What is the purpose of zero() function?

To create a matrix with the first row and first column as 0

To create a matrix with diagonal elements as 0

To create a matrix with all elements as 0

To return an integer number 0

37. What is the output of the below code?

import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([5, 6, 7])
c = np.array([8, 9, 10, 11, 12])
p, q, r = np.ix_(a, b, c)
print(p)

a) 

[[[1]]


 [[2]]


 [[3]]


 [[4]

b)

[[[5]
  [6]
  [7]]]

.c)

[[[ 8  9 10 11 12]]]

d)

[[[1]]


 [[2]]


 [[3]]


 [[4]]

 [[5]]

 [[6]]

 [[7]]

 [[8]]


 [[9]]

 [[10]]

 [[11]]

 [[12]]

a

b

c

d

38. Why do we use ndim?

To find the size of an array

To find the dimension of an array

To find the number of elements in an array

There is no such thing as ndim

39. What is the result of the below code snippet?

import numpy as np


a = np.array([(1,2,50)])


print(a.itemsize

3

4

1

8

40. Which of the following is the correct way of creating an array of type float?

a = np.array([4,3,2,1]).toFloat()

a = np.float([4,3,2,1])

a = np.array([4,3,2,1], dtype=‘f’)

a = np.array([4,3,2,1], type=‘float’)

Hide

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar
Compare

Subscribe to Newsletter

Stay ahead of the rapidly evolving world of technology with our news letters. Subscribe now!