Skip to content

Latest commit

 

History

History
152 lines (101 loc) · 9.26 KB

numpy interview questions.md

File metadata and controls

152 lines (101 loc) · 9.26 KB

Important NumPy Interview Questions

1. What is NumPy and why is it used in Python?

NumPy is a fundamental package for scientific computing in Python. It provides support for arrays, matrices, and many mathematical functions to operate on these data structures efficiently.

2. How do you create a NumPy array and what are the common ways to initialize it?

Use np.array(), np.zeros(), np.ones(), np.empty(), and np.arange() to create arrays with specific values or shapes.

3. What is broadcasting in NumPy and how does it work?

Broadcasting is a technique that allows NumPy to perform operations on arrays of different shapes by automatically expanding the smaller array to match the larger one.

4. How do you perform element-wise operations with NumPy arrays?

Use operators like +, -, *, /, and functions like np.add(), np.subtract(), np.multiply(), and np.divide() for element-wise operations.

5. How can you index and slice NumPy arrays?

Use square brackets [] with indices and slices to access or modify elements of the array. For multi-dimensional arrays, use commas to separate dimensions.

6. How can you reshape a NumPy array?

Use the reshape() method to change the shape of an array without changing its data. Ensure the new shape is compatible with the original data size.

7. What are the different ways to stack arrays in NumPy?

Use np.vstack(), np.hstack(), and np.concatenate() to stack arrays vertically, horizontally, or along a specified axis.

8. How do you handle missing data in NumPy arrays?

Use np.nan to represent missing values and functions like np.isnan() to identify them. You can use np.nanmean(), np.nanstd(), etc., to perform operations while ignoring NaNs.

9. What is the purpose of np.dot() and how does it differ from np.matmul()?

np.dot() performs dot products of two arrays, while np.matmul() performs matrix multiplication. They differ in behavior with higher-dimensional arrays.

10. How do you compute the mean, median, and standard deviation of a NumPy array?

Use np.mean(), np.median(), and np.std() to compute these statistical measures.

11. How do you create an array with random values in NumPy?

Use functions from np.random, such as np.random.rand(), np.random.randn(), and np.random.randint() to generate arrays with random values.

12. How can you use np.apply_along_axis()?

np.apply_along_axis() applies a function along a specified axis of a NumPy array.

13. How do you find the unique elements and their counts in a NumPy array?

Use np.unique() with the return_counts=True argument to get unique elements and their counts.

14. What is the difference between np.copy() and np.view()?

np.copy() creates a full copy of the array, while np.view() creates a new view of the same data without copying it.

15. How do you perform matrix operations like inversion and eigenvalue decomposition in NumPy?

Use np.linalg.inv() for matrix inversion and np.linalg.eig() for eigenvalue decomposition.

16. How do you compute cumulative sums and products in NumPy?

Use np.cumsum() for cumulative sums and np.cumprod() for cumulative products.

17. What is the purpose of np.histogram() function?

np.histogram() computes the histogram of a dataset, which is a way to summarize the distribution of data.

18. How can you find the rank of a NumPy array?

The rank of a NumPy array is equivalent to the number of dimensions, which you can obtain using the ndim attribute.

19. How do you perform element-wise operations between arrays of different shapes?

NumPy uses broadcasting to perform operations between arrays of different shapes, automatically expanding the smaller array to match the larger one.

20. How do you handle large datasets that do not fit into memory using NumPy?

Use np.memmap() to create memory-mapped arrays that allow you to work with large datasets by accessing data on disk as if it were in memory.

21. How do you compute the correlation coefficient between two arrays?

Use np.corrcoef() to compute the correlation coefficient matrix between two or more arrays.

22. What is the purpose of np.meshgrid() function?

np.meshgrid() creates coordinate matrices from coordinate vectors, useful for evaluating functions on a grid.

23. How do you perform element-wise multiplication between two arrays of the same shape?

Use the * operator or np.multiply() function to perform element-wise multiplication.

24. How can you use np.extract()?

