-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathdescente.py
More file actions
138 lines (109 loc) · 3.97 KB
/
descente.py
File metadata and controls
138 lines (109 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/python3
# Descente de gradient
import numpy as np
import matplotlib.pyplot as plt
def descente(f, grad_f, X0, delta=0.1, nmax=10):
liste_X = [X0]
liste_grad = []
X = X0
for i in range(nmax):
gradient = grad_f(*X)
X = X - delta*gradient
liste_X.append(X)
liste_grad.append(gradient)
return liste_X, liste_grad
def affiche_descente(f, grad_f, X0, delta=0.1, nmax=10):
liste_X, liste_grad = descente(f, grad_f, X0, delta=delta, nmax=nmax)
print("Delta",delta)
print("Nombre d'itérations", nmax)
print("Point initial", X0)
for i in range(len(liste_X)-1): # flèches
print("--- Etape",i)
print("Point :", *liste_X[i])
print("Gradient ", *liste_grad[i])
print("Valeur de la fonction ", f(*liste_X[i]))
print("Dernier point :", *liste_X[-1])
print("Dernière valeur de la fonction ", f(*liste_X[-1]))
return
def graphique_descente_2var_2d(f, grad_f, X0, delta=0.1, nmax=10, zone = (-3.0,3.0,-3.0,3.0)):
# 1. Points et gradients
liste_X, liste_grad = descente(f, grad_f, X0, delta=delta, nmax=nmax)
for x, y in liste_X: # points
plt.scatter(x, y, color='red')
for i in range(len(liste_X)-1): # flèches
plt.arrow(*liste_X[i], *(-delta*liste_grad[i]), linewidth=2, color='orange', length_includes_head=True, head_width=0.05, head_length=0.1)
# 2. lignes de niveaux
xmin, xmax, ymin, ymax = zone
num = 40
VX = np.linspace(xmin, xmax, num)
VY = np.linspace(ymin, ymax, num)
X, Y = np.meshgrid(VX, VY)
Z = f(X, Y)
# 3. affichage
# pour intro
# niveaux = [2.28, 3.56, 8.5]
plt.contour(X, Y, Z, 30, colors='black')
plt.scatter(0,0, color='blue') # minimum
# plt.colorbar();
plt.axis('equal')
plt.tight_layout()
plt.savefig('descente_intro_06.png')
plt.show()
return
def graphique_regression(f, grad_f, X0, points, delta=0.1, nmax=10, zone = (-3.0,3.0,-3.0,3.0)):
# 1. Droites
liste_X, liste_grad = descente(f, grad_f, X0, delta=delta, nmax=nmax)
num = 40
xmin, xmax, ymin, ymax = zone
VX = np.linspace(xmin, xmax, num)
# Droite initiale
a, b = X0
VY = a*VX + b
plt.plot(VX, VY, linewidth=2, color='red')
plt.text(6.5, a*6+b, 'k = 0')
# Droite finale
a, b = 0.78, -2.46
VY = a*VX + b
plt.plot(VX, VY, linewidth=2, color='blue')
# plt.text(6.5, a*6+b, 'y = 0.5 x + 3')
for i in range(1, len(liste_X)):
a, b = liste_X[i]
VY = a*VX + b
plt.plot(VX, VY, linewidth=2, color='orange')
plt.text(6.5, a*6+b, 'k = '+str(i))
for x,y in points: # points
plt.scatter(x, y, color='black')
# 3. affichage
plt.axis('equal')
plt.tight_layout()
plt.savefig('descente.png')
plt.show()
return
def graphique_descente_1var(f, grad_f, X0, delta=0.1, nmax=10):
xmin, xmax = -1.5, 2.5
num = 100
VX = np.linspace(xmin, xmax, num)
#1 Graphe de la fonctions
VY = f(VX)
plt.plot(VX,VY,color='blue')
# 2. Points et gradients sur l'axe
liste_X, liste_grad = descente(f, grad_f, X0, delta=delta, nmax=nmax)
for x in liste_X: # points
plt.scatter(x, 0, color='red')
for i in range(len(liste_X)-1): # flèches
plt.arrow(liste_X[i],0, *(-delta*liste_grad[i]),0, linewidth=2, color='orange', length_includes_head=True, head_width=0.05, head_length=0.1)
# 3. Points et gradients sur le graphe
for x in liste_X: # points
plt.scatter(x, f(x), color='red')
for i in range(len(liste_X)-1): # flèches
x = liste_X[i]
xx = liste_X[i+1]
plt.arrow(*x, *f(x), *(xx-x), *(f(xx)-f(x)), linewidth=2, color='orange', length_includes_head=True, head_width=0.05, head_length=0.1)
# 4. affichage
plt.scatter(0,0, color='blue') # minimum
# plt.colorbar();
# plt.axis('equal')
plt.tight_layout()
# plt.savefig('descente_une_var_10.png')
plt.show()
return