Académique Documents
Professionnel Documents
Culture Documents
Anne 2014/2005
Pr.M.Naoum
CONTENTS
le module numpy
1.1 Le module numpy de python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
1
le module scipy
2.1 Le module scipy de python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9
9
le module matplotlib
3.1 Le module matplotlib de python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
11
ii
CHAPTER
ONE
LE MODULE NUMPY
1.1 Le module numpy de python
1.1.1 Calcul numrique avec NumPy
Prsentation de NumPy
Module Python pour la manipulation de grands tableaux de donnes :
dimension quelconque
lments de mme type
optimisation
indexation habituelle avec [ ... ]
type ndarray
Pour utiliser NumPy :
import numpy
ones :
>>> numpy.ones((2,3), dtype=float)
array([[ 1., 1., 1.],
[ 1., 1., 1.]])
zeros :
>>> numpy.zeros(7, dtype=int)
array([0, 0, 0, 0, 0, 0, 0])
arange :
>>> numpy.arange(4, dtype=float, step=0.5)
array([ 0. , 0.5, 1. , 1.5, 2. , 2.5,
3. ,
3.5])
0.],
0.],
0.],
0.]])
Produit : numpy.dot
Produit scalaire :
Produit matriciel :
>>> a = numpy.array([[0, -1], [1, 0]]) ; a
array([[ 0, -1],
[ 1, 0]])
>>> b = numpy.array([[2], [1]]) ; b
array([[2],
[1]])
>>> numpy.dot(a, b)
array([[-1],
[ 2]])
Oprations logiques
Les oprateurs de comparaison fonctionnent eux aussi terme terme. Rsultat : tableau de boolens.
>>> a = numpy.array([[2, -6], [1, 3]]) ; a
array([[ 2, -6],
[ 1, 3]])
>>> a > 1
array([[ True, False],
[False, True]], dtype=bool)
Transposition
Mthode transpose :
Redimensionnement de tableau
Mthode reshape : modifie le tableau en place
>>> a = numpy.array([[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]])
>>> a
array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12]])
>>> a.reshape((3, 4))
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
Mthode flatten :
>>> a = numpy.array([[ 1,
>>> a
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
>>> a.flatten()
array([ 1, 2, 3,
4,
5,
2,
6,
3,
7,
4], [ 5,
8,
6,
7,
Attention : comme dhabitude, les variables sont des rfrences aux objets...
>>> b = a
>>> b[1, 0] = 0
>>> a
array([[1, 2],
[0, 4]])
Remplissage de tableau
fill remplit un tableau sans changer sa forme ni son type (modification en place) :
>>> a = numpy.array([[ 1,
>>> a
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8]])
2,
3,
4], [ 5,
6,
7,
8]])
>>> a.fill(0)
>>> a
array([[0, 0, 0, 0],
[0, 0, 0, 0]])
Copie de tableau
Mthode copy :
>>> a = numpy.array([[1, 2], [3, 4]]) ; a
array([[1, 2],
[3, 4]])
>>> b = a.copy() ; b
array([[1, 2],
[3, 4]])
>>> b[1, 0] = 0
>>> b
array([[1, 2],
[0, 4]])
>>> a
array([[1, 2],
[3, 4]])
Extraction de ligne :
>>> a[0]
array([1, 0, 0, 0])
Extraction de colonne :
>>> a[:,2]
array([0, 0, 3, 0])
Sous-tableaux (2)
Sous-tableaux (3)
= 9 * numpy.ones(4)
9,
9,
9,
9,
>>> a[:2,-2:]
array([[1, 9,
[0, 9,
[0, 9,
[0, 9,
0,
0,
3,
0,
0],
0],
0],
4]])
= 7 * numpy.ones(2) ; a
7, 7],
7, 7],
3, 0],
0, 4]])
Algbre linaire
numpy.linalg.det Dterminant
>>> a = numpy.array([[1/3, 0], [0, 3]]) ; a
array([[ 0.33333333, 0.
],
[ 0.
, 3.
]])
>>> numpy.linalg.det(a)
1.0
numpy.linalg.inv Inverse
>>> numpy.linalg.inv(a)
array([[ 0.66666667, -0.33333333],
[-0.33333333, 0.66666667]])
>>> numpy.linalg.inv(numpy.array([[1, 2], [2, 4]]))
Traceback (most recent call last):
...
numpy.linalg.linalg.LinAlgError: Singular matrix
numpy.linalg.norm Norme
>>> numpy.linalg.norm([3, 4])
5.0
CHAPTER
TWO
LE MODULE SCIPY
2.1 Le module scipy de python
10
CHAPTER
THREE
LE MODULE MATPLOTLIB
3.1 Le module matplotlib de python
11