interview-questions
  1. numpy-interview-questions

NumPy Interview Questions & Answers


  1. What is NumPy?

    • Answer: NumPy is a numerical computing library in Python that provides support for large, multi-dimensional arrays and matrices, along with mathematical functions to operate on these arrays.
  2. Explain the main difference between a Python list and a NumPy array.

    • Answer: NumPy arrays are homogeneous and can contain elements of the same data type, while Python lists can contain elements of different data types.
  3. How do you install NumPy?

    • Answer: You can install NumPy using the command pip install numpy.
  4. What is the purpose of the NumPy ndarray object?

    • Answer: The ndarray is a multi-dimensional array object in NumPy that represents arrays of fixed size and allows for efficient mathematical operations on data.
  5. How can you create a NumPy array?

    • Answer: You can create a NumPy array using the numpy.array() function, passing a Python list or tuple as an argument.
  6. Explain the term "broadcasting" in the context of NumPy.

    • Answer: Broadcasting refers to the ability of NumPy to perform operations on arrays of different shapes and sizes, implicitly expanding smaller arrays to match the shape of larger arrays.
  7. What is the purpose of the NumPy arange() function?

    • Answer: The arange() function generates evenly spaced values within a specified range.
  8. How can you perform element-wise multiplication of two NumPy arrays?

    • Answer: Element-wise multiplication is performed using the * operator.
  9. What is the difference between numpy.dot() and numpy.matmul()?

    • Answer: Both functions perform matrix multiplication, but numpy.dot() is more general and works for arrays of any dimension, while numpy.matmul() is specifically designed for 2-D arrays.
  10. How do you find the transpose of a NumPy array?

    • Answer: The transpose of an array can be obtained using the numpy.transpose() function or the .T attribute.
  11. Explain the purpose of the NumPy linspace() function.

    • Answer: linspace() creates an array of evenly spaced values over a specified range, where the number of elements is specified instead of the step size.
  12. How can you flatten a NumPy array?

    • Answer: You can flatten an array using the numpy.flatten() method or the numpy.ravel() method.
  13. What is the purpose of the NumPy zeros() and ones() functions?

    • Answer: zeros() creates an array filled with zeros, and ones() creates an array filled with ones.
  14. How can you find the index of the maximum and minimum values in a NumPy array?

    • Answer: The numpy.argmax() and numpy.argmin() functions return the indices of the maximum and minimum values, respectively.
  15. Explain the use of the NumPy reshape() function.

    • Answer: reshape() changes the shape of an array, allowing you to specify the number of rows and columns.
  16. How can you concatenate two NumPy arrays?

    • Answer: Concatenation can be done using the numpy.concatenate() function or the numpy.vstack() and numpy.hstack() functions for vertical and horizontal concatenation, respectively.
  17. What is the purpose of the NumPy stack() function?

    • Answer: stack() is used to join two or more arrays along a new axis.
  18. Explain the role of the NumPy ufunc (Universal Functions).

    • Answer: Ufuncs are functions that operate element-wise on NumPy arrays, providing fast and efficient operations.
  19. How do you calculate the mean and median of a NumPy array?

    • Answer: The mean and median can be calculated using numpy.mean() and numpy.median() functions, respectively.
  20. What is the purpose of the NumPy std() function?

    • Answer: std() calculates the standard deviation of a NumPy array, measuring the spread of values.
  21. Explain the use of the NumPy random module.

    • Answer: The random module provides functions to generate random numbers and distributions.
  22. How can you find unique elements in a NumPy array?

    • Answer: The numpy.unique() function returns an array of unique elements in the input array.
  23. What is the purpose of the NumPy save() and load() functions?

    • Answer: save() is used to save a NumPy array to a binary file, and load() is used to load the array from the file.
  24. How can you perform element-wise comparison in NumPy?

    • Answer: Element-wise comparison can be done using comparison operators such as ==, >, <, etc.
  25. Explain the use of the NumPy where() function.

    • Answer: where() returns the indices of elements in an array where a specified condition is true.
  26. How do you perform element-wise addition with a scalar in NumPy?

    • Answer: Element-wise addition with a scalar is done using the + operator.
  27. What is the purpose of the NumPy loadtxt() function?

    • Answer: loadtxt() is used to load data from a text file into a NumPy array.
  28. How can you perform matrix multiplication in NumPy?

    • Answer: Matrix multiplication can be done using the numpy.matmul() function or the @ operator.
  29. Explain the use of the NumPy meshgrid() function.

    • Answer: meshgrid() creates coordinate matrices from coordinate vectors, which are useful for plotting surfaces.
  30. What is the purpose of the NumPy diag() function?

    • Answer: diag() extracts the diagonal or constructs a diagonal array.
  31. How can you calculate the dot product of two vectors in NumPy?

    • Answer: The dot product can be calculated using numpy.dot() or the @ operator.
  32. Explain the use of the NumPy einsum() function.

    • Answer: einsum() provides a powerful way to compute a variety of tensor contractions.
  33. What is the purpose of the NumPy fft module?

    • Answer: The fft module provides functions for computing the discrete Fourier transform.
  34. How can you create a diagonal matrix in NumPy?

    • Answer: A diagonal matrix can be created using the numpy.diag() function.
  35. Explain the use of the NumPy interp() function.

    • Answer: interp() performs one-dimensional linear interpolation.
  36. How can you find the inverse of a matrix in NumPy?

    • Answer: The inverse of a matrix can be found using the `numpy.linalg

.inv()` function.

  1. What is the purpose of the NumPy savez() function?

    • Answer: savez() is used to save multiple NumPy arrays into a single compressed .npz file.
  2. Explain the use of the NumPy vectorize() function.

    • Answer: vectorize() is used to convert a Python function into a vectorized function that can operate on arrays element-wise.
  3. How can you calculate the cross product of two vectors in NumPy?

    • Answer: The cross product can be calculated using numpy.cross().
  4. What is the purpose of the NumPy logspace() function?

    • Answer: logspace() generates an array of numbers spaced evenly on a logarithmic scale.
  5. Explain the use of the NumPy fill() function.

    • Answer: fill() assigns a specified value to all elements in an array.
  6. How can you calculate the determinant of a matrix in NumPy?

    • Answer: The determinant can be calculated using the numpy.linalg.det() function.
  7. What is the purpose of the NumPy corrcoef() function?

    • Answer: corrcoef() calculates the correlation coefficient matrix of a set of variables.
  8. Explain the use of the NumPy rot90() function.

    • Answer: rot90() rotates an array by 90 degrees in the plane specified by axes.
  9. How can you calculate the eigenvalues and eigenvectors of a matrix in NumPy?

    • Answer: Eigenvalues and eigenvectors can be calculated using numpy.linalg.eig().
  10. What is the purpose of the NumPy broadcast_to() function?

    • Answer: broadcast_to() broadcasts an array to a new shape.
  11. Explain the use of the NumPy histogram() function.

    • Answer: histogram() computes the histogram of a set of data.
  12. How can you calculate the cumulative sum of elements in a NumPy array?

    • Answer: The cumulative sum can be calculated using numpy.cumsum().
  13. What is the purpose of the NumPy genfromtxt() function?

    • Answer: genfromtxt() loads data from a text file, with missing values handled gracefully.
  14. Explain the use of the NumPy apply_along_axis() function.

    • Answer: apply_along_axis() applies a function to 1-D slices along the specified axis.