0
0
NumPydata~5 mins

2D array indexing (row, col) in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the expression array[2, 3] mean in a 2D NumPy array?
It means accessing the element at row 2 and column 3 of the 2D array. Rows and columns start counting from 0.
Click to reveal answer
beginner
How do you select the entire 3rd row from a 2D NumPy array named arr?
Use arr[2, :]. The colon : means all columns in row 2.
Click to reveal answer
beginner
How do you select the entire 2nd column from a 2D NumPy array named arr?
Use arr[:, 1]. The colon : means all rows in column 1.
Click to reveal answer
intermediate
What happens if you use negative indices like arr[-1, -2] in a 2D NumPy array?
Negative indices count from the end. -1 is the last row, -2 is the second last column.
Click to reveal answer
intermediate
How can you select a submatrix from rows 1 to 3 and columns 2 to 4 in a 2D NumPy array arr?
Use slicing: arr[1:4, 2:5]. The end index is exclusive, so it selects rows 1,2,3 and columns 2,3,4.
Click to reveal answer
In a 2D NumPy array, what does arr[0, 0] access?
AFirst column, last row element
BLast row, last column element
CFirst row, first column element
DLast column, first row element
How do you select all rows but only the 2nd column from a 2D array arr?
Aarr[2, :]
Barr[1, :]
Carr[:, 2]
Darr[:, 1]
What does arr[-1, :] select in a 2D NumPy array?
ALast row
BFirst row
CLast column
DFirst column
Which slice selects rows 0 to 2 and columns 1 to 3 in arr?
Aarr[0:3, 1:4]
Barr[1:3, 0:4]
Carr[0:2, 1:3]
Darr[1:4, 0:3]
What is the shape of arr[:, 2] if arr is 5x5?
A(5, 5)
B(5,)
C(1, 5)
D(5, 1)
Explain how to access a single element, a full row, and a full column in a 2D NumPy array.
Think about how rows and columns are indexed and how slicing works.
You got /3 concepts.
    Describe how negative indices work in 2D array indexing and give an example.
    Negative numbers start counting backwards from the last element.
    You got /3 concepts.