NumPy Theoretical Interview Questions & Answers
1. What is NumPy?
NumPy (Numerical Python) is a library for numerical computing in Python, providing support for
multi-dimensional arrays and efficient mathematical operations.
2. How is NumPy different from Python lists?
NumPy arrays are more efficient, consuming less memory and enabling faster operations due to
their fixed data types and support for vectorized operations.
3. How do you create a NumPy array?
Using `numpy.array()`, passing a list or tuple. Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
4. What is broadcasting in NumPy?
Broadcasting allows arrays of different shapes to be used in arithmetic operations, expanding
smaller arrays automatically to match the larger ones.
5. What are some key functions in NumPy?
- `np.zeros(shape)`: Creates an array of zeros.
- `np.ones(shape)`: Creates an array of ones.
- `np.mean(array)`: Computes the mean.
- `np.std(array)`: Computes the standard deviation.
6. What is the difference between `np.linspace()` and `np.arange()`?
- `np.linspace(start, stop, num)`: Generates `num` evenly spaced values.
- `np.arange(start, stop, step)`: Generates values with a fixed step size.
7. What is the shape and dtype of a NumPy array?
- `shape`: Tuple representing array dimensions (rows, columns, etc.).
- `dtype`: Data type of array elements (e.g., int32, float64).
8. How do you reshape an array in NumPy?
Using `array.reshape(new_shape)`, where `new_shape` is the desired dimensions. Example:
arr = np.array([1,2,3,4,5,6])
arr.reshape(2,3)
9. What is the difference between `np.copy()` and `np.view()`?
- `np.copy()`: Creates a new independent copy of the array.
- `np.view()`: Creates a new view that shares data with the original array.
10. How does NumPy handle missing values?
NumPy uses `np.nan` (Not a Number) to represent missing values in floating-point arrays.
11. What is the purpose of `np.where()`?
`np.where(condition, x, y)` returns elements from `x` where the condition is True, otherwise from `y`.
It is used for conditional array operations.
12. How do you concatenate arrays in NumPy?
Using `np.concatenate((arr1, arr2), axis)`, where `axis=0` joins along rows, and `axis=1` joins along
columns.
13. What is the difference between `hstack()` and `vstack()`?
- `np.hstack((arr1, arr2))`: Horizontally stacks arrays.
- `np.vstack((arr1, arr2))`: Vertically stacks arrays.
14. What are universal functions (ufuncs) in NumPy?
Ufuncs are optimized functions for element-wise operations, such as `np.add()`, `np.subtract()`,
`np.multiply()`, and `np.exp()`.
15. How can you sort an array in NumPy?
Using `np.sort(array)`, which returns a sorted copy of the array. Use `axis` parameter to sort along
specific dimensions.
16. What is the use of `np.unique()`?
`np.unique(array)` returns the sorted unique values in an array, useful for removing duplicates.
17. What is the difference between `np.dot()` and `np.matmul()`?
- `np.dot(A, B)`: Computes dot product (works for 1D & 2D arrays).
- `np.matmul(A, B)`: Preferred for matrix multiplication (supports broadcasting).
18. How do you generate random numbers in NumPy?
Using `np.random` module, e.g., `np.random.rand()`, `np.random.randint()`, `np.random.normal()`.
19. How can you save and load NumPy arrays?
- Save: `np.save('filename.npy', array)`
- Load: `np.load('filename.npy')`
20. What are masked arrays in NumPy?
Masked arrays handle missing or invalid data by marking certain values as 'masked', preventing
them from affecting calculations. Created using `np.ma.array()`.