Test your basic knowledge of NumPy with these easy interview questions. This quiz covers core NumPy concepts such as arrays, broadcasting, data types, numerical computation, memory optimization, and handling missing values.
What is the primary purpose of NumPy in scientific computing with Python?
Explanation: NumPy is designed for fast numerical computations and array processing in Python, making it highly suitable for scientific computing. While NumPy does not create visualizations, manage databases, or work with web pages directly, its core strength lies in array manipulation and computation. Options mentioning HTML or databases are unrelated to NumPy's functionality. Visualization is handled by other libraries.
Which advantage do NumPy arrays have over regular Python lists when performing mathematical operations?
Explanation: NumPy arrays are optimized for numerical computation, making them both faster and more memory-efficient for math operations compared to Python lists. Regular Python lists do not support vectorized operations or automatic broadcasting. NumPy arrays are homogeneous, so they do not hold mixed types as easily as lists. Memory usage is typically more optimal in NumPy arrays than in lists.
Why is selecting the appropriate data type (dtype) important when creating NumPy arrays?
Explanation: Choosing the correct dtype can save memory and improve performance, especially with large datasets. NumPy supports many data types, not just integers. The dtype has no connection to HTML tags, and disregarding dtype in scientific computing can lead to unnecessary resource consumption or loss of precision.
What does broadcasting allow you to do in NumPy arrays?
Explanation: Broadcasting is a core concept in NumPy, enabling operations on arrays with different shapes without explicit replication of data. It does not relate to web displays, encryption, or audio file saving. Those options are unrelated to NumPy's computational focus.
Which NumPy function is used to handle missing values by masking specific elements in an array?
Explanation: The np.ma.masked_array() method creates masked arrays, allowing missing values to be excluded from computations. The other choices are not actual NumPy functions; np.array_missing(), np.lossy_array(), and np.null_mask() do not exist. Only np.ma.masked_array() serves this specific purpose.
What does standardization do to the features in a NumPy array for machine learning?
Explanation: Standardization adjusts features to have a mean of zero and a standard deviation of one, making them comparable for machine learning. Converting to hexadecimal or sorting are different operations, and standardization does not fill missing values with zeros. This helps ensure balanced feature contributions to ML models.
Which function would you use to join two NumPy arrays along an existing axis?
Explanation: np.concatenate() is the correct NumPy function for joining multiple arrays along a specific axis. np.connect(), np.append_array(), and np.merge() are not valid NumPy functions for this task. Using concatenate keeps array shapes consistent and efficient.
What does the NumPy function np.linalg.inv() compute when given a square matrix?
Explanation: np.linalg.inv() returns the inverse of a square matrix if it exists, which is important for solving linear equations. Transposing (option 3) or finding maxima (option 2) are different operations performed with other functions. Counting nonzero elements is unrelated to matrix inversion.
Why is np.float64 generally preferred over np.float32 in NumPy for numerical stability?
Explanation: The np.float64 data type has higher precision than np.float32, making it less prone to rounding errors in calculations. It does require more memory, not less. np.float32 is supported in NumPy, and neither data type influences the automatic sorting of elements.
Which tool lets you apply a custom function element-wise to each value in a NumPy array?
Explanation: np.vectorize() wraps a Python function so it can be applied efficiently to each element of a NumPy array. np.parallelize(), np.optimize(), and np.gridder() are not valid NumPy functions for element-wise application. This method helps speed up custom computations without explicit loops.