3.19. Array Select

3.19.1. Unique

import numpy as np


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

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

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

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

3.19.2. Diagonal

import numpy as np


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

a.diagonal()
# array([1, 4])
import numpy as np


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

a.diagonal()
# array([1, 5])
import numpy as np


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

a.diagonal()
# array([1, 5, 9])

3.19.3. Nonzero

import numpy as np


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

a.nonzero()
# (array([0, 0, 1, 1]), array([0, 2, 0, 2]))

3.19.4. Where

3.19.4.1. Single argument

  • where(boolarray)

  • indexes of elements

import numpy as np


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

np.where(a != 2)
# (array([0, 2]),)

np.where(a > 1)
# (array([1, 2]),)

np.where(a % 2 != 0)
# (array([0, 2]),)
import numpy as np


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

np.where(a != 3)
# (array([0, 0, 1, 1, 1]), array([0, 1, 0, 1, 2]))

np.where(a % 2 != 0)
# (array([0, 0, 1]), array([0, 2, 1]))

3.19.4.2. Multiple argument

  • where(boolarray, truearray, falsearray):

import numpy as np


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

np.where(a % 2, 'odd', 'even')
# array([['odd', 'even', 'odd'],
#        ['even', 'odd', 'even']], dtype='<U4')
import numpy as np


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

np.where(a > 4, 99, 77)
# array([[77, 77, 77],
#        [77, 99, 99]])
import numpy as np


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

np.where(a != 3, a, np.nan)       # if ``a != 3`` return element, otherwise ``np.nan``
# array([[ 1.,  2., nan],
#        [ 4.,  5.,  6.]])
import numpy as np


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

b = np.logical_and(a > 0, a % 3 == 0)
# array([[False, False,  True],
#        [False, False,  True]])

a[b]
# array([3, 6])

3.19.5. Fancy indexing

import numpy as np


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

a > 2
# array([[False, False,  True],
#        [ True,  True,  True]])

a[a > 2]
# array([3, 4, 5, 6])
import numpy as np


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

even = (a % 2 == 0)
a[even]
# array([2, 4, 6])
import numpy as np


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

a[np.logical_and(a > 2, a <= 5)]
# array([3, 4, 5])
import numpy as np


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

at_index = np.array([0, 1, 0])
a[at_index]
# array([1, 2, 1])

at_index = np.array([0, 2])
a[at_index]
# array([1, 3])
import numpy as np


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

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

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

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


a = np.array([[1, 4], [9, 16]], float)
b = np.array([0, 0, 1, 1, 0], int)
c = np.array([0, 1, 1, 1, 1], int)

a[b,c]
# array([ 1., 4., 16., 16., 4.])
a[ [1,2] ]
array([[4, 5, 6],
       [7, 8, 9]])

3.19.6. Take

import numpy as np


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

at_index = np.array([0, 0, 1, 2, 2, 1])

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


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

at_index = np.array([0, 0, 1])

a.take(at_index, axis=0)
# array([[1, 2, 3],
#        [1, 2, 3],
#        [4, 5, 6]])

a.take(at_index, axis=1)
# array([[1, 1, 2],
#        [4, 4, 5]])

3.19.7. Assignments

3.19.7.1. Array filtering

English
  1. Set random seed to 0

  2. Generate INPUT: ndarray of size 50x50

  3. INPUT must contains random integers from 0 to 1024 inclusive

  4. Create OUTPUT: ndarray with elements selected from INPUT which are power of two

  5. Sort OUTPUT in descending order

  6. Print OUTPUT

Polish
  1. Ustaw ziarno losowości na 0

  2. Wygeneruj INPUT: ndarray rozmiaru 50x50

  3. INPUT musi zawierać losowe liczby całkowite z zakresu od 0 do 1024 włącznie

  4. Stwórz OUTPUT: ndarray z elementami wybranymi z INPUT, które są potęgami dwójki

  5. Posortuj OUTPUT w kolejności malejącej

  6. Wypisz OUTPUT

Hint
  • np.random.randint()

  • np.isin(a, b)

  • np.flip(a)