Data Science with Python NumPy Array Manipulation Questions and Answers — Questions and Answers
Question 1: A data scientist has a 2D NumPy array representing daily sales data for 4 products over 6 days, with a shape of (6, 4). They need to transform this array so that each row represents a product and each column represents a day. Which of the following NumPy operations will correctly perform this transformation?
- arr.reshape(4, 6)
- np.transpose(arr) (Correct answer)
- np.ravel(arr)
- np.vsplit(arr, 2)
Correct answer: np.transpose(arr)
The `np.transpose()` function, or the `.T` attribute, permutes the dimensions of an array. For a 2D array, this is equivalent to swapping the rows and columns. The original shape is (6, 4), and transposing it will result in a shape of (4, 6), which correctly aligns products with rows and days with columns.
Question 2: You are given two 1D NumPy arrays, `a1 = np.array([1, 2, 3])` and `a2 = np.array([4, 5, 6])`. Which function would you use to combine them into a single 2D array where `a1` is the first row and `a2` is the second row?
- np.hstack((a1, a2))
- np.concatenate((a1, a2), axis=1)
- np.vstack((a1, a2)) (Correct answer)
- np.column_stack((a1, a2))
Correct answer: np.vstack((a1, a2))
`np.vstack()` stacks arrays in sequence vertically (row-wise). It takes a tuple of arrays as input and stacks them one on top of the other, creating a new dimension. `hstack` would append them horizontally, and `concatenate` with `axis=1` would raise an error for 1D arrays.
Question 3: What is the key difference between NumPy's `ravel()` and `flatten()` methods when used to convert a multi-dimensional array into a 1D array?
- `ravel()` always returns a copy, while `flatten()` returns a view.
- `flatten()` can only be used on 2D arrays, while `ravel()` works on any dimension.
- `ravel()` returns a view of the original array whenever possible, while `flatten()` always returns a new copy. (Correct answer)
- There is no functional difference; they are aliases for the same operation.
Correct answer: `ravel()` returns a view of the original array whenever possible, while `flatten()` always returns a new copy.
The fundamental difference is that `flatten()` always allocates new memory and returns a copy of the data. In contrast, `ravel()` is more memory-efficient as it returns a view of the original array if possible, meaning modifications to the raveled array can affect the original array.
Question 4: A developer needs to split a NumPy array of shape (8, 10) into 4 equal sub-arrays along its rows. However, if the array cannot be split equally, the program should still proceed without raising an error. Which function is most suitable for this task?
- np.hsplit()
- np.split()
- np.array_split() (Correct answer)
- np.vsplit()
Correct answer: np.array_split()
`np.array_split()` is designed to split an array into multiple sub-arrays of near-equal size. Unlike `np.split()`, it will not raise an error if the split is not even. In this case, since the split is along the rows, `np.array_split(arr, 4, axis=0)` would be the correct usage. `vsplit` would work, but `array_split` is more general and handles the 'unequal' requirement explicitly.
Question 5: Given the code snippet below, what will be the value of the original array `arr` after the final line is executed? ```python import numpy as np arr = np.arange(10).reshape(2, 5) arr.resize(2, 6) ```
- An error will be thrown because the new size is different.
- A new array will be returned, and `arr` will remain unchanged.
- The `arr` array will be modified in-place to shape (2, 6), with the new elements being zeros.
- The `arr` array will be modified in-place to shape (2, 6), with the new elements being repeated values from the original array. (Correct answer)
Correct answer: The `arr` array will be modified in-place to shape (2, 6), with the new elements being repeated values from the original array.
The `ndarray.resize()` method modifies the array in-place. When the new size is larger than the original, the new elements are filled by repeating the existing elements of the array. `np.reshape()`, on the other hand, would require the new shape to have the same total number of elements.
Question 6: Which of the following statements correctly describes the behavior of `np.hstack()`?
- It joins a sequence of arrays along a new axis.
- It splits an array into multiple sub-arrays horizontally.
- It stacks arrays in sequence vertically (row-wise).
- It stacks arrays in sequence horizontally (column-wise). (Correct answer)
Correct answer: It stacks arrays in sequence horizontally (column-wise).
`np.hstack()` stands for horizontal stack. It takes a sequence of arrays and joins them along the second axis (axis=1), effectively placing them side-by-side. The input arrays must have the same shape along all axes except for the second one.
A data scientist has a 2D NumPy array representing daily sales data for 4 products over 6 days, with a shape of (6, 4).
They need to transform this array so that each row represents a product and each column represents a day.
Which of the following NumPy operations will correctly perform this transformation?