Data Science with Python Certification Data Science with Python NumPy Array Manipulation 5 — Questions and Answers
Question 1: What does `np.lib.stride_tricks.as_strided` allow you to do?
- Sort an array using stride-based quicksort
- Create a view of an array with custom shape and strides, enabling advanced windowing operations (Correct answer)
- Compress array storage using stride encoding
- Automatically broadcast two arrays to a common stride
Correct answer: Create a view of an array with custom shape and strides, enabling advanced windowing operations
`as_strided` gives direct control over strides and shape, making it possible to create sliding-window views without copying data.
Question 2: Which NumPy function can be used to split an array into multiple sub-arrays along an axis?
- np.divide
- np.partition
- np.array_split (Correct answer)
- np.separate
Correct answer: np.array_split
`np.array_split` splits an array into N roughly equal sub-arrays, allowing unequal splits unlike `np.split`.
Question 3: What is the difference between `np.dot(A, B)` and `A @ B` for 2-D arrays?
- @ computes element-wise product; dot computes matrix product
- They are equivalent for 2-D arrays (both perform matrix multiplication) (Correct answer)
- dot only works on 1-D arrays
- @ is slower because it always copies data
Correct answer: They are equivalent for 2-D arrays (both perform matrix multiplication)
For 2-D arrays, `np.dot(A, B)` and `A @ B` (matmul operator) both perform matrix multiplication and give identical results.
Question 4: What does `np.pad(a, pad_width=1, mode='constant', constant_values=0)` do to a 2-D array?
- Adds a border of zeros one element wide around the array (Correct answer)
- Repeats edge values once on each side
- Clips values outside the range [0, 1]
- Normalizes each row to sum to 1
Correct answer: Adds a border of zeros one element wide around the array
`np.pad` with `mode='constant'` and `constant_values=0` adds one row/column of zeros to every edge of the 2-D array.
Question 5: What is the result of `np.nonzero(np.array([0, 3, 0, 7, 0]))`?
- array([3, 7])
- (array([1, 3]),) (Correct answer)
- array([False, True, False, True, False])
- array([1, 3])
Correct answer: (array([1, 3]),)
`np.nonzero` returns a tuple of arrays (one per dimension) containing the indices of non-zero elements; for 1-D input it's a tuple with one array.
Question 6: How does boolean indexing behave when the boolean array has fewer dimensions than the target array?
- It raises an IndexError
- It selects along the first axes, keeping remaining axes intact (Correct answer)
- It broadcasts to match the target shape
- It flattens the remaining dimensions automatically
Correct answer: It selects along the first axes, keeping remaining axes intact
A boolean index array selects along the leading dimensions it covers, leaving any trailing axes in the result.
Question 7: What does `np.searchsorted(sorted_arr, values, side='right')` return?
- The index of each value if it exists, otherwise -1
- Insertion indices so that inserting values keeps sorted_arr sorted, using right-side insertion (Correct answer)
- The nearest element to each value
- The count of elements less than each value
Correct answer: Insertion indices so that inserting values keeps sorted_arr sorted, using right-side insertion
`searchsorted` with `side='right'` returns the rightmost index at which each value can be inserted to maintain sorted order.
What does `np.lib.stride_tricks.as_strided` allow you to do?