Cours Python SRI outils scientifiques
October 4, 2016
1 Programmation scientifique en python
Python devient de plus en plus une alternative `a Matlab.
Pourquoi ?
gratuit
“vrai” langage de programmation
b´en´eficie de la communaut´e open source: en progression constante
unifie de nombreuses biblioth`eques existant dans diff´erents langages (alg`ebre, analyse, statistiques,
traitement signal, etc)
librairie python pour “emballer” le tout: scipy, http://www.scipy.org/
On verra ici :
le calcul matriciel avec numpy (base de nombreux autres outils)
une introduction `a quelques outils pertinents
traitement signal
traitement d’image
apprentissage automatique
graphes
robotique
1.1 ## Programmation vectorielle: numerical python (numpy)
introduction `a la programmation scientifique et l’utilisation de vecteurs/matrices
d´efinitions, cr´eations
index/r´ef´erences
quelques op´erations / exemples de calcul vectoris´e
r`egles de “broadcasting” (composition)
op´erations + complexes, convolution (ex: automates cellulaires, fractales)
In [2]: import numpy as np
a=np.random.random(1000)
print type(a)
<type ’numpy.ndarray’>
In [7]: print a[:10]
print a.min(),a.max(),a.mean()
[ 0.30925646 0.86760964 0.48519242 0.77411482 0.38657677 0.50936001
0.58858523 0.06037206 0.26884881 0.24828936]
0.00125452472117 0.999758911835 0.49546800591
1
1.2 Calcul vectoris´e
In [8]: from math import cos
print cos(a)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-aaa6d586df86> in <module>()
1 from math import cos
----> 2 print cos(a)
TypeError: only length-1 arrays can be converted to Python scalars
In [5]: from numpy import cos
c=cos(a)
print c[:10]
[ 0.97542417 0.9559976 0.889291 0.98941373 0.8121354 0.93883746
0.7051168 0.7920507 0.9395283 0.6542547 ]
In [10]: (a+a)[:10]
Out[10]: array([ 0.61851293, 1.73521928, 0.97038485, 1.54822963, 0.77315354,
1.01872002, 1.17717045, 0.12074412, 0.53769763, 0.49657872])
1.2.1 Comparaison
In [13]: b=list(a)
print b[:10]
[0.3092564639249985, 0.86760964130719376, 0.48519242363955573, 0.77411481507324209, 0.38657676754846271, 0.50936000958515759, 0.58858522711366779, 0.060372059305616954, 0.26884881489477819, 0.24828936212184904]
In [11]: %timeit a**2
The slowest run took 16.04 times longer than the fastest. This could mean that an intermediate result is being cached
1000000 loops, best of 3: 1.93 µs per loop
In [10]: %timeit [x**2 for x in b]
1000 loops, best of 3: 274 µs per loop
In [11]: %%timeit
s=0
for x in b:
s = s + x**2
1000 loops, best of 3: 320 µs per loop
2
1.3 Indexation et dimensions
In [14]: c=np.array(b)
print type(c)
print c[9]
c.shape
<type ’numpy.ndarray’>
0.248289362122
Out[14]: (1000,)
In [18]: c=c.reshape(10,100)
print c[0,9]
print
print c[:,2:4]
0.857599610473
[[ 0.47500376 0.14563654]
[ 0.20858125 0.00736357]
[ 0.48992152 0.70656261]
[ 0.2826899 0.1070452 ]
[ 0.43615071 0.99472248]
[ 0.03902043 0.75047057]
[ 0.87706503 0.72165283]
[ 0.78677078 0.88379719]
[ 0.0239854 0.67950247]
[ 0.62720625 0.5302494 ]]
1.4 Cr´eation
In [18]: c=np.zeros(shape=(3,4))
print c.shape
print c
c.ravel()
(3, 4)
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
Out[18]: array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
In [19]: c=np.arange(10)
print c
[0123456789]
In [20]: c=np.arange(0.,1.,0.05)
c
Out[20]: array([ 0. , 0.05, 0.1 , 0.15, 0.2 , 0.25, 0.3 , 0.35, 0.4 ,
0.45, 0.5 , 0.55, 0.6 , 0.65, 0.7 , 0.75, 0.8 , 0.85,
0.9 , 0.95])
In [21]: c=np.linspace(0,1.,21)
c
3
Out[21]: array([ 0. , 0.05, 0.1 , 0.15, 0.2 , 0.25, 0.3 , 0.35, 0.4 ,
0.45, 0.5 , 0.55, 0.6 , 0.65, 0.7 , 0.75, 0.8 , 0.85,
0.9 , 0.95, 1. ])
1.5 Cr´eation (suite)
In [94]: np.zeros((2,2))
Out[94]: array([[ 0., 0.],
[ 0., 0.]])
In [95]: np.ones((2,2))
Out[95]: array([[ 1., 1.],
[ 1., 1.]])
In [24]: np.eye(3)
Out[24]: array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
1.6 R´ef´erences
attention aux r´ef´erences ->diff´erent des listes
In [29]: c[1]= 0
print(c)
a=c[1:-1]
c[1]= -1
print a[:10]
[ 0. 0. 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55
0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1. ]
[-1. 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 ]
In [32]: c[0]= 0
a=np.zeros(c.shape)
# ou
#a[:] = c
a[...]=c
c[0]= 2
print a[:10]
[ 0. -1. 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45]
1.7 Index Conditionnels
In [34]: ia1 =np.argwhere(a>0.55)
ia2 =np.argwhere((a>.55)|(a<0.))
print ia2
[[ 1]
[12]
[13]
[14]
[15]
4
[16]
[17]
[18]
[19]
[20]]
In [47]: print a[ia1].ravel()
print a[ia2].ravel()
[ 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1. ]
[-1. 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1. ]
In [35]: print a[(a<0.)|(a>0.55)]
[ 0.8 0.85 0.9 0.95 1. ]
In [106]: %pylab inline
Populating the interactive namespace from numpy and matplotlib
1.8 Exemples d’utilisation
In [37]: from numpy import sin
%pylab inline
x=np.linspace(-20,20,1000)
a=sin(x/2.)+np.random.normal(scale=0.2,size=1000)
pylab.plot(x,a)
Populating the interactive namespace from numpy and matplotlib
WARNING: pylab import has clobbered these variables: [’cos’]
‘%matplotlib‘ prevents importing * from pylab and numpy
Out[37]: [<matplotlib.lines.Line2D at 0x10621e050>]
5
1 / 14 100%
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans linterface ou les textes ? Ou savez-vous comment améliorer linterface utilisateur de StudyLib ? Nhésitez pas à envoyer vos suggestions. Cest très important pour nous !