Académique Documents
Professionnel Documents
Culture Documents
Le module numpy
La bibliothèque NumPy (http://www.numpy.org/) permet d’effectuer des calculs numériques avec
Python. Elle introduit une gestion facilitée des tableaux de nombres.
0.6198238392789901
Arrondissement
In [5]: x = 3.2598
np.around(x,2)
3.26
Out[5]:
Tableaux - numpy.array()
Les tableaux (en anglais, array) peuvent être créés avec numpy.array(). On utilise des crochets pour
délimiter les listes d’éléments dans les tableaux.
In [7]: a = [1, 2, 3, 4]
print(type(a))
<class 'list'>
In [10]: a[i][j]
a[i,j]
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
C:\Users\REVMIR~1\AppData\Local\Temp/ipykernel_8328/1103745718.py in <module>
----> 1 a[i][j]
2
3 a[i,j]
6
2
7
b = np.array([[1,2,3],[4,5,6],[9,8]])
print(b.shape)
(3,)
(3,)
C:\Users\REVMIR~1\AppData\Local\Temp/ipykernel_8328/2688639039.py:4: VisibleDeprecationWarn
ing: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or
-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do thi
s, you must specify 'dtype=object' when creating the ndarray.
b = np.array([[1,2,3],[4,5,6],[9,8]])
b = np.array([[1,2,3],[4,5,6]])
print(np.size(b))
3
6
Créé un tableau
In [20]: a = np.zeros((2,2)) # Create an array of all zeros
print(a)
[[0. 0.]
[0. 0.]]
[[1. 1.]
[1. 1.]
[1. 1.]]
In [ ]: b + 5
multiplication et addition
In [23]: b = np.ones((3,3))
c = b*9
print(c)
[[9. 9. 9.]
[9. 9. 9.]
[9. 9. 9.]]
In [24]: b = np.ones((3,3))
c = b+9
print(c)
Tableaux et slicing
In [26]: a = np.array([12, 25, 34, 56, 87])
print(a[1:3])
[25 34]
In [28]: print(a[0,1])
2
Out[28]:
In [30]: print(a[:,1:3])
[[2 3]
[5 6]]
In [31]: print(a[0,:])
[1 2 3]
In [32]: print(a[:,1])
[2 5]
In [ ]:
In [ ]:
In [ ]:
plt.plot(X,Y)
plt.show()
In [29]: plt.plot([1,1,2,2,1],[1,2,2,1,1])
plt.show()
Création d’une courbe - plot()
La fonction plt.plot() prend 2 liste qui represente des points, une liste des abscisses et une liste des
ordonnée. le fonction plt.plot() trace des lien entre chaques deux points successive.
In [33]: a = 0
b = 2*np.pi
n = 30
x = np.linspace(a, b, n)
y = []
for i in range(n):
y.append(np.cos(x[i]))
plt.plot(x, y)
plt.show() # affiche la figure à l'écran
In [34]: print(type(x))
<class 'numpy.ndarray'>
In [35]: x = np.linspace(a, b, n)
y = np.exp(x)
plt.plot(x, y)
plt.show() # affiche la figure à l'écran
In [37]: a = -5
b = 5
n = 3
x = np.linspace(a, b, n)
y = f1(x)
plt.plot(x, y)
plt.show() # affiche la figure à l'écran
In [38]: a = -5
b = 5
n = 20
x = np.linspace(a, b, n)
y1 = f1(x)
y2 = np.sin(x)
plt.title("Fonction cosinus")
plt.legend()
In [41]: a = -5
b = 5
n = 200
x = np.linspace(a, b, n)
y1 = f1(x)
y2 = np.sin(x)
plt.title("Fonction cosinus")
plt.xlim((-2,2)) #limiter la vue de la figure
plt.ylim((-2,2)) #limiter la vue de la figure
plt.legend()
In [45]: a = -10
b = 10
n = 200
x1 = np.linspace(a, 0, n//2)
y1 = 1/x1
x2 = np.linspace(0, b, n//2)
y2 = 1/x2
plt.plot(x1, y1)
plt.plot(x2, y2)
plt.title("Fonction cosinus")