3.10. Array Slicing

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

3.10.2. 2-dimensional Array

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

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

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

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

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

3.10.4. Assignments

3.10.4.1. Array Slicing

  • Complexity level: easy

  • Lines of code to write: 3 lines

  • Estimated time of completion: 5 min

  • Filename: solution/numpy_slicing.py

English
  1. Use input ndarray (see below)

  2. Select inner 3x3 and save to OUTPUT: ndarray

  3. Print OUTPUT

Polish
  1. Użyj wejściowej ndarray (patrz poniżej)

  2. Wybierz wewnętrzne 3x3 i zapisz do OUTPUT: ndarray

  3. Wypisz OUTPUT

Input
INPUT = np.array([
    [7, 5, 3, 4, 5],
    [2, 2, 8, 1, 5],
    [3, 8, 8, 4, 4],
    [5, 5, 5, 2, 5],
    [0, 1, 0, 6, 0],
])
Output
print(OUTPUT)
# [[2 8 1]
#  [8 8 4]
#  [5 5 2]]
The whys and wherefores
  • Defining np.array

  • Generating random np.array

3.10.4.2. Sum of inner elements

English
  1. Use only random module from numpy module

  2. Set random seed to zero

  3. Generate outer: 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żywaj tylko modułu random z modułu numpy

  2. Ustaw ziarno losowości na zero

  3. Wygeneruj outer: 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-sum1.png

Figure 71. Sum of inner elements

Hint
  • np.array.sum()