Numpy
Numpy
Tableaux
NumPy propose une structure de données populaire, les tableaux
(de type array), sur lesquels il est possible d’effectuer de manière
efficace des calculs. Les tableaux sont une structure notamment
utile pour effectuer des opérations statistiques basiques ainsi que
de la génération pseudo-aléatoire de nombres.
Création
La création d’un tableau peut s’effectuer avec la méthode array(), à
partir d’une liste, comme nous venon de le faire :
[1 2 4]
Out[2]: numpy.ndarray
In [3]: tableau==1
In [4]: tableau
In [5]: tableau==2
In [10]: s=np.where(tableau==tableau)
In [11]: s
In [7]: tableau==tableau
In [18]: print(tableau)
s=np.where(tableau==tableau)
https://github.com/siriman/pythonDS2021/blob/main/Numpy.ipynb 2/22
07/04/2021 pythonDS2021 / Numpy.ipynb sur main · siriman / pythonDS2021 · GitHub
print(s)
[1 2 4]
(array([0, 1, 2]),)
In [19]: print(s)
(array([0, 1, 2]),)
[[1 2 3]
[4 5 6]]
[1 2 3]
[[3 2]
[5 1]
[6 5]]
In [23]: tableau.ndim
Out[23]: 2
[0. 0. 0. 0.]
On peut préciser le type des zéros (par exemple int, int32, int64,
float, float32, float64, etc.), à l’aide du paramètre dtype
https://github.com/siriman/pythonDS2021/blob/main/Numpy.ipynb 3/22
07/04/2021 pythonDS2021 / Numpy.ipynb sur main · siriman / pythonDS2021 · GitHub
[0 0 0 0]
Le type des éléments d’un tableau est indiqué dans l’attribut dtype :
[0 0 0 0] int64
In [27]: y = x.astype("float")
print(x, x.dtype)
[0 0 0 0] int64
[[0. 0. 0.]
[0. 0. 0.]]
[[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]]
[[0 0 0]
[0 0 0]]
[[1. 1. 1.]
[1. 1. 1.]]
https://github.com/siriman/pythonDS2021/blob/main/Numpy.ipynb 4/22
07/04/2021 pythonDS2021 / Numpy.ipynb sur main · siriman / pythonDS2021 · GitHub
[[1 0]
[0 1]]
[[0. 0. 0.]
[1. 0. 0.]
[0. 1. 0.]]
[[1 0 0]
[0 1 0]
[0 0 1]]
[0 1 2 3 4]
[2 3 4]
( (
https://github.com/siriman/pythonDS2021/blob/main/Numpy.ipynb) ) 5/22
07/04/2021 pythonDS2021 / Numpy.ipynb sur main · siriman / pythonDS2021 · GitHub
In [40]: print( np.arange(2, 10, 2) )
[2 4 6 8]
Dimensions
Pour connaître la dimension d’un tableau, on peut afficher la valeur
de l’attribut ndim :
ndim tableau : 2
ndim tableau_2 : 2
size tableau : 6
size tableau_2 : 6
np.size(tableau) : 6
In [50]: print(tableau_1)
[ 1 2 3 4 5 6 7 8 9 10 11 12]
In [51]: type(tableau_1)
Out[51]: numpy.ndarray
In [52]: tableau_1[5]
Out[52]: 6
In [53]: tableau_2.shape
Out[53]: (4, 3)
In [54]: tableau_2
In [55]: tableau_1[0:4]
In [61]: tableau_2
Out[61]: array([[ 1 2 3]
https://github.com/siriman/pythonDS2021/blob/main/Numpy.ipynb 7/22
07/04/2021 pythonDS2021 / Numpy.ipynb sur main · siriman / pythonDS2021 · GitHub
Out[61]: array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
In [68]: tableau_2[[0,2],2]
In [59]: tableau_1[-1]
Out[59]: 12
tableau_1[-1] : 12
tableau_2[-1] : [10 11 12]
In [50]: tableau_1[2:6]
In [51]: tableau_1==1
In [52]: tableau_1==tableau_1
In [53]: print(np.where(tableau_1==tableau_1))
(array([ 0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11]),)
Slice Tableau 1 :
[3 4]
In [55]: tableau_2
In [56]: tableau_1
Out[56]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12])
In [57]: tableau_1.shape=(4,3)
https://github.com/siriman/pythonDS2021/blob/main/Numpy.ipynb 8/22
07/04/2021 pythonDS2021 / Numpy.ipynb sur main · siriman / pythonDS2021 · GitHub
In [58]: tableau_1.shape
Out[58]: (4, 3)
In [59]: np.reshape?
In [61]: tableau_2
In [62]: tableau_2[1:3,:]
Scile Tableau 2 :
[[ 7 8 9]
[10 11 12]]
In [64]: print(tableau_2[2][0])
print(tableau_2[2,0])
7
7
In [65]: tableau_2
Deuxième colonne :
[[ 2]
[ 5]
[ 8]
[11]]
Deuxièmes et troisièmes colonnes :
[[ 1 3]
[ 4 6]
[ 7 9]
[10 12]]
Pour extraire ou non des éléments d’un tableu, on peut utiliser des
tableaux de booléens en tant que masques. L’idée est de fournir un
tableau de booléens (un masque) de même dimension que celui
pour lequel on désire extraire des éléments sous certaines
conditions. Lorsque la valeur du booléen dans le masque vaut True,
l’élément correspondant du tableau est retourné ; sinon, il ne l’est
pas.
In [70]: tableau
[0 2 1 4]
In [73]: masque
In [75]: print(type(masque))
<class 'numpy.ndarray'>
https://github.com/siriman/pythonDS2021/blob/main/Numpy.ipynb 10/22
07/04/2021 pythonDS2021 / Numpy.ipynb sur main · siriman / pythonDS2021 · GitHub
In [76]: print(tableau[masque])
[3 5 1]
Modification
Pour remplacer les valeurs d’un tableau, on utilise le signe égal (=) :
In [78]: tableau[3]=10
In [79]: #tableau[0] = 10
print(tableau)
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 10 10]]
In [81]: tableau[0:2]
https://github.com/siriman/pythonDS2021/blob/main/Numpy.ipynb 11/22
07/04/2021 pythonDS2021 / Numpy.ipynb sur main · siriman / pythonDS2021 · GitHub
In [83]: tableau[:] = 1
print(tableau)
[[1 1 1]
[1 1 1]
[1 1 1]
[1 1 1]]
Ajout d’éléments
Pour ajouter des éléments, on utilise la fonction append() de
NumPy. Il faut noter que l’appel à cette fonction ne modifie pas
l’objet auquel on ajoute les valeurs. Si on désire que les
modifications sont apportées à cet objet, il faut l’écraser :
t_1 : [1 3 5]
t_1 après l'ajout : [1 3 5 1]
In [85]: np.append?
In [89]: t_1
In [91]: t_1.shape
Out[91]: (4,)
In [93]: t_2
In [95]: ajout_col_t_2
https://github.com/siriman/pythonDS2021/blob/main/Numpy.ipynb 12/22
07/04/2021 pythonDS2021 / Numpy.ipynb sur main · siriman / pythonDS2021 · GitHub
Out[95]: array([[4],
[8]])
In [97]: t_2
In [98]: t_2
In [99]: x=np.array([1,2,3,4,5,6])
t_2 :
[[1 2 3]
[5 6 7]]
t_2 après ajout colonne :
[[1 2 3 4]
[5 6 7 8]]
In [101]: np.vstack??
Suppression d’éléments
t_1 : [ 1 3 5 15]
In [105]: x=np.array([1,2,3,4,5,6])
t_1 : [ 1 3 5 15]
[ 3 15]
t_2 : [[ 1 2 3 4]
[ 5 6 7 8]
[10 11 12 54]]
t_2 : [[ 1 2 3 4]
[ 5 6 7 8]
[10 11 12 54]]
Out[111]: array([[ 2, 4],
[ 6, 8],
[11, 54]])
t_2 : [[ 1 2 3 4]
[ 5 6 7 8]
[10 11 12 54]]
Out[112]: array([[ 5, 6, 7, 8],
https://github.com/siriman/pythonDS2021/blob/main/Numpy.ipynb 14/22
07/04/2021 pythonDS2021 / Numpy.ipynb sur main · siriman / pythonDS2021 · GitHub
t_2 : [[ 1 2 3 4]
[ 5 6 7 8]
[10 11 12 54]]
Out[113]: array([[5, 6, 7, 8]])
In [116]: tableau_2
In [117]: tableau_1[1]=10000
In [118]: tableau_1
In [119]: tableau_2
In [120]: tableau_1
In [121]: tableau_2
In [122]: tableau_2-5
In [123]: tableau_1
In [ ]:
Copie de tableau
https://github.com/siriman/pythonDS2021/blob/main/Numpy.ipynb 15/22
07/04/2021 pythonDS2021 / Numpy.ipynb sur main · siriman / pythonDS2021 · GitHub
In [125]: tableau_2[0] = 0
print("Tableau 1 : \n", tableau_1)
Tableau 1 :
[0 2 3]
Tableau 2 :
[0 2 3]
tableau_1 : [1 2 3]
tableau_2 : [0 2 3]
tableau_1 : [1 2 3]
tableau_2 : [0 2 3]
tableau_1 : [0 2 3 4]
tableau_2 : [0 2]
Tri
La librairie NumPy fournit une fonction pour trier les tableaux :
sort().
https://github.com/siriman/pythonDS2021/blob/main/Numpy.ipynb 16/22
07/04/2021 pythonDS2021 / Numpy.ipynb sur main · siriman / pythonDS2021 · GitHub
Tableau trié : [1 2 3 5 5 6]
Tableau : [3 2 5 1 6 5]
Transposition
Pour obtenir la transposée d’un tableau, on fait appel à l’attribut T. Il
faut noter que l’on obtient une vue de l’objet, que cela ne le modifie
pas.
Tableau :
[[3 2]
[5 1]
[6 5]]
In [132]: tableau.shape
Out[132]: (3, 2)
In [133]: tableau.T
In [134]: print(np.transpose(tableau))
[[3 5 6]
[2 1 5]]
tableau :
[[99 2]
[ 5 1]
[ 6 5]]
tableau_transpose :
[[99 5 6]
[ 2 1 5]]
tableau : None
tableau_transpose : [[99 2]
[ 5 1]
[ 6 5]]
Opérateurs + et -
t_1 + 3 :
[4 5 6 7]
t_1 + 3. :
[4. 5. 6. 7.]
https://github.com/siriman/pythonDS2021/blob/main/Numpy.ipynb 18/22