Explore the essentials of working with NumPy for arrays, pandas for data manipulation, and the basics behind Python's popular data science tools. Perfect for those starting their journey in data analysis and visualization.
What is the main purpose of the NumPy library in Python?
Explanation: NumPy is designed for scientific computing and specializes in efficiently handling large, multi-dimensional arrays and matrices along with mathematical functions. Visualization and animation are best handled by libraries like Matplotlib or Plotly. Managing databases is not NumPy's purpose—other packages cover that. Web scraping and text mining are unrelated to what NumPy offers.
How can you round each number in a NumPy array to two decimal places?
Explanation: np.round(array, 2) is the correct function for rounding all values in the array to two decimal places. array.round(0.2) is incorrect syntax. numpy.truncate does not specify the number of decimals; it behaves differently. array.set_precision(2) is not a NumPy function.
In the import statement 'import numpy as np', what does 'as np' accomplish?
Explanation: 'as np' creates a shorthand alias, making function calls like np.array() quicker and more readable. It does not rename variables or make NumPy faster, nor does it filter only numerical functions—NumPy always loads all its capabilities.
Which code checks if each element in a NumPy array is even and returns a boolean array?
Explanation: Using arr % 2 == 0 evaluates each element for evenness and returns a boolean array. np.is_even(arr) does not exist in NumPy. arr // 2 == 0 checks if the integer division result is zero, which is not the same. arr == 2 only checks if values are exactly two.
What is the correct way to initialize a NumPy random number generator for reproducible results?
Explanation: np.random.default_rng(seed=42) initializes the modern random generator with a reproducible seed. np.random.Random(42), np.seed_random, and np.generate_random are not valid NumPy functions or methods for this purpose.