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

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-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

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.4. Substituting items

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-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.5. 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.6. Assignments

Code 2.33. Solution
"""
* Assignment: Numpy Indexing
* Complexity: easy
* Lines of code: 5 lines
* Time: 5 min

English:
    1. Create `result: np.ndarray`
    2. 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
    3. `result` size must be 2x2
    4. `result` type must be float
    5. Run doctests - all must succeed

Polish:
    1. Stwórz `result: np.ndarray`
    2. 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
    3. Rozmiar `result` musi być 2x2
    4. Typ `result` musi być float
    5. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * `np.zeros(shape, dtype)`

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> type(result) is np.ndarray
    True
    >>> result
    array([[3., 9.],
           [1., 4.]])
"""

import numpy as np


DATA = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])

result = ...