Data Science with Python Certification Data Science with Python NumPy Array Manipulation 4 — Questions and Answers
Question 1: What is the purpose of `np.einsum('ij,jk->ik', A, B)`?
- Element-wise multiplication of A and B
- Matrix multiplication of A and B using Einstein summation notation (Correct answer)
- Transposing A and multiplying by B
- Computing the outer product of A and B
Correct answer: Matrix multiplication of A and B using Einstein summation notation
The subscript `'ij,jk->ik'` contracts over the shared index j, performing standard matrix multiplication.
Question 2: What does `np.argmax(a, axis=0)` return for a 2-D array?
- The maximum value along each column
- The row index of the maximum value in each column (Correct answer)
- The column index of the overall maximum
- A boolean mask marking maximum positions
Correct answer: The row index of the maximum value in each column
`argmax` with `axis=0` reduces along rows, returning the row index where the maximum occurs for each column.
Question 3: Which NumPy function performs element-wise comparison and returns the element-wise minimum?
- np.minimum(a, b) (Correct answer)
- np.min(a, b)
- np.fmin(a, b)
- np.amin(a, b)
Correct answer: np.minimum(a, b)
`np.minimum(a, b)` compares two arrays element-wise and returns a new array with the smaller value at each position.
Question 4: What is a structured array in NumPy?
- An array sorted in ascending order
- An array with a dtype composed of named fields, like a table of records (Correct answer)
- A multi-dimensional array with uniform dtypes
- An array backed by a C struct in memory
Correct answer: An array with a dtype composed of named fields, like a table of records
Structured arrays have a compound dtype with named fields, enabling each element to hold heterogeneous data types like a database row.
Question 5: How do you make a deep copy of a NumPy array so that modifications don't affect the original?
- b = a
- b = a.view()
- b = a.copy() (Correct answer)
- b = np.asarray(a)
Correct answer: b = a.copy()
`a.copy()` creates an independent array with its own data buffer; changes to b will not propagate to a.
Question 6: What does the `order` parameter in `array.reshape(shape, order='F')` control?
- Whether the reshape is done in-place
- The memory layout (Fortran-style column-major vs C-style row-major) used when reading/writing elements (Correct answer)
- The data type conversion order
- The axis along which reshaping occurs
Correct answer: The memory layout (Fortran-style column-major vs C-style row-major) used when reading/writing elements
`order='F'` reads and writes elements in Fortran (column-major) order, while `order='C'` (default) uses row-major order.
Question 7: What does `np.unique(a, return_counts=True)` return?
- Only the unique values sorted
- A tuple of (unique sorted values, count of each unique value) (Correct answer)
- A boolean mask marking first occurrences
- The unique values and their first-occurrence indices
Correct answer: A tuple of (unique sorted values, count of each unique value)
With `return_counts=True`, `np.unique` returns a tuple where the second element is the frequency of each unique value.
What is the purpose of `np.einsum('ij,jk->ik', A, B)`?