Skip to article frontmatterSkip to article content
import numpy as np

contenu de ce notebook (sauter si déjà acquis)

tests sur les tableaux multi-dimensionnels numpy par fonctions vectorisées ufunc

masques/filtres booléens

composition des conditions
opérateurs logiques bit-à-bit & | ~
équivalent numpy np.logical_and np.logical_or np.logical_not

obtenir une vue sur les éléments du tableau initial
numpy.argwhere, numpy.nonzero et numpy.putmask

tests sur tableaux multi-dimensionnels

# le code
tab = np.random.randint(10, size=(2, 3))
print(tab)
print(tab % 2 == 0)
print(np.equal(tab % 2, 0))
res = tab % 2 == 0
print(res.shape)
[[9 9 4]
 [7 5 6]]
[[False False  True]
 [False False  True]]
[[False False  True]
 [False False  True]]
(2, 3)

n’utilisez pas de for-python: utilisez les ufunc

# > est une ufunc

# on peut écrire indifféremment
tab > 5
array([[ True, True, False], [ True, False, True]])
# ou bien
np.greater(tab, 5)
array([[ True, True, False], [ True, False, True]])

combiner les résultats

# le code
tab = np.random.randint(10, size=(2, 3))
res = np.equal(tab%2, 0)
print(np.any(res))
print(np.all(res))
print(np.sum(res))
print(np.count_nonzero(tab%2==0))
print(np.sum(res, axis=0))
np.count_nonzero(tab%2==0, axis=0)
True
False
1
1
[1 0 0]
array([1, 0, 0])

les masques/filtres booléens

# le code
tab = np.random.randint(-10, 10, size=(2, 3, 4))
tab
array([[[ 1, 1, -5, 6], [ 5, -7, -3, 8], [ 8, -7, 0, -5]], [[ 9, -2, -2, -4], [ 0, -3, -1, -9], [ 0, -8, -10, -9]]])
# le code
print(tab[np.greater(tab, 0)])
print(tab[tab > 0])
[1 1 6 5 8 8 9]
[1 1 6 5 8 8 9]
# le code
tab [tab > 0] = 0
tab
array([[[ 0, 0, -5, 0], [ 0, -7, -3, 0], [ 0, -7, 0, -5]], [[ 0, -2, -2, -4], [ 0, -3, -1, -9], [ 0, -8, -10, -9]]])
# le code
np.argwhere(tab==0)
array([[0, 0, 0], [0, 0, 1], [0, 0, 3], [0, 1, 0], [0, 1, 3], [0, 2, 0], [0, 2, 2], [1, 0, 0], [1, 1, 0], [1, 2, 0]])

composition des conditions

# le code
tab = np.random.randint(100, size=(3, 4))
print(tab)
print((tab >= 25) & (tab < 75))
print((tab >= 25) & (tab < 75) & ~(tab%2==0))
[[71 85 20 68]
 [ 8 35 35 52]
 [55 94 73 52]]
[[ True False False  True]
 [False  True  True  True]
 [ True False  True  True]]
[[ True False False False]
 [False  True  True False]
 [ True False  True False]]
# le code
print(np.logical_and(tab >= 25, tab < 75))
print(np.logical_and(tab >= 25, np.logical_and(tab < 75, np.logical_not(tab%2==0))))
[[ True False False  True]
 [False  True  True  True]
 [ True False  True  True]]
[[ True False False False]
 [False  True  True False]
 [ True False  True False]]
# ATTENTION
# en Python pur on a le droit d'écrire un test comme
# 25 <= tab < 75
# MAIS comme ça va implicitement faire un 'and'
# ça ne fonctionne pas avec les tableaux numpy
try:
    25 <= tab < 75
except Exception as exc:
    print("OOPS - ne marche pas avec numpy\n", exc)
OOPS - ne marche pas avec numpy
 The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

modifier les éléments dans tableau d’origine

affecter une sélection

# le code
tab = np.array([[1, 2, 3], [4, 5, 6]])
tab[tab % 2 == 0] = 100
print(tab)
[[  1 100   3]
 [100   5 100]]

c’est fragile (1)

# le code
tab = np.array([[1, 2, 3], [4, 5, 6]])
view = tab[tab%2==0]
view[...] = 100
print(tab)
print(view)
[[1 2 3]
 [4 5 6]]
[100 100 100]

c’est fragile (2)

# le code
tab = np.array([[1, 2, 3], [4, 5, 6]])
tab[tab%2==0][0] = 100
print(tab)
[[1 2 3]
 [4 5 6]]

repérer les éléments par leurs indices


la fonction numpy.nonzero

tab = np.array([[1, 2, 3], [4, 5, 6]])
print("non zero", np.nonzero(~(tab%2==0)))
print("elements", tab[0, 0], tab[0, 2], tab[1, 1])
print("filter", tab[np.nonzero(~(tab%2==0))])
tab[np.nonzero(~(tab%2==0))] = 0
print("edited tab", tab)
non zero (array([0, 0, 1]), array([0, 2, 1]))
elements 1 3 5
filter [1 3 5]
edited tab [[0 2 0]
 [4 0 6]]

la fonction numpy.argwhere

# le code
tab = np.array([[1, 2, 3], [4, 5, 6]])
cond = ~(tab%2==0)
print(np.argwhere(cond).T)
print(np.nonzero(cond))
print(tuple(np.argwhere(cond).T))
tab[tuple(np.argwhere(cond).T)]
[[0 0 1]
 [0 2 1]]
(array([0, 0, 1]), array([0, 2, 1]))
(array([0, 0, 1]), array([0, 2, 1]))
array([1, 3, 5])

modifier avec array.putmask()

# le code
tab = np.arange(12).reshape(3, 4)
np.putmask(tab, tab%2==1, 0)
tab
array([[ 0, 0, 2, 0], [ 4, 0, 6, 0], [ 8, 0, 10, 0]])