Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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

[[2 6 0]
 [6 2 3]]
[[ True  True  True]
 [ True  True False]]
[[ True  True  True]
 [ True  True False]]
(2, 3)

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

array([[False, True, False], [ True, False, False]])
array([[False, True, False], [ True, False, False]])

combiner les résultats

True
False
4
4
[1 2 1]
array([1, 2, 1])

les masques/filtres booléens

array([[[ -1, -3, -6, -5], [ 3, 4, -10, 4], [ 6, -3, -4, 0]], [[ 0, -5, 9, -7], [ 3, -6, -2, 5], [ 6, 5, -6, -4]]])
[3 4 4 6 9 3 5 6 5]
[3 4 4 6 9 3 5 6 5]
array([[[ -1, -3, -6, -5], [ 0, 0, -10, 0], [ 0, -3, -4, 0]], [[ 0, -5, 0, -7], [ 0, -6, -2, 0], [ 0, 0, -6, -4]]])
array([[0, 1, 0], [0, 1, 1], [0, 1, 3], [0, 2, 0], [0, 2, 3], [1, 0, 0], [1, 0, 2], [1, 1, 0], [1, 1, 3], [1, 2, 0], [1, 2, 1]])

composition des conditions

[[48 89 72  8]
 [77  4 27 25]
 [18 93 88 87]]
[[ True False  True False]
 [False False  True  True]
 [False False False False]]
[[False False False False]
 [False False  True  True]
 [False False False False]]
[[ True False  True False]
 [False False  True  True]
 [False False False False]]
[[False False False False]
 [False False  True  True]
 [False False False False]]
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

[[  1 100   3]
 [100   5 100]]

c’est fragile (1)

[[1 2 3]
 [4 5 6]]
[100 100 100]

c’est fragile (2)

[[1 2 3]
 [4 5 6]]

repérer les éléments par leurs indices


la fonction numpy.nonzero

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

[[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()

array([[ 0, 0, 2, 0], [ 4, 0, 6, 0], [ 8, 0, 10, 0]])