Recall & Review
beginner
What does slicing rows and columns mean in a numpy array?
It means selecting specific parts of the array by choosing certain rows and columns using index ranges or lists.
Click to reveal answer
beginner
How do you select the first 3 rows and first 2 columns of a numpy array named
arr?Use
arr[:3, :2]. The first part :3 selects rows 0, 1, 2 and :2 selects columns 0 and 1.Click to reveal answer
intermediate
What does
arr[1:4, 2] select from a numpy array?It selects rows 1, 2, and 3 from column 2, returning a 1D array of those values.
Click to reveal answer
beginner
How can you select all rows but only the last column of a numpy array
arr?Use
arr[:, -1]. The colon means all rows, and -1 means the last column.Click to reveal answer
intermediate
What happens if you use
arr[::2, ::3] on a numpy array?It selects every 2nd row and every 3rd column, starting from the first row and column.
Click to reveal answer
Which numpy slice selects rows 2 to 4 and columns 1 to 3 from array
arr?✗ Incorrect
Slicing in numpy includes the start index but excludes the end index. So rows 2 to 4 means 2:5 and columns 1 to 3 means 1:4.
What does
arr[:, 0] return?✗ Incorrect
The colon means all rows, and 0 means the first column.
How do you select every other row and every other column from
arr?✗ Incorrect
The step value 2 in slicing means every other element starting from index 0.
What is the shape of the result of
arr[1:3, 2:5] if arr has shape (5, 6)?✗ Incorrect
Rows 1 to 2 (1:3) gives 2 rows, columns 2 to 4 (2:5) gives 3 columns, so shape is (2, 3).
Which slice selects the last two rows and all columns from
arr?✗ Incorrect
Negative indices count from the end.
-2: means last two rows, and colon means all columns.Explain how to slice a numpy array to select specific rows and columns.
Think about how you pick parts of a table by row and column numbers.
You got /4 concepts.
Describe what
arr[::2, 1:5:2] does on a numpy array.Focus on the meaning of the double colons and step values.
You got /4 concepts.