Formation Python Numpy et Scipy

publicité
Introduction
Les tableaux dans Numpy : la classe array
Scipy et ces fonctionnalitées
Formation Python
Numpy et Scipy
Xavier JUVIGNY
Riadh Fezzani
Calcul Haute Performance
ONERA
Septembre 2008
1/21
Juvigny–Fezzani
Formation Python Numpy et Scipy
Introduction
Les tableaux dans Numpy : la classe array
Scipy et ces fonctionnalitées
Qu’est ce que Numpy ?
module python open-source
création et manipulation des tableaux multidimensionnels
opérations efficaces sur ces tableaux (arithmétique, fonctions
mathématiques, opérations structurales...).
Historique
Numpy est l’héritier de deux modules antérieurs devenus depuis
obsolètes :
Numeric : non efficace pour les grandes dimensions
Numarray : corrige ce problème, mais est moins efficace pour
les petits tableaux
2/21
Juvigny–Fezzani
Formation Python Numpy et Scipy
Introduction
Les tableaux dans Numpy : la classe array
Scipy et ces fonctionnalitées
Contenu de Numpy
core et lib : objets et fonctions de bases chargées lors de
l’importation de Numpy
linalg : algèbre linéaire de base (BLAS/LAPACK)
fft : transformée de Fourrier discrète (FFTPACK)
random : générateurs de nombres aléatoires
Prise en main
chargement
import numpy a s N
fonctions utiles
N . who ( ) # l i s t e d e s v a r i a b l e s c o u r a n t e s
N . i n f o (N) # i n f o r e l a t i v e a N
3/21
Juvigny–Fezzani
Formation Python Numpy et Scipy
Introduction
Les tableaux dans Numpy : la classe array
Scipy et ces fonctionnalitées
Déclaration et manipulation
La sous classe matrix
La sous classe poly1d
Outils de base
déclaration directe à l’aide d’une liste
l = [[1 ,2 ,3] ,[4 ,5 ,6]]
a = N . a r r a y ( l , d t y p e=f l o a t )
b = N . a s a r r a y ( l , d t y p e=i n t )
propriété d’un tableau
n = a . ndim # n o m b r e d e d i m e n s i o n s
n l , nc = a . s h a p e # t a i l l e ( c a s b i d i m e n s i o n n e l )
a . shape [ 0 ] #n b r e d ’ e l e m e n t s d a n s l a d i m e n s i o n 0
b . dtype #t y p e de d o n n e e s
4/21
Juvigny–Fezzani
Formation Python Numpy et Scipy
Introduction
Les tableaux dans Numpy : la classe array
Scipy et ces fonctionnalitées
Déclaration et manipulation
La sous classe matrix
La sous classe poly1d
Déclaration (à peine) plus avancée
vecteur croissant avec pas constant
r 1 = N . a r a n g e ( 0 , 1 , 1 E−2)
r 2 = N . r [ 0 : 1 : 1 E−2] # e q u i v a l e n t a r 1
tableaux particuliers
z = N. z e r o s ( ( 4 , 5 ) ) # t a b l e a u 4 x5 r e m p l i de 0
o = N. ones ( ( 5 , 4 ) ) # t a b l e a u 4 x5 r e m p l i de 1
e = N . empty ( ( 5 , 5 ) ) # s i m p l e a l l o c a t i o n
e [ : ] = 0.523 # e i j = 0 . 5 2 3 p r t t ( i , j )
i = N. eye (4) # m a t r i c e i d e n t i t e
D = N . d i a g ( r , −2) # r e n 2 i e m s o u s d i a g
tableaux aléatoires
r norm 2D = N . random . r a n d n ( 2 , 3 )
r u n i f 3 D = N . random . u n i f o r m ( 0 , 1 , ( 3 , 3 , 2 ) )
5/21
Juvigny–Fezzani
Formation Python Numpy et Scipy
Introduction
Les tableaux dans Numpy : la classe array
Scipy et ces fonctionnalitées
Déclaration et manipulation
La sous classe matrix
La sous classe poly1d
Opérations structurales
redimensionner
r = r 1 . r e s h a p e ( 3 , 3 ) #1 x 9 −−>3 x 3
r C = r norm 2D . f l a t t e n ( ) # l i g n e s b o u t a b o u t
transposer
rt = r . transpose ()
extraire ou affecter une valeur à un ou plusiseurs éléments
r 10 = r [1 ,0] #e q u i v a l e n t a r [ 1 ] [ 0 ]
r [1 ,1] = 0
re = r [ 0 : 3 : 2 , 0 : 3 : 2 ] # = r [ : : 2 , : : 2 ]
r [ − 2 : , : ] = N. ones (2 , r . shape [ 1 ] )
d = N. diag ( r ) # v e c t e u r d i a g o n a l
6/21
Juvigny–Fezzani
Formation Python Numpy et Scipy
Introduction
Les tableaux dans Numpy : la classe array
Scipy et ces fonctionnalitées
Déclaration et manipulation
La sous classe matrix
La sous classe poly1d
recherche d’éléments
maximum/minimum
a max = a . max ( ) #m a x a b s o l u
a c o l m i n = a . min ( 1 ) #m i n s u r c h a q u e l i g n e
a . argmin ( ) # p o s i t i o n du min de a
par expression booléenne
b = r norm 2D > 0 # b i j = r n o r m 2 D i j >0
N . where ( r norm 2D > 0 )
Exemple : Seuillage
borner les éléments d’une matrice à [-1,1] :
r t h = N . a b s ( r norm 2D )
r t h [ N . where ( r p > 1 ) ] = 1
r t h ∗= N . s i g n ( r norm 2D )
7/21
Juvigny–Fezzani
Formation Python Numpy et Scipy
Introduction
Les tableaux dans Numpy : la classe array
Scipy et ces fonctionnalitées
Déclaration et manipulation
La sous classe matrix
La sous classe poly1d
Opérations agissant élément par élément
entre un scalaire et un tableau : +,-,*,/
p r i n t −A # −1∗A
entre deux tableaux de même taille : +,-,*,/
P = A∗∗3 # = A ∗ A ∗ A :
P i j = A i j ∗∗3
ufunc : appliquée à tous les éléments (sin, arccos, exp. . .) :
L = N . s q r t (A)
Fonctions utiles
nV = N . l i n a l g . norm (V) # n o r m e
t r A = N . t r a c e (A) # t r a c e
detA = N . l i n a l g . d e t (A) # d e t e r m i n a n t
8/21
Juvigny–Fezzani
Formation Python Numpy et Scipy
Introduction
Les tableaux dans Numpy : la classe array
Scipy et ces fonctionnalitées
Déclaration et manipulation
La sous classe matrix
La sous classe poly1d
Opérations algébriques
Les dimensions des matrices doivent être compatibles
N . d o t (A , B) # p r o d u i t m a t r i c i e l
N . i n n e r (A , B) # = N . d o t ( A , B . t r a n s p o s e ( ) )
décomposition
vpA = N . l i n a g . e i g (A) # v a l e t v e c t p r o p r e s
svA = N . l i n a l g . s v d (A) # v a l s i n g u l i e r e s
résolution de systèmes
x = N . l i n a l g . s o l v e (A , b ) #A x=b
Transformée de Fourrier et produit de convolution
c = N . c o n v o l v e ( a , b , ’same ’ ) # c o n v 1 D
Ta = N . f f t . i f f t n (N . f f t . f f t n ( a ) ) # == a
Juvigny–Fezzani
Formation Python Numpy et Scipy
9/21
Introduction
Les tableaux dans Numpy : la classe array
Scipy et ces fonctionnalitées
Déclaration et manipulation
La sous classe matrix
La sous classe poly1d
Les différences entre matrix et array
matrix hérite de array
Déclaration
M1 = N . m a t r i x ( [ [ 3 . , 2 ] , [ 5 j , 2 3 . ] , [ . 5 , 6 ] ] )
M2 = S . mat ( ’3. 2;5j 23.;.5 6’ ) # == M1
raccourcis d’écriture
M. T # M . t r a n s p o s e ( )
M. H # M . c o n j ( ) . t r a n s p o s e ( )
M. I # N . l i n a l g . i n v (M)
opérations
S . m u l t i p l y (A , B) #A ∗ B
A∗B # N . d o t ( A , B )
S . power (M, 3 ) #M∗ ∗ 3
10/21
Juvigny–Fezzani
Formation Python Numpy et Scipy
Introduction
Les tableaux dans Numpy : la classe array
Scipy et ces fonctionnalitées
Déclaration et manipulation
La sous classe matrix
La sous classe poly1d
Gestion des polynômes unidimensionnels
Déclaration
P1 = N . p o l y 1 d ( [ . 5 , 2 , 6 , 4 ] )
outils
P1 ( . 5 ) # e v a l u a t i o n e n . 5
P1 . r # l i s t e d e s r a c i n e s
P1 . c # l i s t e d e s c o e f f i c i e n t s
P1 [ 2 ] # c o e f d e X ∗ ∗ 2
opérations
N . p o l y a d d ( P1 , P2 ) # a d d i t i o n
N . p o l y m u l ( P1 , P2 ) # m u l t i p l i c a t i o n
Q, R = N . p o l y d i v ( P1 , P2 ) # P1 = Q∗ P2 + R
N . p o l y d e r ( P1 , 2 ) # d e r i v e e s e c o n d e
11/21
Juvigny–Fezzani
Formation Python Numpy et Scipy
Introduction
Les tableaux dans Numpy : la classe array
Scipy et ces fonctionnalitées
Déclaration et manipulation
La sous classe matrix
La sous classe poly1d
Lecture et écriture de données
format ASCII
N . s a v e t x t ( ’/tmp/data.dat.gz ’ ,M)
M = N . l o a d t x t ( ’/tmp/data.dat.gz ’ )
Compression/décompression automatique en gzip
12/21
Juvigny–Fezzani
Formation Python Numpy et Scipy
Introduction
Les tableaux dans Numpy : la classe array
Scipy et ces fonctionnalitées
Introduction
Tour d’horizon
Qu’est ce que Scipy ?
Bibliothèque open-source d’algorithmes et outils
mathèmatiques pour Python
projet actif initiateur de plusieurs modules ou outils Python :
Numpy, Pylab, IPython...
interagit avec d’autre modules scientifiques Python : RPy,
Pytables, OpenOpt...
Quels domaines couvre Scipy ?
Transformée de Fourrier discrète
interpolation
optimisation
traitement de signal
algèbre linéaire (support des matrices creuses)
13/21
Juvigny–Fezzani
Formation Python Numpy et Scipy
Introduction
Les tableaux dans Numpy : la classe array
Scipy et ces fonctionnalitées
Introduction
Tour d’horizon
fonctions spéciales
directement accessibles
S . f a c t o r i a l (5) # n !
S . comb ( 5 , 3 ) # n ! / k ! ( n−k ) !
S . rand (2 ,2) # d i s t r i b u t i o n u n i f o r m e
S . lena () # t h e s o c a l l e d l e n a : −)
dans le sous module special
S . i n f o (S . s p e c i a l ) # pour l a l i s t e
S . s p e c i a l . gamma ( 2 . 5 ) # Gamma
S . s p e c i a l . jn (0. ,0.) # B e s s e l
complete
14/21
Juvigny–Fezzani
Formation Python Numpy et Scipy
Introduction
Les tableaux dans Numpy : la classe array
Scipy et ces fonctionnalitées
Introduction
Tour d’horizon
Intégration et EDO
from s c i p y import i n t e g r a t e
Utilse QUADPACK et ODEPACK
intégration à partir d’une fonction
q =i n t e g r a t e . quad ( lambda x : N . s i n ( x ) , 0 , 1 )
intégration à partir d’un sample
t =i n t e g r a t e . t r a p z (N . s i n (N . r [ : 1 : 1 0 0 j ] ) , dx =.01)
intégration d’EDO
S. info ( integrate . odeint )
15/21
Juvigny–Fezzani
Formation Python Numpy et Scipy
Introduction
Les tableaux dans Numpy : la classe array
Scipy et ces fonctionnalitées
Introduction
Tour d’horizon
Optimisation
from s c i p y import o p t i m i z e
algorithmes généraux d’optimization (simplex, quasi Newton,
moindre carrés)
S . i n f o ( optim ize . fmin )
minimisation dans un intervalle
x=o p t i m i z e . fminbound ( S . s p e c i a l . j 1 , 4 , 7 )
résolution d’équations non linéaires
x=o p t i m i z e . f s o l v e ( lambda x : x+2∗N . c o s ( x ) , . 5 )
point fixe
x=o p t i m i z e . f i x e d p o i n t ( lambda x : −2∗N . c o s ( x ) , . 5 )
16/21
Juvigny–Fezzani
Formation Python Numpy et Scipy
Introduction
Les tableaux dans Numpy : la classe array
Scipy et ces fonctionnalitées
Introduction
Tour d’horizon
Interpolation
Utilise la bibliothèque Fortran FITPACK
from s c i p y import i n t e r p o l a t e a s I
interpolation 1D
z1=I . i n t e r p 1 d ( x=N . a r a n g e ( 1 0 ) , y=N . a r a n g e ( 1 0 ) ∗ 2 )
z2=I . U n i v a r i a t e S p l i n e ( x=N . r [ : 1 0 ] , y=N . r [ : 1 0 ] ∗ 2 )
ynew = z1 (N . r [ : 9 : 1 0 0 j ] )
interpolation 2D
x , y=N . m g r i d [ − 1 : 1 : 2 0 j , −1:1:20 j ] # g r o s s i e r e
z=(x+y ) ∗N . exp ( −6.∗( x ∗ x+y ∗ y ) )
s 1=I . i n t e r p 2 d ( x , y , z ) # o u b i e n
s 2=I . B i v a r i a t e S p l i n e ( x , y , z )
xnew , ynew=N . m g r i d [ − 1 : 1 : 1 0 0 j , −1:1:100 j ] # f i n e
znew = s 2 ( xnew [ : , 0 ] , ynew [ 0 , : ] )
17/21
Juvigny–Fezzani
Formation Python Numpy et Scipy
Introduction
Les tableaux dans Numpy : la classe array
Scipy et ces fonctionnalitées
Introduction
Tour d’horizon
Algèbre linéaire
Décompositions
p , l , u = S . l i n a l g . l u (M)
q , r = S . l i n a l g . q r (M)
fonctions de matrices
eM = S . l i n a l g . expm (M) # e x p o n e n t i e l l e
rM = S . l i n a l g . s q r t m (M) # r a c i n e c a r r e e
solvers itératifs
x c g = S . l i n a l g . cg (A , b ) # A=A . H
x g m r e s = S . l i n a l g . gmres (A , b )
18/21
Juvigny–Fezzani
Formation Python Numpy et Scipy
Introduction
Les tableaux dans Numpy : la classe array
Scipy et ces fonctionnalitées
Introduction
Tour d’horizon
Matrices creuses
from s c i p y import s p a r s e
Différents formats de compression :
csc matrix : Compressed Sparse Column
csr matrix : Compressed Sparse Row
lil matrix : List of Lists
dok matrix : Dictionary of Keys
coo matrix : COOrdinate (IJV)
résolution de systèmes creux
Utilise UMFPACK ou SUPERLU
from s c i p y import l i n s o l v e
x = l i n s o l v e . s p s o l v e ( spA , b )
19/21
Juvigny–Fezzani
Formation Python Numpy et Scipy
Introduction
Les tableaux dans Numpy : la classe array
Scipy et ces fonctionnalitées
Introduction
Tour d’horizon
Traitement de signal
from s c i p y import s i g n a l , ndimage
lecture/sauvegarde array↔image
I = S . m i s c . i m r e a d ( ’toto.png ’ )
S . m i s c . i m s a v e ( ’/tmp/lena.pdf ’ , S . l e n a ( ) )
affichage de tableaux
I = S . m i s c . imshow ( S . l e n a ( ) . a s t y p e ( f l o a t ) )
appliquer un filtre
I = S. lena ()
gw = S . mat ( s i g n a l . g a u s s i a n ( 7 , 2 ) )
gw = gw . T∗gw
s i g n a l . f f t c o n v o l v e ( I , gw , ’same ’ ) # o u b i e n
ndimage . g a u s s i a n f i l t e r ( I , 2 )
20/21
Juvigny–Fezzani
Formation Python Numpy et Scipy
Introduction
Les tableaux dans Numpy : la classe array
Scipy et ces fonctionnalitées
Introduction
Tour d’horizon
Lecture/écriture de données dans des formats spécifiques
from s c i p y import i o
format Matrix market
i o . mmwrite ( ’/tmp/data.mtx ’ ,M)
a = i o . mmread ( ’data.mtx ’ )
format Matlab (version 5)
i o . sa vem at ( ’/tmp/data.mat ’ , { ’Apy ’ : A , ’bpy ’ : b } )
i o . l o a d m a t ( ’data.mat ’ )
21/21
Juvigny–Fezzani
Formation Python Numpy et Scipy
Téléchargement