NumPy Tutorial 3
Concept: Targeting Data (Indexing)
In Python, to get a number from a matrix, you use two sets of brackets [row][col]. In NumPy, we use a cleaner, comma-separated syntax.
Python List
val = list[0][1]
Two steps (slow)
val = list[0][1]
Two steps (slow)
NumPy Array
val = arr[0, 1]
One step (fast)
val = arr[0, 1]
One step (fast)
Activity: Target the Number
We want to extract the number 60 from this matrix. Drag the correct Row Index and Column Index into the code below.
Remember: Computer Science counting starts at 0.
Columns: 0 1 2
Row 0
Row 1
10
20
30
40
50
60
Code Builder:
number = arr[
Row
,
Col
]
Draggable Indices:
0
1
2
Knowledge Check
Which syntax is valid for selecting the first element (top-left) in a NumPy matrix?
A. array(0, 0)
B. array[0, 0]
C. array[1, 1]
0 Comments