0
0
NumPydata~5 mins

Slicing rows and columns in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Aarr[1:3, 2:4]
Barr[1:4, 2:5]
Carr[2:5, 1:4]
Darr[2:4, 1:3]
What does arr[:, 0] return?
ALast row, first column
BFirst row, all columns
CFirst row, first column
DAll rows, first column
How do you select every other row and every other column from arr?
Aarr[::2, ::2]
Barr[1::2, 1::2]
Carr[::1, ::1]
Darr[2::2, 2::2]
What is the shape of the result of arr[1:3, 2:5] if arr has shape (5, 6)?
A(2, 3)
B(2, 4)
C(3, 4)
D(3, 3)
Which slice selects the last two rows and all columns from arr?
Aarr[:, -2:]
Barr[-2:, :]
Carr[:2, :]
Darr[-1:, :]
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.