4.10. Array Slicing

4.10.1. 1-dimensional Array

import numpy as np


a = np.array([1, 2, 3])

a[0:2]
# array([1, 2])

a[:2]
# array([1, 2])

a[1:3]
# array([2, 3])

a[-2:]
# array([2, 3])
import numpy as np


a = np.array([1, 2, 3])

a[::2]
# array([1, 3])

a[1::2]
# array([2])

4.10.2. 2-dimensional Array

4.10.2.1. All

import numpy as np


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

a[:]
# array([[1, 2, 3],
#        [4, 5, 6],
#        [7, 8, 9]])

4.10.2.2. Rows

import numpy as np


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

a[1:]
# array([[4, 5, 6],
#        [7, 8, 9]])

a[:1]
# array([[1, 2, 3]])

a[1:3]
# array([[4, 5, 6],
#        [7, 8, 9]])
import numpy as np


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

a[::2]
# array([[1, 2, 3],
#        [7, 8, 9]])

a[1::2]
# array([[4, 5, 6]])

4.10.2.3. Columns

import numpy as np


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

a[:, 0]
# array([1, 4, 7])

a[:, 1]
# array([2, 5, 8])

a[:, 2]
# array([3, 6, 9])

a[:, -1]
# array([3, 6, 9])
import numpy as np


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

a[:, 0:1]
# array([[1],
#        [4],
#        [7]])

a[:, 0:2]
# array([[1, 2],
#        [4, 5],
#        [7, 8]])

a[:, :2]
# array([[1, 2],
#        [4, 5],
#        [7, 8]])
import numpy as np


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

a[:, ::2]
# array([[1, 3],
#        [4, 6],
#        [7, 9]])

a[:, 1::2]
# array([[2],
#        [5],
#        [8]])

4.10.2.4. Rows and Columns

import numpy as np


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

a[0:1, 0:1]
# array([[1]])

a[0:1, 0:2]
# array([[1, 2]])

a[0:1, 0:3]
# array([[1, 2, 3]])

a[0:2, 0:2]
# array([[1, 2],
#        [4, 5]])

a[-1:, -2:]
# array([[8, 9]])
import numpy as np


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

a[::2, ::2]
# array([[1, 3],
#        [7, 9]])

a[1::2, 1::2]
# array([[5]])

4.10.3. Newaxis

import numpy as np


a = np.array([1, 2, 3])

a[:, np.newaxis]
# array([[1],
#        [2],
#        [3]])

a[np.newaxis, :]
# array([[1, 2, 3]])
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

a[:, np.newaxis]
# array([[[1, 2, 3]],
#        [[4, 5, 6]]])

a[np.newaxis, :]
# array([[[1, 2, 3],
#         [4, 5, 6]]])
import numpy as np


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

a[:, np.newaxis, 1]
# array([[2],
#        [5],
#        [8]])

a[np.newaxis, :, 1]
# array([[2, 5, 8]])

a[1, np.newaxis, :]
# array([[4, 5, 6]])

4.10.4. Assignments

4.10.4.1. Array Slicing

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

  2. Print inner 2x2 elements

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

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

  2. Wybierz wewnętrzne 2x2 elementy

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

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

  • Generating random np.array

4.10.4.2. Sum of inner elements

English
  1. Use only random module from numpy library

  2. Set random seed to zero

  3. Generate DATA: ndarray with 16x16 random digits (0-9 inclusive)

  4. Calculate sum of inner 4x4 elements

  5. Inner matrix is exactly in the middle of outer

Polish
  1. Użyj tylko funkcji z modułu random biblioteki numpy

  2. Ustaw ziarno losowości na zero

  3. Wygeneruj DATA: ndarray z 16x16 losowych cyfr (0-9 włącznie)

  4. Policz sumę środkowych 4x4 elementów

  5. Środkowa macierz jest dokładnie w środku większej

../../_images/random-inner-sum.png

Figure 4.7. Sum of inner elements

Hint
  • ndarray.sum()