np.extract() extracts elements from an array that satisfy a given condition.

25. What is the significance of np.arange() and how does it differ from np.linspace()?

np.arange() generates arrays with a specified step size, while np.linspace() generates arrays with a specified number of equally spaced points.

26. How can you work with structured arrays in NumPy?

Structured arrays allow for arrays with fields of different data types. You can define the dtype with a list of tuples specifying field names and types.

27. How can you use np.load() and np.save() functions?

np.save() saves an array to a binary file in .npy format, while np.load() loads an array from a .npy file.

28. How do you handle arrays with different data types (mixed data types)?

Use dtype=object to create an array with mixed data types. For specific operations, ensure all elements are of compatible types or convert as needed.

29. What is the role of np.isnan() and how do you handle NaNs in arrays?

np.isnan() identifies NaN values in arrays. Handle NaNs by using functions like np.nanmean(), np.nanstd(), etc., which ignore NaNs during calculations.

30. How do you use NumPy to perform element-wise comparisons between arrays?

Use comparison operators like ==, !=, <, >, <=, >=, and functions like np.equal(), np.greater(), etc., for element-wise comparisons.

31. How can you use the np.sort() function?

np.sort() returns a sorted copy of an array, and you can use the axis argument to sort along a specified axis.

32. How do you calculate the sum along an axis of a NumPy array?

Use the np.sum() function with the axis argument to specify the axis along which to sum the elements.

33. How can you use np.concatenate() and np.vstack() for array concatenation?

np.concatenate() joins arrays along an existing axis, while np.vstack() stacks arrays vertically (row-wise).

34. What is the difference between np.mean() and np.average()?

np.mean() computes the arithmetic mean, while np.average() allows for weighted averages when weights are provided.

35. How do you perform logical operations on NumPy arrays?

Use logical operators like &, |, ~, and functions like np.logical_and(), np.logical_or(), etc., for element-wise logical operations.

36. How do you use np.tile() and np.repeat() to replicate arrays?

np.tile() repeats the array along specified axes, while np.repeat() replicates elements of the array.

37. How can you compute the dot product and cross product of two arrays?

Use np.dot() for dot product and np.cross() for cross product calculations.

38. How do you calculate the standard deviation and variance of a NumPy array?

Use np.std() for standard deviation and np.var() for variance.

39. What is the purpose of np.squeeze() and np.expand_dims()?

np.squeeze() removes single-dimensional entries from the shape of an array, while np.expand_dims() adds a new axis of length one to the array.

40. How do you use np.setdiff1d() to find the difference between two arrays?

np.setdiff1d() returns the unique values in one array that are not in another.

41. How can you calculate the Euclidean distance between two arrays?

Use np.linalg.norm() with the difference between the two arrays to calculate the Euclidean distance.

42. How do you use np.partition() to perform partial sorting?

np.partition() partially sorts an array, so that the k smallest elements are in the first k positions.

43. How can you use np.unique() to find unique elements in an array?

np.unique() returns the sorted unique elements of an array.

44. How do you use np.gradient() to compute gradients of a NumPy array?

np.gradient() calculates the gradient of an array, which is useful for numerical differentiation.

45. What are the uses of np.flatnonzero()?

np.flatnonzero() returns the indices of the non-zero elements in a flattened array.

46. How can you use np.copyto() to copy data from one array to another?

np.copyto() copies values from one array to another with optional broadcasting.

47. How do you use np.ma.masked_where() to mask values in an array?

np.ma.masked_where() masks elements of an array based on a condition, creating a masked array.

48. How do you use np.argwhere() to find indices of elements satisfying a condition?

np.argwhere() returns the indices of array elements that satisfy a specified condition.

49. How can you compute the covariance matrix of two arrays using NumPy?

Use np.cov() to compute the covariance matrix between two or more arrays.

50. How do you use np.tril() and np.triu() to extract lower and upper triangular parts of an array?

np.tril() extracts the lower triangular part, while np.triu() extracts the upper triangular part of an array.