2.5. Array Getitem¶
2.5.1. Rationale¶
int
list[int]
list[bool]
import numpy as np
a = np.array([[1, 2, 3],
[4, 5, 6]])
a[ 0 ] # int
a[ [0,1] ] # list[int]
a[ [True,False] ] # list[bool]
2.5.2. Index¶
import numpy as np
a = np.array([1, 2, 3])
a.flat[0]
# 1
a.flat[1]
# 2
a.flat[2]
# 3
a.flat[4]
# Traceback (most recent call last):
# IndexError: index 4 is out of bounds for axis 0 with size 3
Flat:
import numpy as np
a = np.array([[1, 2, 3],
[4, 5, 6]])
a.flat[0]
# 1
a.flat[1]
# 2
a.flat[2]
# 3
a.flat[3]
# 4
a.flat[4]
# 5
a.flat[5]
# 6
Multidimensional:
import numpy as np
a = np.array([[1, 2, 3],
[4, 5, 6]])
a[0][0]
# 1
a[0][1]
# 2
a[0][2]
# 3
a[1][0]
# 4
a[1][1]
# 5
a[1][2]
# 6
a[2]
# Traceback (most recent call last):
# IndexError: index 2 is out of bounds for axis 0 with size 2
a[-1][-1]
# 6
a[-3]
# Traceback (most recent call last):
# IndexError: index -3 is out of bounds for axis 0 with size 2
a[0,0]
# 1
a[0,1]
# 2
a[0,2]
# 3
a[1,0]
# 4
a[1,1]
# 5
a[1,2]
# 6
2.5.3. Selecting items¶
2.5.4. 1-dimensional Array¶
import numpy as np
a = np.array([1, 2, 3])
# array([1, 2, 3])
a[0]
# 1
a[1]
# 2
a[2]
# 3
a[3]
# Traceback (most recent call last):
# IndexError: index 3 is out of bounds for axis 0 with size 3
a[-1]
# 3
2.5.5. 2-dimensional Array¶
import numpy as np
a = np.array([[1, 2, 3],
[4, 5, 6]])
a[0]
# array([1, 2, 3])
a[1]
# array([4, 5, 6])
a[2]
# Traceback (most recent call last):
# IndexError: index 2 is out of bounds for axis 0 with size 2
a[0,0]
# 1
a[0,1]
# 2
a[0,2]
# 3
a[1,0]
# 4
a[1,1]
# 5
a[1,2]
# 6
a[2,0]
# Traceback (most recent call last):
# IndexError: index 2 is out of bounds for axis 0 with size 2
import numpy as np
a = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
a[0]
# array([1, 2, 3])
a[1]
# array([4, 5, 6])
a[2]
# Traceback (most recent call last):
# IndexError: index 2 is out of bounds for axis 0 with size 2
a[0,0]
# 1
a[0,1]
# 2
a[0,2]
# 3
a[1,0]
# 4
a[1,1]
# 5
a[1,2]
# 6
a[2,0]
# 7
a[2,1]
# 8
a[2,2]
# 9
2.5.6. 3-dimensional Array¶
import numpy as np
a = np.array([[[ 1, 2, 3],
[ 4, 5, 6],
[ 5, 6, 7]],
[[11, 22, 33],
[44, 55, 66],
[77, 88, 99]]])
a[0,0,0]
# 1
a[0,0,1]
# 2
a[0,0,2]
# 3
a[0,0,3]
# Traceback (most recent call last):
# IndexError: index 3 is out of bounds for axis 2 with size 3
a[0,1,2]
# 6
a[0,2,1]
# 6
a[2,1,0]
# Traceback (most recent call last):
# IndexError: index 2 is out of bounds for axis 0 with size 2
2.5.7. Substituting items¶
2.5.8. 1-dimensional Array¶
Will type cast values to
np.ndarray.dtype
import numpy as np
a = np.array([1, 2, 3])
a[0] = 99
# array([99, 2, 3])
a[-1] = 11
# array([99, 2, 11])
import numpy as np
a = np.array([1, 2, 3], float)
a[0] = 99.9
# array([99.9, 2., 3.])
a[-1] = 11.1
# array([99.9, 2., 11.1])
import numpy as np
a = np.array([1, 2, 3], int)
a[0] = 99.9
# array([99, 2, 3])
a[-1] = 11.1
# array([99, 2, 11])
2.5.9. 2-dimensional Array¶
import numpy as np
a = np.array([[1, 2, 3],
[4, 5, 6]])
a[0,0] = 99
# array([[99, 2, 3],
# [ 4, 5, 6]])
a[1,2] = 11
# array([[99, 2, 3],
# [ 4, 5, 11]])
2.5.10. Multi-indexing¶
import numpy as np
a = np.array([1, 2, 3])
a[0], a[2], a[-1]
# (1, 3, 3)
a[[0,2,-1]]
# array([1, 3, 3])
a[[True, False, True]]
# array([1, 3])
import numpy as np
a = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
a[[0,1]]
# array([[1, 2, 3],
# [4, 5, 6]])
a[[0,2,-1]]
# array([[1, 2, 3],
# [7, 8, 9],
# [7, 8, 9]])
a[[True, False, True]]
# array([[1, 2, 3],
# [7, 8, 9]])
2.5.11. Assignments¶
"""
* Assignment: Numpy Indexing
* Complexity: easy
* Lines of code: 5 lines
* Time: 5 min
English:
1. Use data from "Given" section (see below)
2. Create `result: np.ndarray`
3. Add to `result` elements from `DATA` at indexes:
a. row 0, column 2
b. row 2, column 2
c. row 0, column 0
d. row 1, column 0
4. `result` size must be 2x2
5. `result` type must be float
6. Compare result with "Tests" section (see below)
Polish:
1. Użyj danych z sekcji "Given" (patrz poniżej)
2. Stwórz `result: np.ndarray`
3. Dodaj do `result` elementy z `DATA` o indeksach:
a. wiersz 0, kolumna 2
b. wiersz 2, kolumna 2
c. wiersz 0, kolumna 0
d. wiersz 1, kolumna 0
4. Rozmiar `result` musi być 2x2
5. Typ `result` musi być float
6. Porównaj wyniki z sekcją "Tests" (patrz poniżej)
Hints:
* `np.zeros(shape, dtype)`
Tests:
>>> type(result) is np.ndarray
True
>>> result
array([[3., 9.],
[1., 4.]])
"""
# Given
import numpy as np
DATA = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
result = ...