NumPy Q&A
20 Core Questions
Interview Prep
NumPy for ML & Scientific Computing: Interview Q&A
Short questions and answers on NumPy: ndarrays, broadcasting, vectorization and basic linear algebra.
ndarrays
Broadcasting
Vectorization
Linear Algebra
1
What is a NumPy ndarray and why is it useful?
⚡ Beginner
Answer: An ndarray is a multi-dimensional, homogeneous array providing fast vectorized operations and serving as the base for many ML libraries.
2
How do you create a NumPy array from a Python list?
⚡ Beginner
Answer: Use
np.array(my_list).
3
What are
shape and dtype attributes of an array?
⚡ Beginner
Answer:
shape gives the dimensions (rows, cols, ...), dtype gives the data type of elements.
4
What is broadcasting in NumPy?
📊 Intermediate
Answer: Broadcasting lets arrays of different but compatible shapes be combined in arithmetic by implicitly expanding dimensions.
5
Why is vectorization preferred over Python loops in NumPy code?
📊 Intermediate
Answer: Vectorized operations run in optimized C code, making them much faster and more concise than explicit Python loops.
6
How do you compute basic statistics like mean and std in NumPy?
⚡ Beginner
Answer: Use
np.mean(a), np.std(a), optionally with axis to aggregate along dimensions.
7
How do you perform matrix multiplication with NumPy arrays?
⚡ Beginner
Answer: Use the
@ operator or np.dot(A, B) / A.dot(B).
8
What is the difference between
view() and copy() in NumPy?
🔥 Advanced
Answer: A view shares the same underlying data; a copy allocates new memory. Changing a view affects the original array.
9
How do you reshape an array?
⚡ Beginner
Answer: Use
a.reshape(new_shape) or a.reshape(-1, n) to infer one dimension.
10
What is
axis in aggregation functions like sum or mean?
📊 Intermediate
Answer:
axis specifies which dimension to collapse; e.g., axis=0 aggregates rows, axis=1 aggregates columns in 2D arrays.
11
How can you generate random numbers with NumPy?
⚡ Beginner
Answer: Use
np.random module or the np.random.default_rng() generator with methods like normal, integers, choice.
12
What is boolean indexing in NumPy?
📊 Intermediate
Answer: It selects elements where a boolean mask is True, e.g.,
a[a > 0].
13
How do you stack arrays vertically and horizontally?
📊 Intermediate
Answer: Use
np.vstack([a, b]) for vertical, np.hstack([a, b]) for horizontal, or np.concatenate with axis.
14
How can you compute eigenvalues and eigenvectors with NumPy?
🔥 Advanced
Answer: Use
np.linalg.eig(A) for general matrices or np.linalg.eigh for symmetric/Hermitian ones.
15
What is the difference between
np.dot and elementwise * multiplication?
📊 Intermediate
Answer:
* multiplies elements position-wise; dot (or @) performs matrix/inner product.
16
Why is NumPy so important for machine learning in Python?
⚡ Beginner
Answer: Because libraries like pandas, scikit-learn, PyTorch, TensorFlow all rely on efficient NumPy-style arrays and operations.
17
What are some common performance pitfalls when using NumPy?
🔥 Advanced
Answer: Pitfalls: using Python loops instead of vectorization, excessive copies, not using views, mixing Python lists and arrays.
18
How do you set the random seed for reproducible NumPy results?
⚡ Beginner
Answer: With the legacy API:
np.random.seed(42); with Generator API: rng = np.random.default_rng(42).
19
Give a real-world example where NumPy is heavily used in an ML project.
⚡ Beginner
Answer: NumPy underpins feature preprocessing, custom loss functions, numerical simulations and glue code between ML libraries.
20
What is the key message to remember about NumPy for ML engineers?
⚡ Beginner
Answer: NumPy is the numerical backbone of the Python ML stack; fluent array thinking and broadcasting will make your ML code faster and cleaner.
Quick Recap: NumPy
Mastering ndarrays, indexing, broadcasting and basic linear algebra gives you the core numerical skills behind most Python ML frameworks.