NumPy Interview Questions

Checkout Vskills Interview questions with answers in NumPy to prepare for your next job role. The questions are submitted by professionals to help you to prepare for the Interview.

Q.1 What is NumPy?
NumPy is a fundamental library for numerical computing in Python, providing support for arrays, matrices, and many mathematical functions to operate on these data structures.
Q.2 How do you create a NumPy array?
Use numpy.array() with a list or tuple: np.array([1, 2, 3]).
Q.3 What is the difference between a Python list and a NumPy array?
NumPy arrays are more efficient for numerical operations and support vectorized operations, while Python lists are general-purpose containers and do not support element-wise operations.
Q.4 How do you create a NumPy array with specific dimensions and values?
Use functions like np.zeros((rows, cols)), np.ones((rows, cols)), or np.full((rows, cols), value) to create arrays with specific dimensions and values.
Q.5 What is broadcasting in NumPy?
Broadcasting is a technique that allows NumPy to perform operations on arrays of different shapes by automatically expanding the smaller array to match the shape of the larger array.
Q.6 How do you perform element-wise operations on NumPy arrays?
Element-wise operations are performed using standard arithmetic operators, e.g., np.array1 + np.array2 for addition.
Q.7 How do you index and slice NumPy arrays?
Use indexing and slicing syntax similar to Python lists. For example, arr[2:5] slices the array from index 2 to 4.
Q.8 How can you reshape a NumPy array?
Use the reshape() method: arr.reshape((new_shape)). The total number of elements must remain the same.
Q.9 What are some common functions for array aggregation in NumPy?
Common functions include np.sum(), np.mean(), np.max(), np.min(), and np.std() for summation, mean, maximum, minimum, and standard deviation, respectively.
Q.10 How do you handle missing values in NumPy arrays?
Use np.isnan() to identify NaNs and functions like np.nanmean() and np.nanfill() to perform operations while ignoring or filling missing values.
Q.11 How do you stack multiple NumPy arrays vertically or horizontally?
Use np.vstack() for vertical stacking and np.hstack() for horizontal stacking.
Q.12 What is the purpose of the np.where() function?
np.where() returns indices of elements in an array where a condition is met or can be used to select elements from two arrays based on a condition.
Q.13 How can you generate random numbers using NumPy?
Use functions from numpy.random, such as np.random.rand(), np.random.randn(), and np.random.randint() to generate random numbers and arrays.
Q.14 How do you compute the dot product of two NumPy arrays?
Use np.dot() or the @ operator: np.dot(arr1, arr2) or arr1 @ arr2.
Q.15 What is the purpose of the np.linalg module?
np.linalg provides linear algebra functions, including matrix operations such as inversion, eigenvalue decomposition, and solving linear systems.
Q.16 How can you apply a function to each element of a NumPy array?
Use NumPy's vectorized functions or np.vectorize() to apply a function element-wise. For example, np.sqrt(arr) applies the square root function to each element.
Q.17 How do you calculate the correlation coefficient matrix for a NumPy array?
Use np.corrcoef(arr) to compute the correlation coefficient matrix of the array.
Q.18 What is the purpose of the np.flatten() method?
np.flatten() is used to convert a multi-dimensional array into a one-dimensional array.
Q.19 How can you remove duplicate elements from a NumPy array?
Use np.unique(arr) to return a sorted array of unique elements from the input array.
Q.20 How do you sort a NumPy array?
Use np.sort(arr) to return a sorted copy of the array or arr.sort() to sort the array in place.
Q.21 What is the difference between np.copy() and np.deepcopy()?
np.copy() creates a shallow copy of an array, while np.deepcopy() (not a NumPy function but a Python standard library function) creates a deep copy of an object, which is unnecessary for NumPy arrays since they are already deeply copied.
Q.22 How do you perform element-wise logical operations on NumPy arrays?
Use logical operators such as &, |, ~ for and, or, and not operations, or functions like np.logical_and(), np.logical_or(), and np.logical_not().
Q.23 How do you create an identity matrix in NumPy?
Use np.eye(n) to create an identity matrix of size n x n.
Q.24 What is the difference between np.arange() and np.linspace()?
np.arange() creates arrays with evenly spaced values within a given interval, while np.linspace() generates a specified number of evenly spaced values between a start and stop value.
Q.25 How do you compute the eigenvalues and eigenvectors of a matrix in NumPy?
Use np.linalg.eig() to compute the eigenvalues and eigenvectors of a matrix.
Q.26 How do you handle arrays with different data types in NumPy?
NumPy arrays can hold only one data type at a time. To handle different types, use a structured array or object data type.
Q.27 What is the np.percentile() function used for?
np.percentile() calculates the percentile of a given array, allowing you to determine the value below which a given percentage of data falls.
Q.28 How do you use the np.apply_along_axis() function?
np.apply_along_axis() applies a function to 1-D slices along the specified axis of a multidimensional array.
Q.29 What are NumPy's "view" and "copy" operations?
A "view" is a new array object that looks at the same data as the original array, while a "copy" is a new array object with its own data, created by np.copy().
Q.30 How do you perform element-wise multiplication of two NumPy arrays?
Use the * operator for element-wise multiplication: arr1 * arr2.
Q.31 What is the np.meshgrid() function used for?
np.meshgrid() creates coordinate matrices from coordinate vectors, useful for evaluating functions over a grid.
Q.32 How do you calculate the mean along a specific axis of a NumPy array?
Use np.mean(arr, axis=axis_number) to compute the mean along the specified axis.
Q.33 What is the np.extract() function used for?
np.extract() returns the elements of an array that satisfy a condition.
Q.34 How do you normalize data in a NumPy array?
Normalize data by subtracting the mean and dividing by the standard deviation: (arr - np.mean(arr)) / np.std(arr).
Q.35 How can you check the version of NumPy installed in your environment?
Use np.__version__ to check the version of NumPy.
Q.36 What is the difference between np.dot() and np.matmul()?
np.dot() performs matrix multiplication or dot product depending on input arrays, while np.matmul() performs matrix multiplication specifically and is preferred for higher-dimensional arrays.
Q.37 How do you handle arrays with differing shapes for broadcasting?
Ensure that dimensions are compatible for broadcasting by checking if dimensions are equal or one of them is 1, which allows NumPy to broadcast the smaller array to match the shape of the larger one.
Q.38 What is the np.diff() function used for?
np.diff() computes the n-th discrete difference along a specified axis, useful for finding differences between consecutive elements.
Q.39 How do you generate a NumPy array of random integers?
Use np.random.randint(low, high, size) to generate an array of random integers between low and high, with a specified size.
Q.40 What is the np.select() function used for?
np.select() allows you to choose values from a list of options based on a list of conditions, useful for applying multiple conditional logic to arrays.
Q.41 How can you compute the covariance matrix of a NumPy array?
Use np.cov(arr) to compute the covariance matrix of the input array. If the array has multiple dimensions, specify the row or column axis as needed.
Q.42 What is the np.gradient() function used for?
np.gradient() computes the gradient of an array, providing estimates of the derivatives along each axis.
Q.43 How do you find the indices of the maximum and minimum values in a NumPy array?
Use np.argmax() and np.argmin() to find the indices of the maximum and minimum values, respectively.
Q.44 What is the np.histogram() function used for?
np.histogram() computes the histogram of an array, which is useful for understanding the distribution of data.
Q.45 How do you compute the standard deviation of a NumPy array?
Use np.std(arr) to compute the standard deviation of the input array.
Q.46 What are “structured arrays” in NumPy?
Structured arrays are arrays with heterogeneous data types in each element, similar to a database table or data frame.
Q.47 How do you perform matrix inversion in NumPy?
Use np.linalg.inv(matrix) to compute the inverse of a square matrix, provided it is invertible.
Q.48 What is the purpose of the np.percentile() function, and how is it different from np.quantile()?
Both functions compute percentiles and quantiles, respectively, but np.percentile() takes percentages (0-100) while np.quantile() uses fractions (0-1).
Q.49 How can you calculate the cumulative sum of elements in a NumPy array?
Use np.cumsum(arr) to compute the cumulative sum of the array elements.
Q.50 What is the np.unique() function used for?
np.unique() returns the sorted unique elements of an array and can also return the indices of unique elements and their counts.
Q.51 How do you perform an element-wise comparison between two NumPy arrays?
Use comparison operators such as ==, !=, >, <, etc., to perform element-wise comparisons.
Q.52 What is the np.concatenate() function used for?
np.concatenate() joins two or more arrays along an existing axis, specified by the axis parameter.
Q.53 How can you fill missing values in a NumPy array?
Use functions like np.nan_to_num() to replace NaNs with specified values or fill in missing values using custom logic.
Q.54 What are NumPy’s “strides,” and how do they affect array operations?
Strides are the number of bytes to step in each dimension when traversing an array. They affect how arrays are stored in memory and can impact performance.
Q.55 How do you use NumPy’s “ufunc” (universal functions)?
Ufuncs perform element-wise operations on NumPy arrays, e.g., np.add(), np.sin(), np.exp(). They are highly optimized for performance.
Q.56 What is the np.tril() function used for?
np.tril() returns the lower triangular part of an array, with zeros in the upper part.
Q.57 How do you perform polynomial fitting in NumPy?
Use np.polyfit(x, y, degree) to fit a polynomial of the specified degree to the data points (x, y).
Q.58 What is the np.pad() function used for?
np.pad() adds padding to an array along specified dimensions, which is useful for various operations, such as convolution.
Q.59 How can you compute the determinant of a matrix using NumPy?
Use np.linalg.det(matrix) to compute the determinant of a square matrix.
Q.60 How do you extract sub-arrays from a NumPy array using slicing?
Use slicing notation, e.g., arr[start:stop:step] to extract sub-arrays from a NumPy array, where start, stop, and step define the slice parameters.
Get Govt. Certified Take Test
 For Support