Introduction à Numpy pour le Calcul Scientifique
Introduction à Numpy pour le Calcul Scientifique
Python
numpy ?
▶ Le module numpy est l’outil de base utilisé dans tous calculs scientifiques et donc
numériques en Python
▶ numpy fournit en particulier des objets de type vecteurs, matrices et plus
généralement tableaux à n dimensions
▶ numpy facilite et optimise† les opérations de stockage et de manipulation des
données numériques notamment lorsque la taille des tableaux devient
importante → array oriented computing
▶ Convention d’importation
▶ Convention d’importation
In [1]: [Link]?
1 Docstring:
2 array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)
3
4 Create an array.
5 ...
In [1]: [Link]?
1 Docstring:
2 array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)
3
4 Create an array.
5 ...
▶ Matrice 2×2
In [4]: M = [Link]([[0, 1], [2, 3]])
In [5]: M
Out[5]:
array([[0, 1],
[2, 3]])
▶ Matrice 2×2
In [4]: M = [Link]([[0, 1], [2, 3]])
In [5]: M
Out[5]:
array([[0, 1],
[2, 3]])
▶ Matrice 2×2
In [4]: M = [Link]([[0, 1], [2, 3]])
In [5]: M
Out[5]:
array([[0, 1],
[2, 3]])
▶ Les objets de type [Link] ≡ à une liste Python (ou liste de listes)
▶ Pourquoi ne pas simplement utiliser les listes Python pour les calculs au
lieu de créer un nouveau type de tableau ?
▶ Démonstration
In [3]: a = [Link](1000)
In [4]: %timeit a**2
100000 loops, best of 3: 12.7 us per loop
▶ Démonstration
In [3]: a = [Link](1000)
In [4]: %timeit a**2
100000 loops, best of 3: 12.7 us per loop
In [3]: [Link]
Out[3]: dtype('int64')
In [3]: [Link]
Out[3]: dtype('int64')
In [3]: [Link]
Out[3]: dtype('int64')
In [3]: [Link]
Out[3]: dtype('int64')
Dans la pratique, les valeurs d’un tableau sont rarement saisies une par une
▶ Fonction arange ≡ range
In [1]: [Link](10)
Out[1]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
▶ Fonctions linspace/logspace
Dans la pratique, les valeurs d’un tableau sont rarement saisies une par une
▶ Fonction arange ≡ range
In [1]: [Link](10)
Out[1]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
▶ Fonctions linspace/logspace
In [1]: [Link](10)
Out[1]: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
In [2]: [Link](shape=(3,3))
Out[2]:
array([[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]])
In [4]: [Link]((3,3))
Out[4]:
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
In [1]: [Link](3)
Out[1]: array([ 0.21401051, 0.19514481, 0.92647823])
In [3]: [Link](3)
Out[3]: array([-0.4829445 , -1.05459848, -1.30539831])
In [1]: [Link](1234)
In [1]: [Link](3)
Out[1]: array([ 0.21401051, 0.19514481, 0.92647823])
In [3]: [Link](3)
Out[3]: array([-0.4829445 , -1.05459848, -1.30539831])
In [1]: [Link](1234)
30
2
25
In [1]: import numpy as np
In [2]: import [Link] as plt
1
In [3]: %matplotlib 20
−2
5
−3
0 . .
0 5 10 15 20 25 30
▶ Comme pour les listes qui sont des objets mutables, il est possible d’assigner
une valeur en spécifiant l’indice
In [4]: x[0, 0] = 12
In [5]: x
Out[5]:
array([[12, 3, 6, 4],
[ 9, 8, 2, 0],
[ 0, 5, 5, 4]])
▶ Comme pour les listes qui sont des objets mutables, il est possible d’assigner
une valeur en spécifiant l’indice
In [4]: x[0, 0] = 12
In [5]: x
Out[5]:
array([[12, 3, 6, 4],
[ 9, 8, 2, 0],
[ 0, 5, 5, 4]])
In [1]: x
Out[1]:
array([[3, 3, 6, 4],
[9, 8, 2, 0],
[0, 5, 5, 4]])
In [2]: x[0]
Out[2]: array([[3, 3, 6, 4])
In [1]: x
Out[1]:
array([[3, 3, 6, 4],
[9, 8, 2, 0],
[0, 5, 5, 4]])
In [2]: x[0]
Out[2]: array([[3, 3, 6, 4])
In [1]: x
Out[1]:
array([[3, 3, 6, 4],
[9, 8, 2, 0],
[0, 5, 5, 4]])
In [4]: xx[0, 0] = 0
In [5]: x
Out[5]:
array([[0, 3, 6, 4],
[9, 8, 2, 0],
[0, 5, 5, 4]])
In [1]: x
Out[1]:
array([[3, 3, 6, 4],
[9, 8, 2, 0],
[0, 5, 5, 4]])
In [4]: xx[0, 0] = 0
In [5]: x
Out[5]:
array([[0, 3, 6, 4],
[9, 8, 2, 0],
[0, 5, 5, 4]])
▶ Pour réaliser une copie d’un sous espace vectoriel, on utilisera la méthode
copy()
In [1]: l = [1, 2, 3, 4]
In [2]: l+5
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-53-1cb32c2d071d> in <module>()
----> 1 l+5
In [1]: x = [Link](4)
In [2]: x
Out[2]: array([0, 1, 2, 3])
In [3]: x+5
Out[3]: array([5, 6, 7, 8])
In [4]: x-5
Out[4]: array([-5, -4, -3, -2])
In [5]: x*5
Out[5]: array([ 0, 5, 10, 15])
In [5]: x/5
Out[5]: array([ 0. , 0.2, 0.4, 0.6]))
In [1]: x = [Link](4)
In [2]: -x
Out[2]: array([0, -1, -2, -3])
In [3]: x**2
Out[3]: array([0, 1, 4, 9])
In [4]: x%2
Out[4]: array([0, 1, 0, 1])
In [2]: [Link](theta)
Out[2]: array([ 1.00000000e+00, 6.12323400e-17, -1.00000000e+00])
In [3]: [Link](theta)
Out[3]: array([ 0.00000000e+00, 1.00000000e+00, 1.22464680e-16])
In [4]: [Link](theta)
Out[4]: array([ 0.00000000e+00, 1.63312394e+16, -1.22464680e-16])
In [2]: [Link](theta)
Out[2]: array([ 1.00000000e+00, 6.12323400e-17, -1.00000000e+00])
In [3]: [Link](theta)
Out[3]: array([ 0.00000000e+00, 1.00000000e+00, 1.22464680e-16])
In [4]: [Link](theta)
Out[4]: array([ 0.00000000e+00, 1.63312394e+16, -1.22464680e-16])
In [2]: [Link](theta)
Out[2]: array([ 1.00000000e+00, 6.12323400e-17, -1.00000000e+00])
In [3]: [Link](theta)
Out[3]: array([ 0.00000000e+00, 1.00000000e+00, 1.22464680e-16])
In [4]: [Link](theta)
Out[4]: array([ 0.00000000e+00, 1.63312394e+16, -1.22464680e-16])
In [1]: x = [Link](100)
In [2]: sum(x)
Out[2]: 50.394482884150314
In [3]: [Link](x)
Out[3]: 50.394482884150314
In [1]: x = [Link](100)
In [2]: sum(x)
Out[2]: 50.394482884150314
In [3]: [Link](x)
Out[3]: 50.394482884150314
Fonction Description
[Link] Somme des éléments
[Link] Produit des éléments
[Link] Valeur moyenne
[Link] Standard déviation
[Link] Variance
[Link] Valeur minimale
[Link] Valeur maximale
[Link] Indice de la valeur minimale
[Link] Indice de la valeur maximale
[Link] Valeur médiane
[Link] Quantiles
▶ Multiplication de matrices
In [1]: M = [Link](shape=(3,3))
In [2]: M
Out[2]:
array([[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]])
In [3]: M*M
Out[3]:
array([[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]])
In [4]: [Link](M)
Out[4]:
array([[ 3., 3., 3.],
[ 3., 3., 3.],
[ 3., 3., 3.]])
▶ Transposition de matrices
In [3]: [Link]()
Out[3]:
array([[4, 2, 1],
[1, 3, 0],
[0, 0, 2]])
In [1]: v = [Link](4)
In [2]: v
Out[2]: array([0, 1, 2, 3])
0 . .
0 1 2 3 4 5
.
numpy : librairie pour le calcul scientifique 28
Opérations logiques
In [2]: x < 3
Out[2]: array([ True, True, False, False, False], dtype=bool)
In [3]: x == 3
Out[3]: array([False, False, True, False, False], dtype=bool)
In [4]: (x * 2) == (x**2)
Out[4]: array([False, True, False, False, False], dtype=bool)
In [3]: x < 5
Out[3]: array([False, False, False, True, True, False, False, True, True, False], dtype=bool)
In [4]: x = [Link](1000)
In [5]: y = [Link](1000)
0.6
In [6]: [Link](x, y, alpha=0.3)
In [7]: [Link]("scaled"); [Link]([0, 1, 0, 1])
0.4
In [8]: mask = (x*y > 0.5)
In [9]: [Link](x[mask], y[mask], alpha=0.6,
edgecolors="orange", c="none", 0.2
s=200)
0.0
0.0 0.2 0.4 0.6 0.8 1.0
[Link](3)+5
0 1 2 + 5 5 5 = 5 6 7
[Link]((3,3))+[Link](3)
1 1 1 0 1 2 1 2 3
1 1 1 + 0 1 2 = 1 2 3
1 1 1 0 1 2 1 2 3
[Link](3).reshape((3,1))+[Link](3)
0 0 0 0 1 2 0 1 2
1 1 1 + 0 1 2 = 1 2 3
2 2 2 0 1 2 2 3 4
..
† pour plus de détails, cf. discussion