NumPy Interview Questions and Answers
Freshers / Beginner level questions & answers
Ques 1. What is NumPy?
NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with mathematical functions to operate on these arrays.
Ques 2. How to install NumPy?
You can install NumPy using the command 'pip install numpy'.
Ques 3. Create a NumPy array from a Python list.
import numpy as npnmy_list = [1, 2, 3]narr = np.array(my_list)
Ques 4. How to find the dimension of a NumPy array?
You can use the attribute 'ndim'. For example, if 'arr' is your array, use 'arr.ndim'.
Ques 5. What is the difference between 'np.zeros()' and 'np.ones()' in NumPy?
'np.zeros()' creates an array filled with zeros, while 'np.ones()' creates an array filled with ones.
Ques 6. How to access elements from a 2D NumPy array?
You can use indices. For example, 'arr[1, 2]' accesses the element in the second row and third column of 'arr'.
Ques 7. How to find the shape of a NumPy array?
You can use the attribute 'shape'. For example, 'arr.shape' returns the shape of 'arr'.
Ques 8. What is the purpose of 'np.eye()' in NumPy?
'np.eye()' creates an identity matrix with ones on the main diagonal and zeros elsewhere.
Ques 9. How to perform element-wise addition of two NumPy arrays?
import numpy as npnarr1 = np.array([1, 2, 3])narr2 = np.array([4, 5, 6])nresult = arr1 + arr2
Ques 10. What is the purpose of 'np.arange()' in NumPy?
'np.arange()' creates an array with regularly spaced values within a specified range.
Ques 11. How to find the mean of a NumPy array?
You can use 'np.mean()'. For example, 'mean_value = np.mean(arr)'.
Intermediate / 1 to 5 years experienced level questions & answers
Ques 12. Explain broadcasting in NumPy.
Broadcasting is a powerful mechanism that allows NumPy to work with arrays of different shapes when performing arithmetic operations.
Ques 13. How to perform element-wise multiplication of two NumPy arrays?
import numpy as npnarr1 = np.array([1, 2, 3])narr2 = np.array([4, 5, 6])nresult = arr1 * arr2
Ques 14. Explain the purpose of np.random.seed() in NumPy.
np.random.seed() is used to initialize the random number generator in NumPy, ensuring reproducibility of random results.
Ques 15. Explain the concept of a NumPy universal function (ufunc).
A ufunc in NumPy is a flexible function that operates element-wise on NumPy arrays, supporting broadcasting.
Ques 16. How to concatenate two NumPy arrays vertically?
You can use 'np.vstack()' or 'np.concatenate()' with 'axis=0'.
Ques 17. What is the purpose of 'np.ravel()' in NumPy?
'np.ravel()' returns a flattened 1D array from a multi-dimensional array.
Ques 18. Explain the purpose of 'np.concatenate()' in NumPy.
'np.concatenate()' joins a sequence of arrays along an existing axis.
Ques 19. How to perform matrix multiplication in NumPy?
You can use 'np.matmul()' or the '@' operator. For example, 'result = np.matmul(arr1, arr2)' or 'result = arr1 @ arr2'.
Experienced / Expert level questions & answers
Ques 20. What is the purpose of np.newaxis in NumPy?
np.newaxis is used to increase the dimension of the existing array by one more dimension when used once.
Ques 21. Explain the differences between np.dot() and np.matmul() in NumPy.
np.dot() performs matrix multiplication for 2-D arrays, while np.matmul() is equivalent to the '@' operator and is more general.
Ques 22. Create a diagonal matrix using NumPy.
import numpy as npnarr = np.diag([1, 2, 3])
Ques 23. Explain the purpose of np.meshgrid() in NumPy.
np.meshgrid() is used to create coordinate matrices from coordinate vectors, commonly used for 3D plotting.
Ques 24. Explain the use of 'np.histogram()' in NumPy.
'np.histogram()' computes the histogram of a set of data, returning the bin counts and bin edges.
Ques 25. How to find the index of the maximum value in a NumPy array?
You can use 'np.argmax()'. For example, 'np.argmax(arr)' returns the index of the maximum value in 'arr'.
Ques 26. What is the purpose of 'np.where()' in NumPy?
'np.where()' returns the indices of elements that satisfy a given condition.
Ques 27. Explain the use of 'np.linalg.inv()' in NumPy.
'np.linalg.inv()' computes the (multiplicative) inverse of a matrix.
Ques 28. What is the purpose of 'np.percentile()' in NumPy?
'np.percentile()' calculates the nth percentile of a dataset.
Ques 29. Explain the use of 'np.unique()' in NumPy.
'np.unique()' returns the unique elements of an array.
Ques 30. How to transpose a NumPy array?
You can use 'arr.T'. For example, 'transposed_arr = arr.T'.
Most helpful rated by users:
Related interview subjects
Python Matplotlib interview questions and answers - Total 30 questions |
Django interview questions and answers - Total 50 questions |
Pandas interview questions and answers - Total 30 questions |
Deep Learning interview questions and answers - Total 29 questions |
PySpark interview questions and answers - Total 30 questions |
Flask interview questions and answers - Total 40 questions |
PyTorch interview questions and answers - Total 25 questions |
Data Science interview questions and answers - Total 23 questions |
SciPy interview questions and answers - Total 30 questions |
Generative AI interview questions and answers - Total 30 questions |
NumPy interview questions and answers - Total 30 questions |
Python interview questions and answers - Total 106 questions |
Python Pandas interview questions and answers - Total 48 questions |