Chapitre 2 Array in Python
Chapitre 2 Array in Python
Chapitre 2 Array in Python
de Kairouan
Cours Python
Niveau 2éme année
Chapitre 2 :
Les tableaux en Python
2020-2021
2
Plan
o Introduction
o Création de tableaux monodimensionnels
o Création de tableaux à n dimensions
o Indiçage et accès
o Slicing pour les vecteurs
o Slicing pour les matrices
3
Introduction
• Nous allons voir comment créer des tableaux avec
la fonction array() du module NumPy (Numerical
Python).
• Ce module permet la manipulation des vecteurs et
des matrices grâce à des fonctions de calcul numériques et
matriciels.
• • L’objet tableau en python s’appelle array et le type
correspondant est : numpy.ndarray
4
Autre façon :
• La création de matrice se fait avec la fonction
numpy.array() et une liste de liste
>>> A=np.array([[1,2,3],[3,4,5],[5,6,7]])
>>> print (A)
• La fonction tolist() permet de récupérer une
liste de liste à partir d’une matrice
>>> A.tolist()
10
Indiçage et accès
Même indiçage que les listes
– Tableau à une dimension (vecteur)
>>> a = np.array([4,7,9])
>>> a[0]
>>> a[-1]
>>> a[-1]=5 ; print(a)
– Tableau à deux dimensions (matrice)
>>> m=np.array([[1,2,3],[3,4,5],[5,6,7]])
>>> m[1,1]
11
Exemple:
>>> A=np.array([[1,2,3],[3,4,5],[5,6,7]])
>>> A[ : , :]
>>> B=A[ : , : ]
>>> id(A), id(B)
>>> A[ 1 : , 2:]
>>> A[ 1 : , 1:]
>>> A[ : , 1:]
>>> A[ : 2 , :]
>>> A[ 1 , :]
>>> A[ : , 2]
14
La fonction numpy.size()
La fonction numpy.size() renvoie le nombre
d’éléments du tableau :
>>> a = np.array([4,7,9,12])
>>> np.size(a)
>>> m=np.array([[1,2,3],[3,4,5],[5,6,7]])
>>> np.size(m)
15
La fonction numpy.shape()
La fonction numpy.shape() renvoie la taille du
tableau :
>>> a = np.array([4,7,9,12])
>>> np.shape(a)
>>> m=np.array([[1,2,3],[3,4,5],[5,6,7]])
>>> np.shape (m)
16
La fonction numpy.dot()
La fonction numpy.dot() permet de réaliser le
produit matriciel
• rappel : le produit d’une matrice de taille (n,m)
par une matrice (m,p) donne une matrice (n,p)
>>> a=np.array([[1,2,3],[3,4,5]])
>>> b=np.array([[8],[6],[3]])
>>> np.dot(a,b)
18
Somme
>>> a=np.array([[1,2,3],[3,4,5]])
>>> a.sum (), a.max(), a.min()
>>> a.sum(axis=0) # somme des colonnes
>>> a.sum(axis=1) # somme des lignes
>>> r= np.sqrt(a) # print (r)
>>> e= np.exp(a) # print (e)
19
La fonction numpy.zeros()
La fonction numpy.zeros() renvoie un tableau
de 0 :
>>> z = np.zeros(4)
>>> print (z)
>>> zz=np.zeros((3,3))
>>> print (zz)
>>> zzi=np.zeros((3,3),int)
>>> print (zzi)
20
La fonction numpy.ones()
La fonction numpy.ones() renvoie un tableau
de 1 :
>>> o = np.ones(3)
>>> print (o)
>>> oi = np.ones(3,int)
>>> print (oi)
>>> ob = np.ones(3,bool)
>>> print (ob)
21
La fonction numpy.eye(n)
La fonction numpy.eye(n) renvoie une matrice
identité d’ordre n :
>>> i = np.eye(3)
>>> print (i)
>>> i2 = np.eye(3,dtype=int)
>>> print (i2)
22
La fonction numpy.transpose()
La fonction numpy.transpose() renvoie la
transposée d’une matrice donnée :
>>> b=np.array([[8,6,3],[3,4,5]])
>>> t=np.transpose(b)
23
Un peu d’algèbre linéaire
Numpy.linalg : un module d’algèbre linéaire