NumPy is one of those core Python libraries which are highly powerful and can save a lot of time for you because it handles all your numerical calculations for array-based data. Its efficient array processing capabilities make it indispensable for tasks involving large datasets or mathematical operations.
This library finds its application in everything starting from data science and machine learning, and ending with scientific modelling. Its use is very widespread in the field due to its performance, flexibility, and great ease of use with other Python libraries. Understanding NumPy provides a good background for handling a lot of advanced computing tasks.
In this blog, we will discuss more than 70 requisite NumPy interview questions. These are sectionalised for fresh graduates, intermediate level and experts. You will get more familiar with Numpy as you go through these different questions and answers.
NumPy Interview Questions and Answers for Freshers
1. How do you install NumPy?
You can install NumPy using pip, the Python package manager. Open a terminal or
command prompt and run:
pip install numpy
For specific versions:
pip install numpy==1.21.0
To verify the installation:
import numpy as np
print(np.__version__)
2. How do I create a NumPy array?
You can create a NumPy array using the np.array() function.
Example:
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr)
Other methods:
- Using np.zeros((2, 2)) for an array of zeros.
- Using np.ones((3, 3)) for an array of ones.
- Using np.arange(0, 10, 2) for a range of numbers.
3. What is the purpose of NumPy in Python?
- Efficient Data Handling: When you want a perfect data handling tool, NumPy offers you whole array manipulations, which are not just fast but also optimised for memory usage.
- Integration: Libraries like Pandas, SciPy and Matplotlib are well-integrated for perfect use.
- Operations in Maths: This also covers linear algebra functions, Fourier transform functions, and functions to generate random numbers.
4. Can you distinguish between single precision and double precision when you are using a NumPy array?
| Aspect | Single Precision (float32) | Double Precision (float64) |
| Bit Size | 32 bits | 64 bits |
| Precision | Approximately 7 decimal digits | Approximately 16 decimal digits |
| Memory Usage | Uses less memory per element (4 bytes) | Uses more memory per element (8 bytes) |
| Performance | Faster computations due to smaller size | Slower compared to single-precision |
| Use Cases | Graphics, machine learning models where speed is critical | Scientific computations requiring high precision |
| Range | ±3.4e−38 to ±3.4e+38 | ±1.7e−308 to ±1.7e+308 |
5. You have to find the dot product of two arrays. Find it with the help of NumPy.
Use the np.dot() function.
Example:
import numpy as np
a = np.array([10, 12, 14])
b = np.array([3, 2, 1])
dot_product = np.dot(a, b)
print(dot_product) # Output: 68
For matrices:
matrix_a = np.array([[10, 20], [1, 2]])
matrix_b = np.array([[3, 6], [30, 40]])
result = np.dot(matrix_a, matrix_b)
print(result)
6. What are the main features of NumPy?
- N-dimensional Array: Supports multi-dimensional arrays.
- Mathematical Functions: Includes operations for linear algebra, statistics, and more.
- Broadcasting: Performs operations on arrays of different shapes.
- Integration: Can interface with C, C++, and Fortran for performance.
7. Why is NumPy popular in data science and machine learning?
- Performance: Faster than Python lists for numerical operations.
- Library Support: Essential for libraries like TensorFlow and Pandas.
- Ease of Use: Simplifies tasks like matrix operations, statistical analysis, and array manipulation.
8. What are some common data types supported by NumPy?
| Data Type | Description |
| int32 | 32-bit integer |
| float64 | 64-bit floating point |
| complex | Complex numbers |
| bool | Boolean values |
| object | Python objects |
Example:
arr = np.array([1, 2, 3], dtype='float64')
print(arr.dtype) # Output: float64
9. Can you distinguish between a shallow copy and a deep copy when you are using the NumPy library?
| Aspect | Shallow Copy | Deep Copy |
| Definition | References original data | Creates a new array |
| Changes | Reflect in both copies | Independent of each other |
Example:
import numpy as np
a = np.array([1, 2, 3])
shallow = a.view()
deep = a.copy()
10. How do I access elements in a NumPy array?
- Single Element: Use indexing.
arr = np.array([10, 20, 30])
print(arr[1]) # Output: 20
- Slices:
print(arr[1:]) # Output: [20, 30]
- Multidimensional:
matrix = np.array([[1, 2], [3, 4]])
print(matrix[1, 0]) # Output: 3
11. Define the min and max function in NumPy.
- min(): Returns the smallest value.
- max(): Returns the largest value.
Example:
arr = np.array([10, 20, 30])
print(np.min(arr)) # Output: 10
print(np.max(arr)) # Output: 30
For multidimensional arrays:
matrix = np.array([[1, 2], [3, 4]])
print(np.min(matrix, axis=0)) # Output: [1, 2]
print(np.max(matrix, axis=1)) # Output: [2, 4]
12. What is the distinction between np.dot() and np.matmul()?
| Aspect | np.dot() | np.matmul() |
| Operation Type | Dot product for vectors; matrix multiplication for matrices | Matrix multiplication for 2D arrays and higher |
| Behavior with Higher Dimensions | Performs tensor dot product, which can be less intuitive | Handles stacking of matrices for higher dimensions |
| Broadcasting | Limited broadcasting capabilities | Supports more extensive broadcasting |
| Product Type | Scalar, vector, or matrix based on input dimensions | Strictly matrix product for 2D or higher arrays |
| Alternative Operators | Can be replaced by @ operator in Python 3.5+ | Same as @ operator |
13. Define the mean function in NumPy.
The np.mean() function returns the mean of the elements in an array.
Example:
arr = np.array([1, 2, 3, 4])
print(np.mean(arr)) # Output: 2.5f
For multidimensional arrays:
matrix = np.array([[1, 2], [3, 4]])
print(np.mean(matrix, axis=0)) # Output: [2, 3]
14. How are NumPy Arrays better than Lists in Python?
| Feature | NumPy Arrays | Python Lists |
| Speed | Faster for numerical tasks | Slower |
| Memory Efficiency | Compact storage | Requires more memory |
| Broadcasting Support | Yes | No |
| Element-wise Operations | Built-in | Requires loops |
15. How to perform element-wise operations on NumPy arrays?
Use arithmetic operators directly:
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print(arr1 + arr2) # Output: [5 7 9]
print(arr1 * arr2) # Output: [4 10 18]
16. Define the var function in NumPy.
The np.var() function calculates the variance of elements in an array.
Example:
arr = np.array([1, 2, 3, 4])
print(np.var(arr)) # Output: 1.25
17. What is the significance of the random module in NumPy?
The np.random module generates random numbers and performs sampling operations:
- Random numbers:random.rand()
- Normal distribution:random.randn()
- Integers:random.randint(1, 10)
Example:
random_array = np.random.rand(3)
print(random_array) # Example Output: [0.5 0.8 0.2]
18. Convert a multidimensional array to a 1D array.
Use the flatten() or ravel() function:
matrix = np.array([[1, 2], [3, 4]])
flattened = matrix.flatten()
print(flattened) # Output: [1 2 3 4]
19. How can you create a NumPy array from a Python list?
Use the np.array() function:
python_list = [1, 2, 3, 4]
np_array = np.array(python_list)
print(np_array) # Output: [1 2 3 4]
20. What is Matrix Inversion in NumPy?
Matrix inversion is finding a matrix A⁻¹ such that A * A⁻¹ = I, where I is the identity matrix.
Use np.linalg.inv():
matrix = np.array([[1, 2], [3, 4]])
inverse = np.linalg.inv(matrix)
print(inverse)
21. Write a NumPy code snippet to create an array of zeros.
zeros_array = np.zeros((3, 3))
print(zeros_array)
Output:
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
22. How can you concatenate two NumPy arrays vertically?
Use np.vstack():
arr1 = np.array([1, 2])
arr2 = np.array([3, 4])
result = np.vstack((arr1, arr2))
print(result)
# Output:
# [[1 2]
# [3 4]]
23. How can you create an array with the same values?
Use np.full():
arr = np.full((3, 3), 7)
print(arr)
# Output:
# [[7 7 7]
# [7 7 7]
# [7 7 7]]
Use np.ones() or np.zeros():
ones = np.ones((2, 2))
zeros = np.zeros((2, 2))
24. Your task is to compute the Fourier transformation in a signal. Write the procedure for the same using NumPy.
You can calculate a signal’s Fourier transform with NumPy’s np.fft.fft() function. This takes in the time domain of a signal into the frequency domain. Thus enabling you to look at exactly what frequencies exist. That is:
import numpy as np
signal = np.array([0, 1, 0, -1])
fft_result = np.fft.fft(signal)
print(fft_result)
25. How do you remove missing or null values from a NumPy array?
To remove missing or null values from a NumPy array, you can use boolean indexing or filtering. Although NumPy does not natively support NaN handling like Pandas, you can still identify NaN values using np.isnan().
For example:
import numpy as np
arr = np.array([1, 2, np.nan, 4])
cleaned_array = arr[~np.isnan(arr)]
print(cleaned_array) # Output: [1. 2. 4.]
26. What is the difference between slicing and indexing in NumPy?
| Aspect | Indexing | Slicing |
| Purpose | Accesses specific elements | Extracts a subarray |
| Input | Single value or index array | Start:Stop:Step format |
| Returns | A single value or a lower-dimensional array | A view or copy of the original array |
27. What are some of the limitations of NumPy?
- Memory Usage: NumPy arrays are less memory-efficient than specialised data structures for sparse data.
- Limited Functionality: This does not support features like handling categorical data or direct visualisation.
- Steep Learning Curve: Requires understanding of array broadcasting, dimensions, and indexing.
- Single Data Type: Arrays must-have elements of the same type, which limits flexibility compared to Python lists.
- No Built-in Null Support: Unlike Pandas, it lacks direct handling of NaN and None values for missing data.
Despite these limitations, NumPy remains a powerful library for numerical computing.
28. What is the use of diag() square matrix?
The np.diag() function extracts the diagonal elements of a square matrix or creates a diagonal matrix from a 1D array.
For example:
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
diagonal = np.diag(matrix)
print(diagonal) # Output: [1 5 9]
To create a diagonal matrix:
array = np.array([1, 2, 3])
diag_matrix = np.diag(array)
print(diag_matrix)
This function is particularly useful in linear algebra, where diagonal elements play a key role in operations like eigenvalue decomposition.
29. What is negative indexing in NumPy arrays?
Negative indexing is very useful when you don’t know the exact size of the array. All you have to do is assign an index to -1, which ultimately means the last index in the array, then -2 to the second last index in the array, and so on.
For example:
import numpy as np
arr = np.array([10, 20, 30, 40])
print(arr[-1]) # Output: 40
print(arr[-2]) # Output: 30
It also works with multidimensional arrays:
matrix = np.array([[1, 2], [3, 4]])
print(matrix[-1, -1]) # Output: 4
30. What is np.sum() in NumPy and np.sum() in Python?
| Aspect | np.sum() | sum() |
| Library | NumPy | Built-in Python |
| Input Types | NumPy arrays, lists, tuples | Iterables like lists, tuples, etc. |
| Performance | Optimised for large numerical data | Slower for large datasets |
| Axis Parameter | Supports multi-dimensional arrays with axis argument | Only works with 1D iterables |
| Data Types | Handles various numerical data types efficiently | Primarily for integers and floats |
| Return Type | Returns a NumPy scalar or array | Returns a Python scalar |
| Broadcasting | Supports broadcasting for element-wise summation | Does not support broadcasting |
31. Can you create a plot in NumPy?
NumPy itself does not have plotting functions. However, you can use NumPy in conjunction with Matplotlib, in order to create various different types of plots. For example, you can create data with the help of NumPy and then use Matplotlib to make interesting plots.
Example:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.title("Sine Wave")
plt.show()
Here, NumPy generates the data, while Matplotlib handles the plotting. This combination is widely used in data visualisation tasks.
32. How do you concatenate 2 NumPy arrays?
Example:
import numpy as np
arr1 = np.array([1, 2])
arr2 = np.array([3, 4])
result = np.concatenate((arr1, arr2))
print(result) # Output: [1 2 3 4]
For multidimensional arrays:
matrix1 = np.array([[1, 2]])
matrix2 = np.array([[3, 4]])
result = np.concatenate((matrix1, matrix2), axis=0)
print(result)
33. How do you multiply 2 NumPy array matrices?
Matrix multiplication is performed using np.matmul() or the @ operator.
Example:
import numpy as np
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])
result = np.matmul(matrix1, matrix2)
print(result)
This calculates the dot product of rows and columns. For element-wise multiplication, use the * operator:
result = matrix1 * matrix2
print(result)
34. What are the differences between structured arrays and record arrays in NumPy?
| Feature | Structured Arrays | Record Arrays |
| Definition | Arrays with complex data types (fields with names) | Subclass of structured arrays with attribute access |
| Access Method | Access fields using indexing (e.g., arr[‘field’]) | Access fields as attributes (e.g., arr.field) |
| Functionality | Suitable for heterogeneous data storage | Enhanced access and usability over structured arrays |
| Creation | np.array([…], dtype=[(‘field1’, type), …]) | arr.view(np.recarray) |
| Use Cases | Storing records with multiple data types | Easier access for data analysis and manipulation |
35. How to reverse a NumPy array?
You can reverse an array using slicing with a step of -1.
Example:
import numpy as np
arr = np.array([10, 20, 30, 40])
reversed_arr = arr[::-1]
print(reversed_arr) # Output: [40 30 20 10]
For multidimensional arrays:
matrix = np.array([[1, 2], [3, 4]])
reversed_matrix = matrix[::-1, ::-1]
print(reversed_matrix)
This approach reverses both rows and columns effectively.

