Data Science with Python NumPy Array Manipulation Flashcards
6 cards from real Data Science with Python Certification practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 6 Data Science with Python NumPy Array Manipulation flashcards as text
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?
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.
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?
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.
What is the key difference between NumPy's `ravel()` and `flatten()` methods when used to convert a multi-dimensional array into a 1D array?
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.
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?
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.
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) ```
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.
Which of the following statements correctly describes the behavior of `np.hstack()`?
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.
How this page was made: written with AI tools and published by Nathan Beja, founder of Practice Test Geeks, who is responsible for its content. We have not yet matched this exam's facts to an official source, so confirm fees, dates and rules with the exam body. Not reviewed by a subject-matter expert. Editorial policy