Vous êtes sur la page 1sur 15

Ingnierie numrique avec python

Anne 2014/2005

Pr.M.Naoum

May 07, 2015

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

Cration dun tableau


numpy.array( ) :
# 1D (vecteur)
>>> a = numpy.array([3, 6, 2, 8, -3, -5, 9])
>>> a
array([ 3, 6, 2, 8, -3, -5, 9])
# 2D (matrice)
>>> b = numpy.array([[0.707, -0.707], [0.707, 0.707]])
>>> b
array([[ 0.707, -0.707],
[ 0.707, 0.707]])

Indiquer explicitement le type des lments

Argument nomm dtype (data type) :

Ingnierie numrique avec python, Anne 2014/2005

>>> c = numpy.array([3, 2.7, 1], dtype=int)


>>> c
array([3, 2, 1])
>>> d = numpy.array([3, 2, 1], dtype=float)
>>> d
array([ 3., 2., 1.])
>>> e = numpy.array([3, 2, 1], dtype=complex)
>>> e
array([ 3.+0.j, 2.+0.j, 1.+0.j])
>>> # connatre le type des lments
>>> e.dtype
dtype(complex128)

Cration de tableaux spciaux

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

Cration de tableaux spciaux (2)

identity matrice identit :


>>> numpy.identity(3)
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])

eye matrice diagonale :


>>> numpy.eye(4, k=-1)
array([[ 0., 0., 0.,
[ 1., 0., 0.,
[ 0., 1., 0.,
[ 0., 0., 1.,

0.],
0.],
0.],
0.]])

k est le numro de la diagonale, en partant de k = 0 pour la diagonale principale.


Obtenir des informations sur un tableau
Taille :
2

Chapter 1. le module numpy

Ingnierie numrique avec python, Anne 2014/2005

>>> a = numpy.array([[1, 2, 3, 4], [5, 6, 7, 8]])


>>> a.shape
(2, 4)

Contient-il une certaine valeur ?


>>> 2 in a
True
>>> 9 in a
False

Obtenir les lments de la diagonale : diagonal


Obtenir des informations sur un tableau (2)

sum, prod somme, produit des lments


mean moyenne
min, max extrema
argmin, argmax indice dun extremum dans le tableau aplati
>>> a = numpy.array([[-2, 3, 1], [2, 7, -5]]) ; a
array([[-2, 3, 1],
[ 2, 7, -5]])
>>> a.sum()
6
>>> a.mean()
1.0
>>> a.argmax()
4

Oprations sur les tableaux


Les oprateurs airthmtiques habituels fonctionnent sur les tableaux, terme terme :
+ : addition de matrices
- : soustraction de matrices
mais * nest pas le produit de matrices
>>> 3 * numpy.identity(2)
array([[ 3., 0.],
[ 0., 3.]])
>>> numpy.array([[1, 2], [3, 4]]) + numpy.ones(2)
array([[ 2., 3.],
[ 4., 5.]])

Produit : numpy.dot

Produit scalaire :

1.1. Le module numpy de python

Ingnierie numrique avec python, Anne 2014/2005

>>> numpy.dot(numpy.array([1, 2]), numpy.array([3, 4]))


11

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)

Prdicats all et any :


>>> (a > 0).all()
False
>>> (a > 0).any()
True

Oprations logiques (2)

Oprateurs boolens terme terme :


numpy.logicial_or
numpy.logicial_and
numpy.logicial_not
>>> numpy.logical_or(a < -4, a >= 1)
array([[ True, True],
[ True, True]], dtype=bool)
>>> numpy.logical_or(a < -4, a >= 1).all()
True

Transposition

Mthode transpose :

Chapter 1. le module numpy

Ingnierie numrique avec python, Anne 2014/2005

>>> a = numpy.array([[2], [1]]) ; a


array([[2],
[1]])
>>> a.transpose()
array([[2, 1]])

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

Le nombre dlments ne peut changer :


>>> a.reshape((2, 5))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: total size of new array must be unchanged

Cas particulier : aplatir un tableau

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,

8], [ 9, 10, 11, 12]])

9, 10, 11, 12])

Accs et modification dlments de tableaux


Syntaxe Python habituelle [ ]
>>> a = numpy.array([[1, 2], [3, 4]]) ; a
array([[1, 2],
[3, 4]])
>>> a[1, 0] = 5 ; a
array([[1, 2],
[5, 4]])

Attention : comme dhabitude, les variables sont des rfrences aux objets...

1.1. Le module numpy de python

Ingnierie numrique avec python, Anne 2014/2005

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

Slection dlments dun tableau


Entre [ ] peut figurer une condition portant sur des tableaux :
>>> a = numpy.array([[6, 4], [5, 9]]) ; a
array([[6, 4],
[5, 9]])
>>> a[a >= 6]
array([6, 9])

(La condition est un tableau de boolens...)


Sous-tableaux
Extraction de vecteurs ligne ou colonne

Chapter 1. le module numpy

Ingnierie numrique avec python, Anne 2014/2005

>>> a = numpy.diag([1, 2, 3, 4]) ; a


array([[1, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 3, 0],
[0, 0, 0, 4]])

Extraction de ligne :
>>> a[0]
array([1, 0, 0, 0])

Extraction de colonne :
>>> a[:,2]
array([0, 0, 3, 0])

Sous-tableaux (2)

Slection de plages avec :


>>> a = numpy.diag([1, 2, 3, 4]) ; a
array([[1, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 3, 0],
[0, 0, 0, 4]])
>>> a[0:3,1:]
array([[0, 0, 0],
[2, 0, 0],
[0, 3, 0]])

Sous-tableaux (3)

Fonctionne aussi pour laffectation :


>>> a[:,1]
>>> a
array([[1,
[0,
[0,
[0,

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

1.1. Le module numpy de python

Ingnierie numrique avec python, Anne 2014/2005

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

Algbre linaire (2)

numpy.linalg.norm Norme
>>> numpy.linalg.norm([3, 4])
5.0

numpy.linalg.eig Valeurs et vecteurs propres


>>> eigval, eigvec = numpy.linalg.eig(a)
>>> eigval # valeurs propres
array([ 3., 1.])
>>> eigvec # vecteurs propres norms en colonne
array([[ 0.70710678, -0.70710678],
[ 0.70710678, 0.70710678]])

Pour aller plus loin


Autres domaines de NumPy :
Polynmes
Nombres alatoires
SciPy tend considrablement NumPy :
Transforme de Fourier discrte
Intgration
Interpolation
Algbre linaire avance
Traitement du signal (filtres...)
=> NumPy contient les bases ; SciPy est un ensemble de botes outils spcialises

Chapter 1. le module numpy

CHAPTER

TWO

LE MODULE SCIPY
2.1 Le module scipy de python

Ingnierie numrique avec python, Anne 2014/2005

10

Chapter 2. le module scipy

CHAPTER

THREE

LE MODULE MATPLOTLIB
3.1 Le module matplotlib de python

11

Vous aimerez peut-être aussi