POSTGRADUATE PROGRAM IN
Multi Cloud Architecture & DevOps
Master cloud architecture, DevOps practices, and automation to build scalable, resilient systems.
NumPy Interview Questions and Answers for Intermediate
36. Can you sort a NumPy array in any order?
Use np.sort() for ascending order:
import numpy as np
arr = np.array([3, 1, 2, 4])
print(np.sort(arr)) # Output: [1 2 3 4]
Use slicing for descending order:
print(np.sort(arr)[::-1]) # Output: [4 3 2 1]
Sort along a specific axis in a multidimensional array:
matrix = np.array([[3, 1], [4, 2]])
print(np.sort(matrix, axis=0))
37. How can you access elements in a NumPy array based on specific conditions?
Use conditional indexing:
arr = np.array([1, 2, 3, 4, 5])
print(arr[arr > 3]) # Output: [4 5]
Combine conditions:
print(arr[(arr > 2) & (arr < 5)]) # Output: [3 4]
38. Can you generate random numbers which have a normal distribution? How can you do this using NumPy?
Here, you can use np.random.randn(). This gives you the standard normal distribution:
random_numbers = np.random.randn(5)
print(random_numbers)
For custom mean and standard deviation:
mean, std = 5, 2
numbers = np.random.normal(mean, std, 5)
print(numbers)
39. Can you convert Pandas DataFrame into a NumPy array?
You can use DataFrame.values or to_numpy():
import pandas as pd
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
np_array = df.to_numpy()
print(np_array)
# Output:
# [[1 3]
# [2 4]]
How to generate random numbers with NumPy?
Use np.random.rand() for uniform distribution:
random_numbers = np.random.rand(5)
print(random_numbers)
Use np.random.randint() for integers:
random_integers = np.random.randint(1, 10, 5)
print(random_integers)
40. Can you modify the overall data type of any NumPy array?
Yes, you can use astype():
arr = np.array([1, 2, 3])
float_arr = arr.astype('float64')
print(float_arr) # Output: [1. 2. 3.]
41. How do you reshape a NumPy array?
Use np.reshape():
arr = np.array([1, 2, 3, 4, 5, 6])
reshaped = arr.reshape(2, 3)
print(reshaped)
# Output:
# [[1 2 3]
# [4 5 6]]
Use -1 to let NumPy calculate the dimension:
reshaped = arr.reshape(-1, 2)
42. What is a masked array in NumPy?
Masked arrays allow the hiding of specific elements.
You can use np.ma.masked_where() to mask based on a condition:
arr = np.array([1, 2, 3, 4])
masked = np.ma.masked_where(arr > 2, arr)
print(masked)
# Output: [1 2 — –]
Masked arrays are useful in computations where some data points are invalid or missing.
43. How to use NumPy with Matplotlib?
It works seamlessly with NumPy and Matplotlib for data visualisation. NumPy handles the numerical calculations, while Matplotlib creates the plots.
Steps to Use NumPy with Matplotlib:
Import Libraries:
import numpy as np
import matplotlib.pyplot as plt
Generate Data with NumPy:
x = np.linspace(0, 10, 100)
y = np.sin(x)
Create Plot with Matplotlib:
plt.plot(x, y)
plt.title("Sine Wave")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
44. How is arr[:,0] different from arr[:, [0]]?
Understanding slicing differences is key in NumPy.
| Aspect | arr[:, 0] | arr[:, [0]] |
| Result Shape | 1D array (n,) | 2D array (n, 1) |
| Usage | Access as a flat array | Maintain 2D structure |
Example:
import numpy as np
arr = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
col1 = arr[:, 0]
print(col1) # Output: [1 4 7]
print(col1.shape) # Output: (3,)
col2 = arr[:, [0]]
print(col2)
# Output:
# [[1]
# [4]
# [7]]
print(col2.shape) # Output: (3, 1)
45. How do we check for an empty array (or zero elements array)?
To determine if a NumPy array is empty, use the size attribute.
Method: Using size Attribute
import numpy as np
empty_array = np.array([])
non_empty_array = np.array([1, 2, 3])
if empty_array.size == 0:
print("The array is empty.")
else:
print("The array is not empty.")
if non_empty_array.size == 0:
print("The array is empty.")
else:
print("The array is not empty.")
Output:
The array is empty.
The array is not empty.
Alternative Method: Using len()
if len(empty_array) == 0:
print(“The array is empty.”)
Note:
- size is reliable for multidimensional arrays.
- An array with shape (0, 3) is considered empty.
46. How can you find the dimensions and shape of a NumPy array?
NumPy provides attributes to determine an array’s structure.
Key Attributes:
- ndim: Number of dimensions.
- shape: Tuple indicating the size in each dimension.
- size: Total number of elements.
Example:
import numpy as np
array_1d = np.array([1, 2, 3])
array_2d = np.array([[1, 2, 3], [4, 5, 6]])
array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(array_1d.ndim, array_1d.shape, array_1d.size) # Output: 1 (3,) 3
print(array_2d.ndim, array_2d.shape, array_2d.size) # Output: 2 (2, 3) 6
print(array_3d.ndim, array_3d.shape, array_3d.size) # Output: 3 (2, 2, 2) 8
47. How can you identify outliers in a NumPy array?
Outliers can be identified using the Interquartile Range (IQR):
data = np.array([10, 12, 14, 100, 16, 18])
q1 = np.percentile(data, 25)
q3 = np.percentile(data, 75)
iqr = q3 - q1
lower_bound = q1 - 1.5 * iqr
upper_bound = q3 + 1.5 * iqr
outliers = data[(data < lower_bound) | (data > upper_bound)]
print(outliers)
# Output: [100]
48. Discuss the purpose of the np.reshape() function in NumPy, giving an example.
The np.reshape() function resizes an existing array without changing the data.
Purpose:
- Flexibility: Convert between different dimensions.
- Data Preparation: Align data for machine learning models.
- Matrix Operations: Facilitate mathematical computations.
Example:
import numpy as np
original = np.array([1, 2, 3, 4, 5, 6])
reshaped = np.reshape(original, (2, 3))
print(reshaped)
# Output:
# [[1 2 3]
# [4 5 6]]
Using -1: Allows NumPy to automatically determine one dimension.
python
Copy code
reshaped = np.reshape(original, (3, -1))
print(reshaped)
# Output:
# [[1 2]
# [3 4]
# [5 6]]
49. One-dimensional, two-dimensional, and multi-dimensional arrays. Can you distinguish these three types of arrays?
NumPy supports arrays of various sizes. Each size is suitable for different jobs.
| Dimension | Description | Example |
| 1D Array | Single axis, linear sequence | [1, 2, 3, 4] |
| 2D Array | Two axes, rows and columns | [[1, 2], [3, 4]] |
| Multi-Dimensional | Three or more axes, complex structures | [[[1,2],[3,4]], [[5,6],[7,8]]] |
50. How does NumPy handle numerical exceptions?
NumPy manages numerical exceptions like division by zero or overflow using its error-handling system.
Default Behavior:
- Warnings: NumPy issues runtime warnings.
- Results: Operations may return inf or nan without stopping execution.
Managing Exceptions with np.seterr():
import numpy as np
# Set behavior for floating-point errors
np.seterr(divide='raise', over='ignore', under='ignore', invalid='warn')
try:
result = np.array([1, 2, 3]) / 0
except FloatingPointError as e:
print("Error:", e)
Options:
- ‘ignore’: Do nothing.
- ‘warn’: Issue a warning.
- ‘raise’: Raise an exception.
- ‘call’: Call a specified function.
Example: Handling Division by Zero:
np.seterr(divide=’warn’)
result = np.array([1, 2, 3]) / 0
print(result) # Output: [inf inf inf] with a warning
51. How to Get the Eigenvalues of a Matrix.
Eigenvalues are essential in various scientific and engineering fields. NumPy provides functions to compute them efficiently.
- Understanding Eigenvalues: For a square matrix A, an eigenvalue λ satisfies:
A * v = λ * v
where v is the eigenvector.
Using np.linalg.eig():
import numpy as np
matrix = np.array([[4, -2],
[1, 1]])
eigenvalues, eigenvectors = np.linalg.eig(matrix)
print("Eigenvalues:", eigenvalues)
# Output: [3. 2.]
Explanation:
- eigenvalues: Array of eigenvalues.
- eigenvectors: Corresponding eigenvectors.
52. Find a Matrix or Vector Norm Using NumPy.
Norms measure the size or length of vectors and matrices, crucial in optimisation and machine learning.
Types of Norms:
- Vector Norms:
- L1 Norm: Sum of absolute values.
- L2 Norm: Euclidean distance.
- Matrix Norms:
- Frobenius Norm: Square root of the sum of squares of all elements.
Using np.linalg.norm():
import numpy as np
# Vector example
vector = np.array([3, 4])
l1 = np.linalg.norm(vector, ord=1)
l2 = np.linalg.norm(vector, ord=2)
print("L1 Norm:", l1) # Output: 7.0
print("L2 Norm:", l2) # Output: 5.0
# Matrix example
matrix = np.array([[1, 2], [3, 4]])
fro = np.linalg.norm(matrix, ord='fro')
print("Frobenius Norm:", fro) # Output: 5.477225575051661
Key Points:
- ord Parameter: Specifies the type of norm.
- Vectors: Useful in measuring distance and regularization.
- Matrices: Important in stability and optimisation problems.
Applications:
- Machine Learning: Regularization to prevent overfitting.
- Optimisation: Defining objective functions.
- Numerical Analysis: Assessing matrix properties.
NumPy’s norm function provides a versatile tool for various computational needs.
53. Can you calculate the QR Decomposition of a Given Matrix? You must use NumPy for this.
QR decomposition splits the matrix into an orthogonal matrix Q and an upper triangular matrix R.
Purpose:
- Solving Linear Systems: Efficiently solve equations.
- Eigenvalue Algorithms: Basis for iterative methods.
- Numerical Stability: Improves computational accuracy.
Using np.linalg.qr():
import numpy as np
A = np.array([[12, -51, 4],
[6, 167, -68],
[-4, 24, -41]])
Q, R = np.linalg.qr(A)
print("Matrix Q:\n", Q)
print("\nMatrix R:\n", R)
Output:
Matrix Q:
[[ -6. 69. -58. ]
[ -3. -158. 6. ]
[ 2. 6. 33. ]]
Matrix R:
[[-14. 21. -14.]
[ 0. 175. -70.]
[ 0. 0. 35.]]
Applications:
- Least Squares Problems: Find best-fit solutions.
- QR Algorithm: Compute eigenvalues.
- Signal Processing: Decompose signals for analysis.
NumPy’s QR Decomposition function is a powerful tool for various matrix operations.
54. What is the Difference Between the shape and size Attributes of the NumPy Array?
Understanding shape and size is fundamental for array manipulation.
| Attribute | Description | Example |
| shape | Tuple indicating the size in each dimension. | (3, 4) for 2D array |
| size | Total number of elements in the array. | 12 for (3, 4) array |
Example:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print("Shape:", arr.shape) # Output: (2, 3)
print("Size:", arr.size) # Output: 6
55. What are Universal Functions in NumPy?
Universal Functions, or ufuncs, are core to NumPy’s performance, enabling fast element-wise operations.
Key Features:
- Element-Wise Operations: Apply functions to each array element.
- Broadcasting: Handle arrays of different shapes seamlessly.
- Vectorization: Eliminate the need for explicit loops, enhancing speed.
Common Ufuncs:
- Arithmetic: add(), np.subtract(), np.multiply(), np.divide()
- Trigonometric:sin(), np.cos()
- Exponential:exp(), np.log()
Example:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
# Element-wise addition
c = np.add(a, b)
print(c) # Output: [5 7 9]
# Trigonometric function
angles = np.array([0, np.pi/2, np.pi])
sine = np.sin(angles)
print(sine)
# Output: [0.0, 1.0, 0.0]
NumPy Interview Questions and Answers for Experience Professionals
56. Is SIMD used by NumPy?
Yes, NumPy implements SIMD (single instruction, multiple data) through custom low-level libraries such as BLAS and LAPACK. These libraries allow NumPy to perform the same operations on multiple data points simultaneously, specially optimised for calculating large numbers. By taking advantage of SIMD instructions, NumPy can handle vector addition. Even efficiently multiplies matrices and more. This parallel processing capability greatly accelerates array operations, making NumPy a powerful tool for scientific computing and data analysis. Modern CPUs with advanced SIMD features also help increase NumPy performance. This ensures that complex mathematical operations are carried out quickly.
57. How is vstack() different from hstack() in NumPy?
| Function | Description | Example |
| vstack() | Stacks arrays vertically (row-wise) | Combines two 1D arrays as rows in a 2D array |
| hstack() | Stacks arrays horizontally (column-wise) | Concatenates two 1D arrays side by side |
Example:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
v = np.vstack((a, b))
print(v)
# Output:# [[1 2 3]
# [4 5 6]]
h = np.hstack((a, b))
print(h)
# Output: [1 2 3 4 5 6]
Here, for adding rows, you can use vstack() and when you need to add columns, you can use hstack().
58. How is fliplr different from flipud methods in NumPy?
fliplr and flipud are used to flip arrays horizontally or vertically.
| Method | Description | Example |
| fliplr() | Flips array left to right (horizontally) | Reverses columns in each row |
| flipud() | Flips array up to down (vertically) | Reverses rows in the array |
Example:
import numpy as np
arr = np.array([[1, 2, 3],
[4, 5, 6]])
flipped_lr = np.fliplr(arr)
print(flipped_lr)
# Output:
# [[3 2 1]
# [6 5 4]]
flipped_ud = np.flipud(arr)
print(flipped_ud)
# Output:
# [[4 5 6]
# [1 2 3]]
Here, you can use fliplr for horizontal flips and flipud for vertical flips.
How will you implement the moving average for the 1D array in NumPy?
Implementing a moving average can be achieved using convolution.
Steps:
- Define the window size.
- Create a window with equal weights.
- Use np.convolve() to compute the moving average.
Example:
import numpy as np
def moving_average(arr, window_size):
window = np.ones(window_size) / window_size
return np.convolve(arr, window, mode='valid')
data = np.array([1, 2, 3, 4, 5, 6])
avg = moving_average(data, 3)
print(avg)
# Output: [2. 3. 4. 5.]
This method efficiently calculates the average over a sliding window without explicit loops.
59. What happens when we use the array_split() method for splitting the NumPy array?
array_split() divides an array into multiple sub-arrays, handling uneven splits gracefully.
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
# Split into 3 parts
split = np.array_split(arr, 3)
print(split)
# Output: [array([1, 2, 3]), array([4, 5, 6]), array([7])]
Hare, array_split() will ensure if all elements are included or not, even if splits could be uneven.
60. What happens when the split() method is used for splitting NumPy arrays?
When you use split() method, it will divide any given array into equal parts. But you must give a divisible number, which can equally divide the array into that divisible number of splits only.
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
# Split into 3 equal parts
split = np.split(arr, 3)
print(split)
# Output: [array([1, 2]), array([3, 4]), array([5, 6])]
# Uneven split raises ValueError
np.split(arr, 4) # Raises ValueError
Use split() when you need equal-sized sub-arrays and the array size allows it.
61. How is Vectorization Related to Broadcasting in NumPy?
Vectorisation and broadcasting are interconnected features that enhance NumPy’s efficiency.
Vectorisation:
- Definition: Performing operations on entire arrays without explicit loops.
- Benefit: Faster and more readable code.
Broadcasting:
- Definition: Automatically expanding arrays to compatible shapes for operations.
- Purpose: Enables operations on arrays of different sizes.
Relation: When you use vectorization, it generally relies on broadcasting, which is used to perform seamless element-wise operations, across varying array shapes.
Example:
import numpy as np
a = np.array([1, 2, 3])
b = 2
result = a * b # Broadcasting scalar to array
print(result)
# Output: [2 4 6]
Here, broadcasting allows the scalar b to be multiplied with each element of array a via vectorisation.
62. How do you find the local peaks (or maxima) in a 1-D NumPy Array?
Identifying local maxima involves comparing each element with its neighbours.
Steps:
- Compare each element (excluding the first and last) with its adjacent elements.
- Use boolean indexing to identify peaks.
Example:
import numpy as np
def find_local_maxima(arr):
return (arr[1:-1] > arr[:-2]) & (arr[1:-1] > arr[2:])
data = np.array([1, 3, 2, 4, 3, 5, 4])
peaks = find_local_maxima(data)
peak_indices = np.where(peaks)[0] + 1
print(peak_indices) # Output: [1 3 5]
print(data[peak_indices]) # Output: [3 4 5]
This method efficiently locates indices of local peaks within a 1D array.
63. What do you understand by Vectorization in NumPy?
Vectorization in NumPy refers to executing operations on entire arrays without explicit loops, leveraging optimised low-level implementations.
Example:
import numpy as np
# Vectorized addition
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = a + b
print(c)
# Output: [5 7 9]
Here, the addition is applied element-wise across the arrays without looping.
64. Write a program for interchanging two axes of the NumPy array.
Interchanging axes can be done using np.swapaxes() or np.transpose().
Using swapaxes():
import numpy as np
# Original array with shape (2, 3, 4)
arr = np.random.rand(2, 3, 4)
# Swap axis 0 and axis 2
swapped = np.swapaxes(arr, 0, 2)
print(swapped.shape) # Output: (4, 3, 2)
Using transpose():
# Transpose axes
transposed = arr.transpose(2, 1, 0)
print(transposed.shape) # Output: (4, 3, 2)
These functions effectively interchange the specified axes, altering the array’s structure for different computational needs.
65. Write a program for changing the dimension of a NumPy array.
Changing dimensions can be achieved using reshape() or np.newaxis.
Example Using reshape():
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
# Reshape to 2x3
reshaped = arr.reshape((2, 3))
print(reshaped)
# Output:
# [[1 2 3]
# [4 5 6]]
Example Using np.newaxis:
# Convert to 2D column vector
column = arr[:, np.newaxis]
print(column)
# Output:
# [[1]
# [2]
# [3]
# [4]
# [5]
# [6]]
These methods adjust the array’s dimensions, facilitating various computational and data processing tasks.
66. Write a program for inserting space between characters of all elements in a NumPy array.
To insert spaces between characters, use NumPy’s string operations.
Example:
import numpy as np
arr = np.array(['Hello', 'World'])
# Insert space between characters
spaced = np.char.join(' ', arr)
print(spaced)
# Output:
# ['H e l l o' 'W o r l d']
Alternative Method:
spaced = np.array([‘ ‘.join(list(s)) for s in arr])
print(spaced)
Both approaches effectively add spaces between characters in each string element of the array.
67. Write a program for creating an integer array with values belonging to the range 10 and 60.
Use np.arange() or np.linspace() to create the desired range.
Using arange():
import numpy as np
arr = np.arange(10, 61) # 10 to 60 inclusive
print(arr)
Using linspace() with dtype=int:
arr = np.linspace(10, 60, num=51, dtype=int)
print(arr)
Output:
[10 11 12 … 58 59 60]
These methods generate an integer array containing values from 10 to 60 efficiently.
68. What is the difference between NumPy’s arange() and linspace() functions?
arange() and linspace() both create sequences of numbers but differ in parameters and usage.
| Function | Description | Parameters |
| arange() | Creates array with evenly spaced values based on start, stop, step | start, stop, step |
| linspace() | Creates array with a specified number of evenly spaced values between start and stop | start, stop, num |
Key Differences:
- Step vs. Number of Points: arange() uses step size, while linspace() specifies the number of points.
- Inclusivity: linspace() includes the stop value by default (endpoint=True), whereas arange() excludes it.
- Floating Point Precision: linspace() is preferred for precise control over the number of elements.
69. Compare NumPy’s array() and asarray() functions.
array() and asarray() both create NumPy arrays but differ in copying behavior.
| Function | Description | Copy Behavior |
| array() | Creates a new array object from input data | Always makes a copy |
| asarray() | Converts input to array without copying if possible | Does not copy if input is already an array |
Example:
import numpy as np
lst = [1, 2, 3]
arr1 = np.array(lst)
arr2 = np.asarray(lst)
# If input is already an array
existing_arr = np.array([4, 5, 6])
arr3 = np.array(existing_arr) # Creates a copy
arr4 = np.asarray(existing_arr) # References existing array
print(arr3 is existing_arr) # Output: False
print(arr4 is existing_arr) # Output: True
Choose based on whether a new copy is necessary or referencing is sufficient.
70. Explain the difference between NumPy’s flatten() and ravel() functions.
| Feature | flatten() | ravel() |
| Functionality | Returns a copy of the array flattened into 1D | Returns a flattened view of the array if possible |
| Memory | Always creates a new copy | Returns a view when possible, otherwise a copy |
| Modifiability | Changes to the flattened array do not affect the original | Changes affect the original array if a view is returned |
| Method Type | Method of ndarray | Method of ndarray |
| Usage Scenario | When an independent 1D array is needed | When a flattened array is needed without extra memory |
71. How do NumPy’s where() and nonzero() methods differ?
| Aspect | np.where() | np.nonzero() |
| Purpose | Select elements based on a condition | Find indices of non-zero elements |
| Usage | np.where(condition, x, y) or np.where(condition) | np.nonzero(array) |
| Return Type | Tuple of arrays for indices or modified array | Tuple of arrays containing indices |
| Flexibility | Can return elements from two arrays based on condition | Only returns indices where condition is True |
| Common Use | Conditional selection and assignment | Identifying locations of non-zero values |
72. How is the broadcasting of arrays different from standard element-wise operations?
| Feature | Broadcasting | Standard Element-Wise Operations |
| Definition | Automatically expands arrays to match shapes | Requires arrays to have identical shapes |
| Flexibility | Allows operations on arrays with different dimensions | Limited to same-shaped arrays |
| Mechanism | Follows specific rules to align array dimensions | Directly applies operations element by element |
| Use Cases | Performing operations between scalar and array, or differently shaped arrays | Operations on arrays of the same shape |
| Performance | Optimises memory and computation by avoiding explicit replication | May require explicit replication for shape matching |
73. How does NumPy’s np.save() differ from np.savetxt()?
| Feature | np.save() | np.savetxt() |
| File Format | Binary .npy format | Text-based formats like .txt, .csv |
| Data Types | Supports all NumPy data types, including objects | Primarily numerical data, limited to strings |
| Efficiency | Faster and more space-efficient due to binary storage | Slower and larger file sizes due to text encoding |
| Compatibility | Best used with NumPy for loading via np.load() | Can be opened with text editors and other software |
| Human-Readable | Not human-readable | Human-readable |
| Usage Scenario | Saving and loading NumPy arrays for computational use | Exporting data for sharing, analysis in other tools |
Conclusion
NumPy remains an indispensable tool for anyone working with numerical data in Python. Its powerful array structures and optimised performance make it ideal for tasks ranging from simple data manipulation to complex scientific computations. Whether you’re a beginner or an experienced professional, mastering NumPy can significantly enhance your data processing capabilities.
This comprehensive list of over 70 interview questions and answers covers all essential aspects, helping you build confidence and demonstrate your expertise effectively. Embrace NumPy to streamline your workflows and excel in data-driven roles. Learn Python and its libraries like NumPy with the Integrated Program in Data Science, Artificial Intelligence & Machine Learning offered by Hero Vired in collaboration with Open Learning and get a professional certificate.
How is NumPy used in data analysis?
Can NumPy be integrated with other Python libraries?
Is NumPy suitable for machine learning applications?
How does NumPy improve performance compared to standard Python lists?
What are some common functions used in NumPy for data manipulation?
Can NumPy handle missing data?
What are the benefits of using NumPy for numerical computations?
Updated on December 17, 2024
