Chapitre 9 - Bibliothèques Scientifiques
Chapitre 9 - Bibliothèques Scientifiques
Chapitre 9 - Bibliothèques Scientifiques
BIBLIOTHÈQUE SCIENTIFIQUE
EN PYTHON
Mustapha HAIN
INTRODUCTION
Python possède des librairies pour à peu près tout ce que vous
pouvez imaginer, et notamment:
Numpy et Scipy pour les calculs matriciels
Pandas pour gérer les données (les charger, appliquer des
opérations d'algèbre relationnelle, etc.)
Matplotlib et Seaborn pour la visualisation
Scikit-learn pour les algorithmes IA
Tensorflow et PyTorch pour le deep learning.
2
ENVIRONNEMENT DU TRAVAIL
3
ENVIRONNEMENT DU TRAVAIL
4
DEFINITION
5
LA BIBLIOTHÈQUE NUMPY
Déclaration
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a) [[1 2 3]
[4 5 6]]
print(a.size) 6
print(a.ndim) 2
print(a.shape) (2, 3)
print(type(a)) <class 'numpy.ndarray'>
http://docs.scipy.org/doc/numpy/reference/index.html 6
LA BIBLIOTHÈQUE NUMPY
Fonctions statistiques
import numpy as np [[ 2 4 6]
a=np.array([[2,4,6], [ 4 11 5]
[4,11,5], [ 4 6 12]]
[4,6,12]])
print(a) 2
print(a.min()) 12
print(a.max()) 6.0
print(a.mean()) [[ 2 4 6]
a.sort() [ 4 5 11]
print(a) [ 4 6 12]]
Numpy est un package pour Python spécialisé dans la manipulation des
tableaux (array).
Le package propose un grand nombre des fonctions d’accès rapide aux
données (ex. recherche, extraction), pour les manipulations diverses (ex.
tri), pour les calculs (ex. calcul statistique).
tous les éléments d'un array doivent être du même type. 7
LA BIBLIOTHÈQUE NUMPY
Fonctions diverses
numpy.exp(x) exponentielle
numpy.sign(x) signe
8
LA BIBLIOTHÈQUE NUMPY
Fonctions mathématiques
Fonctions trigonométriques
numpy.sin(x) sinus
numpy.cos(x) cosinus
numpy.tan(x) tangente
numpy.arcsin(x) arcsinus
numpy.arccos(x) arccosinus
numpy.arctan(x) arctangente
9
Atelier x :
Travaillons ensemble-
LA BIBLIOTHÈQUE NUMPY
Fonctions Spécifiques
import numpy as np
a = np.arange(24)
print(a)
Reshaping we can add or remove
dimensions or change number of elements
in each dimension.
b = a.reshape(4,2,3)
print b
11
LA BIBLIOTHÈQUE NUMPY
Fonctions Spécifiques
import numpy as np
a = np.arange(24)
print(a)
Reshaping we can add or remove
dimensions or change number of elements
in each dimension. 4,
b = a.reshape(4,2,3)
2,
print b 3
12
LA BIBLIOTHÈQUE NUMPY
Fonctions Spécifiques
13
LA BIBLIOTHÈQUE NUMPY
Fonctions Spécifiques
a = np.arange(10)
print(a)
s = slice(2,7,2)
print(s)
print (a[s])
a = np.arange(20)
print(a)
s = slice(10,16,5)
print(s)
print a[s] 14
LA BIBLIOTHÈQUE NUMPY
Advanced Indexing
import numpy as np
8 4 1 7 2
arr =
np.array([8,4,1,7,2]) Index 0 Index 1 Index 2 Index 3 Index 4
print(arr[2]) 1 s=arr[-2]+arr[2]-arr[1]
print(s)
print(arr[-2]) 7
15
LA BIBLIOTHÈQUE NUMPY
Advanced Indexing
NumPy - Advanced
Indexing
import numpy as np
Last element from 2nd dim
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print(arr[0, 1])
16
LA BIBLIOTHÈQUE NUMPY
Advanced Indexing
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr[0, 1, 2])
[1, 2, 3] [4, 5, 6]
4 5 6
17
LA BIBLIOTHÈQUE NUMPY
Advanced Indexing
import numpy as np
print(arr[0, 3, 2])
print(arr[1, 0, 2])
18
LA BIBLIOTHÈQUE NUMPY
Advanced Indexing
import numpy as np
x = np.arange(10)
print(x) [0 1 2 3 4 5 6 7 8 9]
print(x[2:7]) [2 3 4 5 6]
print(x[2:7:1]) [2 3 4 5 6]
print(x[2:7:2]) [2 4 6]
print(x[7:1:-3]) [7 4]
19
LA BIBLIOTHÈQUE NUMPY
Advanced Indexing
import numpy as np
import numpy as np [[ 0 1 2 3]
print(np.arange(12).reshape(3,4)) [ 4 5 6 7]
[ 8 9 10 11]]
print(np.arange(12).reshape(2,6)) [[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]]
print(np.arange(10).reshape(2,5))
print(np.arange(10).reshape(5,2))
20
GÉNÉRATION DES VALEURS
Nombres aléatoires
La fonction numpy.random.random() permet d’obtenir des
nombres compris entre 0 et 1 par tirage aléatoire avec une loi
uniforme.
np.random.random() 0.5540884899329033
21
GÉNÉRATION DES VALEURS
numpy.zeros
Returns a new array of specified size, filled with zeros.
import numpy as np
x = np.zeros(5)
print (x) [ 0. 0. 0. 0. 0.]
print(np.zeros(2, dtype=int)) [0 0]
[[0. 0. 0.]
print(np.zeros([3, 3])) [0. 0. 0.]
[0. 0. 0.]]
22
Atelier x :
Travaillons ensemble-
24