0% ont trouvé ce document utile (0 vote)
11 vues24 pages

Chapitre 9 - Bibliothèques Scientifiques

Télécharger au format pdf ou txt
Télécharger au format pdf ou txt
Télécharger au format pdf ou txt
Vous êtes sur la page 1/ 24

FORMATION PYTON

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

La bibliothèque NumPy (http://www.numpy.org/) permet


d’effectuer des calculs avancés sur les tableaux/ Matrices avec
Python.

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

x**n x à la puissance n, exemple : x**2

numpy.sqrt(x) racine carrée

numpy.exp(x) exponentielle

numpy.log(x) logarithme népérien

numpy.abs(x) valeur absolue

numpy.sign(x) signe

8
LA BIBLIOTHÈQUE NUMPY
Fonctions mathématiques

Fonctions mathématiques avec NumPy

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

Arange function Return evenly spaced values within a


given interval.

numpy.arange([start, ]stop, [step, ]dtype=None, *, like=None)

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

Arange function Return evenly spaced values within a


given interval.

numpy.arange([start, ]stop, [step, ]dtype=None, *, like=None)

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 Python slice object is constructed by giving start, stop, and


step parameters to the built-in slice

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

Array indexing is the same as accessing an array elment

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[0]) 8 Index -5 Index -4 Index -3 Index -2 Index -1

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])

print(arr[1, 4]) 2nd element on 1st dim

print(arr[1, -1]) 5th element on 2nd dim

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])

arr[0, 1, 2] prints the value 6.

[[1, 2, 3], [4, 5, 6]] [[7, 8, 9], [10, 11, 12]]

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

4 5 6

17
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, 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 avec pas Nb valeur

np.linspace(3, 9, 10) [ 3. , 3.66666667, 4.33333333, 5.


, 5.66666667, 6.33333333, 7. ,
À partir Jusqu’au 7.66666667, 8.33333333, 9. ]

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

np.random.random(3) array([ 0.86431861, 0.88519197,


0.30663316])
([[ 0.66265691, 0.39385577, 0.09319192],
np.random.random((2,3)) [ 0.43483474, 0.42859904, 0.79189574]])

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]

print(np.zeros([2, 2], [[0 0]


dtype=int)) [0 0]]

[[0. 0. 0.]
print(np.zeros([3, 3])) [0. 0. 0.]
[0. 0. 0.]]
22
Atelier x :

Travaillons ensemble-
24

Vous aimerez peut-être aussi