4.9. Array Getitem

4.9.1. Index

Listing 4.144. 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
Listing 4.145. 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]         # IndexError: index 2 is out of bounds for axis 0 with size 2

a[-1][-1]    # 6
a[-3]        # 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

4.9.2. Selecting items

4.9.2.1. 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]        # IndexError: index 3 is out of bounds for axis 0 with size 3
a[-1]       # 3

4.9.2.2. 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]        # 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]      # 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]        # 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

4.9.2.3. 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]    # 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]    # IndexError: index 2 is out of bounds for axis 0 with size 2

4.9.3. Substituting items

4.9.3.1. 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] = 88
# array([99,  2,  88])
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])

4.9.3.2. 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] = 88
# array([[99,  2,  3],
#        [ 4,  5, 88]])

4.9.4. 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]])

4.9.5. Assignments

4.9.5.1. Numpy Indexing

English
  1. Use data from "Input" section (see below)

  2. Create result: np.ndarray

  3. Add to result elements from DATA at indexes:

    • row 0, column 2

    • row 2, column 2

    • row 0, column 0

    • row 1, column 0

  4. result size must be 2x2

  5. result type must be float

  6. Compare result with "Output" section (see below)

Polish
  1. Użyj danych z sekcji "Input" (patrz poniżej)

  2. Stwórz result: np.ndarray

  3. Dodaj do result elementy z DATA o indeksach:

    • wiersz 0, kolumna 2

    • wiersz 2, kolumna 2

    • wiersz 0, kolumna 0

    • wiersz 1, kolumna 0

  4. Rozmiar result musi być 2x2

  5. Typ result musi być float

  6. Porównaj wyniki z sekcją "Output" (patrz poniżej)

Input
DATA = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])
Output
result: np.ndarray
# array([[3., 9.],
#        [1., 4.]])
The whys and wherefores
  • Defining np.ndarray

  • Indexing np.ndarray

Hint
  • np.zeros(shape, dtype)