Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
teaching:progappchim:numpy_simple [2015/03/30 09:48] – [Autres fonctions] villersd | teaching:progappchim:numpy_simple [2023/03/07 13:05] (Version actuelle) – villersd | ||
---|---|---|---|
Ligne 2: | Ligne 2: | ||
NumPy est une extension du langage de programmation Python, destinée à manipuler des matrices ou tableaux multidimensionnels ainsi que des fonctions mathématiques opérant sur ces tableaux. | NumPy est une extension du langage de programmation Python, destinée à manipuler des matrices ou tableaux multidimensionnels ainsi que des fonctions mathématiques opérant sur ces tableaux. | ||
- | Numpy permet la manipulations des vecteurs, matrices et polynômes. | + | Chaque élément d'un tableau numpy occupe un nombre fixe d' |
+ | |||
+ | Numpy permet la manipulations des vecteurs, matrices et polynômes. | ||
===== Directive d' | ===== Directive d' | ||
Ligne 9: | Ligne 12: | ||
===== Tableaux numériques ===== | ===== Tableaux numériques ===== | ||
On convertit facilement des listes Python en tableau numpy. Essayez ceci : | On convertit facilement des listes Python en tableau numpy. Essayez ceci : | ||
- | <sxh python;> | + | <code python> |
import numpy as np | import numpy as np | ||
- | a=np.array([[1, | + | a = np.array([[1, |
- | print a | + | print(a) |
- | print a.dtype | + | print(a.dtype) |
- | </sxh> | + | </code> |
Sortie : | Sortie : | ||
< | < | ||
Ligne 25: | Ligne 28: | ||
Les fonctions arange et shape sont bien pratiques pour générer des nombres en séquences et réarranger des listes de nombres. La fonction linspace est utile parce qu' | Les fonctions arange et shape sont bien pratiques pour générer des nombres en séquences et réarranger des listes de nombres. La fonction linspace est utile parce qu' | ||
- | Vous pouvez consulter [[http://wiki.scipy.org/Numpy_Example_List|cette page]] pour consulter d' | + | Vous pouvez consulter [[https://docs.scipy.org/doc/numpy/|cette page]] pour consulter d' |
</ | </ | ||
- | <sxh python; title : arrays_01.py> | + | <code python arrays_01.py> |
#! / | #! / | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
Ligne 35: | Ligne 38: | ||
import numpy as np | import numpy as np | ||
- | a=np.array(((1, | + | a = np.array(((1, |
# afficher a, le nombre de dimensions, les dimensions, le type de donnée | # afficher a, le nombre de dimensions, les dimensions, le type de donnée | ||
- | print a, a.ndim, a.shape, a.dtype | + | print(a, a.ndim, a.shape, a.dtype) |
# avec des " | # avec des " | ||
- | b= np.array( | + | b = np.array([ |
- | | + | [1.1, 2.2, 3.3, 4.4], |
[5.5, 6.6, 7.7, 8.8], | [5.5, 6.6, 7.7, 8.8], | ||
- | [9.9, 0.2, 1.3, 2.4]]) | + | [9.9, 0.2, 1.3, 2.4], |
- | print b, b.ndim, b.shape, b.dtype | + | |
+ | print(b, b.ndim, b.shape, b.dtype) | ||
# un tableau de zéros | # un tableau de zéros | ||
- | c=np.zeros((4, | + | c = np.zeros((4, |
- | print c, c.ndim, c.shape, c.dtype | + | print(c, c.ndim, c.shape, c.dtype) |
# un tableau tridimensionnel de 1 " | # un tableau tridimensionnel de 1 " | ||
- | d=np.ones((2, | + | d = np.ones((2, |
- | print d, d.ndim, d.shape, d.dtype | + | print(d, d.ndim, d.shape, d.dtype) |
# un tableau avec arange, et ensuite reshape | # un tableau avec arange, et ensuite reshape | ||
- | e1= np.arange(1, | + | e1 = np.arange(1, |
- | e=np.reshape(e1, | + | e = np.reshape(e1, |
- | print e, e.ndim, e.shape, e.dtype | + | print(e, e.ndim, e.shape, e.dtype) |
- | f=np.random.rand(3, | + | f = np.random.rand(3, |
- | print f, f.ndim, f.shape, f.dtype | + | print(f, f.ndim, f.shape, f.dtype) |
# utilisation de linspace pour imposer le nombre d' | # utilisation de linspace pour imposer le nombre d' | ||
- | g=np.linspace(0., | + | g = np.linspace(0., |
- | print g, g.ndim, g.shape, g.dtype | + | print(g, g.ndim, g.shape, g.dtype) |
- | </sxh> | + | </code> |
Quelques manipulations élémentaires : | Quelques manipulations élémentaires : | ||
- | <sxh python; title : arrays_02.py> | + | <code python arrays_02.py> |
#! / | #! / | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
Ligne 70: | Ligne 74: | ||
import numpy as np | import numpy as np | ||
- | a=np.array([[1, | + | a = np.array([[1, |
- | b=np.array([[1, | + | b = np.array([[1, |
- | c=a+b # addition terme à terme | + | c = a + b # addition terme à terme |
- | print c, c.ndim, c.shape, c.dtype | + | print(c, c.ndim, c.shape, c.dtype) |
- | d=a*b # multiplication terme à terme | + | d =a * b # multiplication terme à terme |
- | print d, d.ndim, d.shape, d.dtype | + | print(d, d.ndim, d.shape, d.dtype) |
- | e=np.dot(a, | + | e = np.dot(a,b) # multiplication matricielle |
- | print e, e.ndim, e.shape, e.dtype | + | print(e, e.ndim, e.shape, e.dtype) |
- | f=np.sin(np.pi*0.5*a) # fonction mathématique et adaptation automatique du type | + | f = np.sin(np.pi*0.5*a) # fonction mathématique et adaptation automatique du type |
- | print f, f.ndim, f.shape, f.dtype | + | print(f, f.ndim, f.shape, f.dtype) |
- | g=np.transpose(a) # transposition | + | g = np.transpose(a) # transposition |
- | print g, g.ndim, g.shape, g.dtype | + | print(g, g.ndim, g.shape, g.dtype) |
- | print np.sum(a), | + | print(np.sum(a), |
- | </sxh> | + | </code> |
==== Fonctions mathématiques principales : ==== | ==== Fonctions mathématiques principales : ==== | ||
Ligne 102: | Ligne 106: | ||
===== Algèbre linéaire ===== | ===== Algèbre linéaire ===== | ||
- | <sxh python; title : simple_linear_system.py> | + | <code python simple_linear_system.py> |
#! / | #! / | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
""" | """ | ||
Solve a system of simultaneous equation in two variables of the form | Solve a system of simultaneous equation in two variables of the form | ||
- | 2*x + 7*y=17. | + | 2 * x + 7 * y = 17. |
- | 3*x - 5*y=-21. | + | 3 * x - 5 * y = -21. |
reference : http:// | reference : http:// | ||
Ligne 117: | Ligne 121: | ||
a = np.array([[2., | a = np.array([[2., | ||
b = np.array([[17.], | b = np.array([[17.], | ||
- | print np.linalg.solve(a, | + | print(np.linalg.solve(a, |
- | </sxh> | + | </code> |
Quelques possibilités supplémentaires : | Quelques possibilités supplémentaires : | ||
- | <sxh python; title : arrays_linalg_03.py> | + | <code python arrays_linalg_03.py> |
#! / | #! / | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
Ligne 129: | Ligne 133: | ||
import numpy as np | import numpy as np | ||
- | a=np.array([[1, | + | a = np.array([[1, |
- | print a, a.ndim, a.shape, a.dtype | + | print(a, a.ndim, a.shape, a.dtype) |
- | b=np.linalg.inv(a) | + | b = np.linalg.inv(a) |
- | print b, b.ndim, b.shape, b.dtype | + | print(b, b.ndim, b.shape, b.dtype) |
unit = np.eye(2) | unit = np.eye(2) | ||
- | print unit, unit.ndim, unit.shape, unit.dtype | + | print(unit, unit.ndim, unit.shape, unit.dtype) |
v = np.array([[10.], | v = np.array([[10.], | ||
- | x1=np.dot(b, | + | x1 = np.dot(b, |
- | x2=np.linalg.solve(a, | + | x2 = np.linalg.solve(a, |
# des inconnues a et de coefficients indépendants b | # des inconnues a et de coefficients indépendants b | ||
# les deux techniques donnent évidemment le même résultat ! | # les deux techniques donnent évidemment le même résultat ! | ||
- | print x1, x1.ndim, x1.shape, x1.dtype | + | print(x1, x1.ndim, x1.shape, x1.dtype) |
- | print x2, x2.ndim, x2.shape, x2.dtype | + | print(x2, x2.ndim, x2.shape, x2.dtype) |
# valeurs propres et vecteurs propres de matrices : | # valeurs propres et vecteurs propres de matrices : | ||
- | d=np.array([[1, | + | d = np.array([[1, |
- | print np.linalg.eig(d) | + | print(np.linalg.eig(d)) |
- | </sxh> | + | </code> |
Numpy dispose aussi d'une classe particulière de " | Numpy dispose aussi d'une classe particulière de " | ||
Ligne 155: | Ligne 159: | ||
===== Statistiques élémentaires ===== | ===== Statistiques élémentaires ===== | ||
- | <sxh python; title : arrays_stats_elem_04.py> | + | <code python arrays_stats_elem_04.py> |
#! / | #! / | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
Ligne 163: | Ligne 167: | ||
import numpy as np | import numpy as np | ||
- | a=np.array([1., | + | a = np.array([1., |
- | print a, a.ndim, a.shape, a.dtype | + | print(a, a.ndim, a.shape, a.dtype) |
- | print " | + | print(" |
- | print " | + | print(" |
- | print " | + | print(" |
- | print " | + | print(" |
- | </sxh> | + | </code> |
+ | ==== Références complémentaires ==== | ||
+ | * [[http:// | ||
===== Itérations sur les tableaux ===== | ===== Itérations sur les tableaux ===== | ||
- | <sxh python; title : arrays_iteration_05.py> | + | <code python arrays_iteration_05.py> |
#! / | #! / | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
Ligne 180: | Ligne 186: | ||
import numpy as np | import numpy as np | ||
- | a=np.array([1., | + | a = np.array([1., |
for x in a: | for x in a: | ||
- | print x | + | print(x) |
# l' | # l' | ||
- | b= np.array( | + | b = np.array([ |
- | | + | [1.1, 2.2, 3.3, 4.4], |
[5.5, 6.6, 7.7, 8.8], | [5.5, 6.6, 7.7, 8.8], | ||
- | [9.9, 0.2, 1.3, 2.4]]) | + | [9.9, 0.2, 1.3, 2.4], |
+ | | ||
for x in b: | for x in b: | ||
- | print x | + | print(x) |
for y in x: | for y in x: | ||
- | print y,", ", | + | print(y,", ",) |
- | </sxh> | + | </code> |
===== Manipulation de polynômes ===== | ===== Manipulation de polynômes ===== | ||
Ligne 199: | Ligne 206: | ||
**poly1d & polynomial ordonnent les coefficients en sens inverses !!!**</ | **poly1d & polynomial ordonnent les coefficients en sens inverses !!!**</ | ||
- | <sxh python; title : arrays_polynomes_06.py> | + | <code python arrays_polynomes_06.py> |
#! / | #! / | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
Ligne 206: | Ligne 213: | ||
""" | """ | ||
import numpy as np | import numpy as np | ||
+ | from numpy.polynomial import Polynomial as P | ||
- | # les coefficients du polynômes sont donnés par ordre décroissance des degrés dans poly1d | + | # les coefficients du polynômes sont donnés par ordre décroissance des dégrés |
- | a=np.poly1d([1.,2.,3.,4.]) # = x³ + 2x² + 3x +4 | + | a = P([4., 3., 2., 1.]) # = x³ + 2x² + 3x + 4 |
- | print " | + | print(" |
# les coefficients de a : | # les coefficients de a : | ||
- | print " | + | print(" |
# les racines de a : | # les racines de a : | ||
- | print " | + | print(" |
# l' | # l' | ||
- | print "ordre : ",a.order | + | print("ordre : ", a.degree()) |
# évaluations sur un vecteur | # évaluations sur un vecteur | ||
- | x=np.linspace(0, | + | x = np.linspace(0, |
- | print "x = ",x | + | print("x = ", x) |
- | print " | + | print(" |
# dérivation | # dérivation | ||
- | print " | + | print(" |
+ | print(" | ||
+ | print(" | ||
+ | print(" | ||
# intégration | # intégration | ||
- | print " | + | print(" |
# création d'un polynôme par ses racines | # création d'un polynôme par ses racines | ||
- | b=a.roots | + | b = a.roots() |
- | c=np.poly1d(b,True) | + | c = P.fromroots(b) |
- | print " | + | print(" |
+ | # | ||
# fitting polynomial | # fitting polynomial | ||
- | xd=np.array([0., | + | # |
- | yd1=np.array([0.05, | + | # utilisation de poly1d (ancienne librairie) |
- | pfit=np.poly1d(np.polyfit(xd, | + | # |
- | print "fit d'une parabole (polynôme d' | + | # numpy.polyfit (poly1d) : |
- | print xd | + | # https:// |
- | print yd1 | + | # https:// |
- | print " | + | # |
- | </sxh> | + | xd = np.array([0., |
+ | yd = np.array([0.05, | ||
+ | pfit = np.poly1d(np.polyfit(xd, | ||
+ | print("fit d'une parabole (polynôme d' | ||
+ | print(xd) | ||
+ | print(yd) | ||
+ | print(" | ||
+ | # | ||
+ | # " | ||
+ | # in the opposite order of that for np.polyfit and np.polyval" | ||
+ | # → https:// | ||
+ | # | ||
+ | ################################################## | ||
+ | # Ajouter les fits utilisant numpy.polynomial... # | ||
+ | ################################################## | ||
+ | # | ||
+ | # numpy.polynomial.polynomial.Polynomial.fit : | ||
+ | # https:// | ||
+ | # https:// | ||
+ | # https:// | ||
+ | # https:// | ||
+ | # | ||
+ | # numpy.polynomial.polynomial.polyfit : | ||
+ | # https:// | ||
+ | # | ||
+ | </code> | ||
Autres fonctions : voir [[http:// | Autres fonctions : voir [[http:// | ||
Ligne 246: | Ligne 283: | ||
Le module de [[http:// | Le module de [[http:// | ||
- | <sxh python; title : fonctions-FT-04.py> | + | <code python fonctions-FT-04.py> |
# | # | ||
#-*- coding: utf-8 -*- | #-*- coding: utf-8 -*- | ||
Ligne 277: | Ligne 314: | ||
x = np.arange(0.0, | x = np.arange(0.0, | ||
y1 = f1(x) | y1 = f1(x) | ||
- | z1=fft.fft(y1) | + | z1 = fft.fft(y1) |
- | w1 = np.abs(z1[: | + | w1 = np.abs(z1[: |
y2 = f2(x) | y2 = f2(x) | ||
- | z2=fft.fft(y2) | + | z2 = fft.fft(y2) |
- | w2 = np.abs(z2[: | + | w2 = np.abs(z2[: |
y3 = f3(x) | y3 = f3(x) | ||
- | z3=fft.fft(y3) | + | z3 = fft.fft(y3) |
- | w3 = np.abs(z3[: | + | w3 = np.abs(z3[: |
# doc subplot : http:// | # doc subplot : http:// | ||
Ligne 321: | Ligne 358: | ||
plt.savefig(' | plt.savefig(' | ||
plt.show() | plt.show() | ||
- | </sxh> | + | </code> |
Figure obtenue : | Figure obtenue : | ||
Ligne 327: | Ligne 364: | ||
{{ : | {{ : | ||
+ | ===== Avantages de numpy ===== | ||
+ | L' | ||
+ | |||
+ | {{gh> | ||
===== Références ===== | ===== Références ===== | ||
Ligne 334: | Ligne 375: | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
- | * [[http:// | + | * [[http:// |
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | |||
+ | ==== Références avancées ==== | ||
+ | * [[https:// | ||
+ | |||