-
Notifications
You must be signed in to change notification settings - Fork 0
/
plt_quad_logistic.py
328 lines (283 loc) · 12.7 KB
/
plt_quad_logistic.py
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
"""
plt_quad_logistic.py
interactive plot and supporting routines showing logistic regression
"""
import time
from matplotlib import cm
import matplotlib.colors as colors
from matplotlib.gridspec import GridSpec
from matplotlib.widgets import Button
from matplotlib.patches import FancyArrowPatch
from ipywidgets import Output
from lab_utils_common import np, plt, dlc, dlcolors, sigmoid, compute_cost_matrix, gradient_descent
# for debug
#output = Output() # sends hidden error messages to display when using widgets
#display(output)
class plt_quad_logistic:
''' plots a quad plot showing logistic regression '''
# pylint: disable=too-many-instance-attributes
# pylint: disable=too-many-locals
# pylint: disable=missing-function-docstring
# pylint: disable=attribute-defined-outside-init
def __init__(self, x_train,y_train, w_range, b_range):
# setup figure
fig = plt.figure( figsize=(10,6))
fig.canvas.toolbar_visible = False
fig.canvas.header_visible = False
fig.canvas.footer_visible = False
fig.set_facecolor('#ffffff') #white
gs = GridSpec(2, 2, figure=fig)
ax0 = fig.add_subplot(gs[0, 0])
ax1 = fig.add_subplot(gs[0, 1])
ax2 = fig.add_subplot(gs[1, 0], projection='3d')
ax3 = fig.add_subplot(gs[1,1])
pos = ax3.get_position().get_points() ##[[lb_x,lb_y], [rt_x, rt_y]]
h = 0.05
width = 0.2
axcalc = plt.axes([pos[1,0]-width, pos[1,1]-h, width, h]) #lx,by,w,h
ax = np.array([ax0, ax1, ax2, ax3, axcalc])
self.fig = fig
self.ax = ax
self.x_train = x_train
self.y_train = y_train
self.w = 0. #initial point, non-array
self.b = 0.
# initialize subplots
self.dplot = data_plot(ax[0], x_train, y_train, self.w, self.b)
self.con_plot = contour_and_surface_plot(ax[1], ax[2], x_train, y_train, w_range, b_range, self.w, self.b)
self.cplot = cost_plot(ax[3])
# setup events
self.cid = fig.canvas.mpl_connect('button_press_event', self.click_contour)
self.bcalc = Button(axcalc, 'Run Gradient Descent \nfrom current w,b (click)', color=dlc["dlorange"])
self.bcalc.on_clicked(self.calc_logistic)
# @output.capture() # debug
def click_contour(self, event):
''' called when click in contour '''
if event.inaxes == self.ax[1]: #contour plot
self.w = event.xdata
self.b = event.ydata
self.cplot.re_init()
self.dplot.update(self.w, self.b)
self.con_plot.update_contour_wb_lines(self.w, self.b)
self.con_plot.path.re_init(self.w, self.b)
self.fig.canvas.draw()
# @output.capture() # debug
def calc_logistic(self, event):
''' called on run gradient event '''
for it in [1, 8,16,32,64,128,256,512,1024,2048,4096]:
w, self.b, J_hist = gradient_descent(self.x_train.reshape(-1,1), self.y_train.reshape(-1,1),
np.array(self.w).reshape(-1,1), self.b, 0.1, it,
logistic=True, lambda_=0, verbose=False)
self.w = w[0,0]
self.dplot.update(self.w, self.b)
self.con_plot.update_contour_wb_lines(self.w, self.b)
self.con_plot.path.add_path_item(self.w,self.b)
self.cplot.add_cost(J_hist)
time.sleep(0.3)
self.fig.canvas.draw()
class data_plot:
''' handles data plot '''
# pylint: disable=missing-function-docstring
# pylint: disable=attribute-defined-outside-init
def __init__(self, ax, x_train, y_train, w, b):
self.ax = ax
self.x_train = x_train
self.y_train = y_train
self.m = x_train.shape[0]
self.w = w
self.b = b
self.plt_tumor_data()
self.draw_logistic_lines(firsttime=True)
self.mk_cost_lines(firsttime=True)
self.ax.autoscale(enable=False) # leave plot scales the same after initial setup
def plt_tumor_data(self):
x = self.x_train
y = self.y_train
pos = y == 1
neg = y == 0
self.ax.scatter(x[pos], y[pos], marker='x', s=80, c = 'red', label="malignant")
self.ax.scatter(x[neg], y[neg], marker='o', s=100, label="benign", facecolors='none',
edgecolors=dlc["dlblue"],lw=3)
self.ax.set_ylim(-0.175,1.1)
self.ax.set_ylabel('y')
self.ax.set_xlabel('Tumor Size')
self.ax.set_title("Logistic Regression on Categorical Data")
def update(self, w, b):
self.w = w
self.b = b
self.draw_logistic_lines()
self.mk_cost_lines()
def draw_logistic_lines(self, firsttime=False):
if not firsttime:
self.aline[0].remove()
self.bline[0].remove()
self.alegend.remove()
xlim = self.ax.get_xlim()
x_hat = np.linspace(*xlim, 30)
y_hat = sigmoid(np.dot(x_hat.reshape(-1,1), self.w) + self.b)
self.aline = self.ax.plot(x_hat, y_hat, color=dlc["dlblue"],
label="y = sigmoid(z)")
f_wb = np.dot(x_hat.reshape(-1,1), self.w) + self.b
self.bline = self.ax.plot(x_hat, f_wb, color=dlc["dlorange"], lw=1,
label=f"z = {np.squeeze(self.w):0.2f}x+({self.b:0.2f})")
self.alegend = self.ax.legend(loc='upper left')
def mk_cost_lines(self, firsttime=False):
''' makes vertical cost lines'''
if not firsttime:
for artist in self.cost_items:
artist.remove()
self.cost_items = []
cstr = f"cost = (1/{self.m})*("
ctot = 0
label = 'cost for point'
addedbreak = False
for p in zip(self.x_train,self.y_train):
f_wb_p = sigmoid(self.w*p[0]+self.b)
c_p = compute_cost_matrix(p[0].reshape(-1,1), p[1],np.array(self.w), self.b, logistic=True, lambda_=0, safe=True)
c_p_txt = c_p
a = self.ax.vlines(p[0], p[1],f_wb_p, lw=3, color=dlc["dlpurple"], ls='dotted', label=label)
label='' #just one
cxy = [p[0], p[1] + (f_wb_p-p[1])/2]
b = self.ax.annotate(f'{c_p_txt:0.1f}', xy=cxy, xycoords='data',color=dlc["dlpurple"],
xytext=(5, 0), textcoords='offset points')
cstr += f"{c_p_txt:0.1f} +"
if len(cstr) > 38 and addedbreak is False:
cstr += "\n"
addedbreak = True
ctot += c_p
self.cost_items.extend((a,b))
ctot = ctot/(len(self.x_train))
cstr = cstr[:-1] + f") = {ctot:0.2f}"
## todo.. figure out how to get this textbox to extend to the width of the subplot
c = self.ax.text(0.05,0.02,cstr, transform=self.ax.transAxes, color=dlc["dlpurple"])
self.cost_items.append(c)
class contour_and_surface_plot:
''' plots combined in class as they have similar operations '''
# pylint: disable=missing-function-docstring
# pylint: disable=attribute-defined-outside-init
def __init__(self, axc, axs, x_train, y_train, w_range, b_range, w, b):
self.x_train = x_train
self.y_train = y_train
self.axc = axc
self.axs = axs
#setup useful ranges and common linspaces
b_space = np.linspace(*b_range, 100)
w_space = np.linspace(*w_range, 100)
# get cost for w,b ranges for contour and 3D
tmp_b,tmp_w = np.meshgrid(b_space,w_space)
z = np.zeros_like(tmp_b)
for i in range(tmp_w.shape[0]):
for j in range(tmp_w.shape[1]):
z[i,j] = compute_cost_matrix(x_train.reshape(-1,1), y_train, tmp_w[i,j], tmp_b[i,j],
logistic=True, lambda_=0, safe=True)
if z[i,j] == 0:
z[i,j] = 1e-9
### plot contour ###
CS = axc.contour(tmp_w, tmp_b, np.log(z),levels=12, linewidths=2, alpha=0.7,colors=dlcolors)
axc.set_title('log(Cost(w,b))')
axc.set_xlabel('w', fontsize=10)
axc.set_ylabel('b', fontsize=10)
axc.set_xlim(w_range)
axc.set_ylim(b_range)
self.update_contour_wb_lines(w, b, firsttime=True)
axc.text(0.7,0.05,"Click to choose w,b", bbox=dict(facecolor='white', ec = 'black'), fontsize = 10,
transform=axc.transAxes, verticalalignment = 'center', horizontalalignment= 'center')
#Surface plot of the cost function J(w,b)
axs.plot_surface(tmp_w, tmp_b, z, cmap = cm.jet, alpha=0.3, antialiased=True)
axs.plot_wireframe(tmp_w, tmp_b, z, color='k', alpha=0.1)
axs.set_xlabel("$w$")
axs.set_ylabel("$b$")
axs.zaxis.set_rotate_label(False)
axs.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
axs.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
axs.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
axs.set_zlabel("J(w, b)", rotation=90)
axs.view_init(30, -120)
axs.autoscale(enable=False)
axc.autoscale(enable=False)
self.path = path(self.w,self.b, self.axc) # initialize an empty path, avoids existance check
def update_contour_wb_lines(self, w, b, firsttime=False):
self.w = w
self.b = b
cst = compute_cost_matrix(self.x_train.reshape(-1,1), self.y_train, np.array(self.w), self.b,
logistic=True, lambda_=0, safe=True)
# remove lines and re-add on contour plot and 3d plot
if not firsttime:
for artist in self.dyn_items:
artist.remove()
a = self.axc.scatter(self.w, self.b, s=100, color=dlc["dlblue"], zorder= 10, label="cost with \ncurrent w,b")
b = self.axc.hlines(self.b, self.axc.get_xlim()[0], self.w, lw=4, color=dlc["dlpurple"], ls='dotted')
c = self.axc.vlines(self.w, self.axc.get_ylim()[0] ,self.b, lw=4, color=dlc["dlpurple"], ls='dotted')
d = self.axc.annotate(f"Cost: {cst:0.2f}", xy= (self.w, self.b), xytext = (4,4), textcoords = 'offset points',
bbox=dict(facecolor='white'), size = 10)
#Add point in 3D surface plot
e = self.axs.scatter3D(self.w, self.b, cst , marker='X', s=100)
self.dyn_items = [a,b,c,d,e]
class cost_plot:
""" manages cost plot for plt_quad_logistic """
# pylint: disable=missing-function-docstring
# pylint: disable=attribute-defined-outside-init
def __init__(self,ax):
self.ax = ax
self.ax.set_ylabel("log(cost)")
self.ax.set_xlabel("iteration")
self.costs = []
self.cline = self.ax.plot(0,0, color=dlc["dlblue"])
def re_init(self):
self.ax.clear()
self.__init__(self.ax)
def add_cost(self,J_hist):
self.costs.extend(J_hist)
self.cline[0].remove()
self.cline = self.ax.plot(self.costs)
class path:
''' tracks paths during gradient descent on contour plot '''
# pylint: disable=missing-function-docstring
# pylint: disable=attribute-defined-outside-init
def __init__(self, w, b, ax):
''' w, b at start of path '''
self.path_items = []
self.w = w
self.b = b
self.ax = ax
def re_init(self, w, b):
for artist in self.path_items:
artist.remove()
self.path_items = []
self.w = w
self.b = b
def add_path_item(self, w, b):
a = FancyArrowPatch(
posA=(self.w, self.b), posB=(w, b), color=dlc["dlblue"],
arrowstyle='simple, head_width=5, head_length=10, tail_width=0.0',
)
self.ax.add_artist(a)
self.path_items.append(a)
self.w = w
self.b = b
#-----------
# related to the logistic gradient descent lab
#----------
def truncate_colormap(cmap, minval=0.0, maxval=1.0, n=100):
""" truncates color map """
new_cmap = colors.LinearSegmentedColormap.from_list(
'trunc({n},{a:.2f},{b:.2f})'.format(n=cmap.name, a=minval, b=maxval),
cmap(np.linspace(minval, maxval, n)))
return new_cmap
def plt_prob(ax, w_out,b_out):
""" plots a decision boundary but include shading to indicate the probability """
#setup useful ranges and common linspaces
x0_space = np.linspace(0, 4 , 100)
x1_space = np.linspace(0, 4 , 100)
# get probability for x0,x1 ranges
tmp_x0,tmp_x1 = np.meshgrid(x0_space,x1_space)
z = np.zeros_like(tmp_x0)
for i in range(tmp_x0.shape[0]):
for j in range(tmp_x1.shape[1]):
z[i,j] = sigmoid(np.dot(w_out, np.array([tmp_x0[i,j],tmp_x1[i,j]])) + b_out)
cmap = plt.get_cmap('Blues')
new_cmap = truncate_colormap(cmap, 0.0, 0.5)
pcm = ax.pcolormesh(tmp_x0, tmp_x1, z,
norm=cm.colors.Normalize(vmin=0, vmax=1),
cmap=new_cmap, shading='nearest', alpha = 0.9)
ax.figure.colorbar(pcm, ax=ax)