-
Notifications
You must be signed in to change notification settings - Fork 11
/
helper_functions.py
294 lines (218 loc) · 9.08 KB
/
helper_functions.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
import ipywidgets as widgets
import matplotlib.pyplot as plt
from IPython.display import Math, HTML, display, Latex, YouTubeVideo, clear_output
import matplotlib
import numpy as np
import time
from os import path
def embed_video(url, labels={}):
names = list(labels.keys())
buttons = []
def button_clicked(change):
clear_output()
display(YouTubeVideo('ARJ8cAGm6JE', start=(
labels[change.description]), autoplay=True))
display(button_box)
for i in range(len(names)):
buttons.append(widgets.Button(description=names[i]))
buttons[-1].on_click(button_clicked)
button_box = widgets.HBox(buttons)
display(YouTubeVideo('ARJ8cAGm6JE', autoplay=False))
display(button_box)
def toggle_code(title="code", on_load_hide=True, above=1):
if above < 1:
print("Error: please input a valid value for 'above'")
else:
display_string = """
<script>
function get_new_label(butn, hide) {
var shown = $(butn).parents("div.cell.code_cell").find('div.input').is(':visible');
var title = $(butn).val().substr($(butn).val().indexOf(" ") + 1)
return ((shown) ? 'Show ' : 'Hide ') + title
}
function code_toggle(butn, hide) {
$(butn).val(get_new_label(butn,hide));
$(hide).slideToggle();
};
</script>
<input type="submit" value='initiated' class='toggle_button'>
<script>
var hide_area = $(".toggle_button[value='initiated']").parents('div.cell').prevAll().addBack().slice(-""" + str(above) + """)
hide_area = $(hide_area).find("div.input").add($(hide_area).filter("div.text_cell"))
$(".toggle_button[value='initiated']").prop("hide_area", hide_area)
$(".toggle_button[value='initiated']").click(function(){
code_toggle(this, $(this).prop("hide_area"))
});
$(".toggle_button[value='initiated']").parents("div.output_area").insertBefore($(".toggle_button[value='initiated']").parents("div.output").find('div.output_area').first());
var shown = $(".toggle_button[value='initiated']").parents("div.cell.code_cell").find('div.input').is(':visible');
var title = ((shown) ? 'Hide ' : 'Show ') + '""" + title + """';
"""
if on_load_hide:
display_string += """ $(".toggle_button[value='initiated']").addClass("init_show");
$(hide_area).addClass("init_hidden"); """
else:
display_string += """ $(".toggle_button[value='initiated']").addClass("init_hide");
$(hide_area).addClass("init_shown"); """
display_string += """ $(".toggle_button[value='initiated']").val(title);
</script>"""
display(HTML(display_string))
def dropdown_math(title, text=None, file=None):
out = widgets.Output()
with out:
if not(file == None):
handle = open(file, 'r')
display(Latex(handle.read()))
else:
display(Math(text))
accordion = widgets.Accordion(children=[out])
accordion.set_title(0, title)
accordion.selected_index = None
return accordion
def remove_axes(which_axes=''):
frame = plt.gca()
if 'x' in which_axes:
frame.axes.get_xaxis().set_visible(False)
if 'y' in which_axes:
frame.axes.get_yaxis().set_visible(False)
elif which_axes == '':
frame.axes.get_xaxis().set_visible(False)
frame.axes.get_yaxis().set_visible(False)
return
def set_notebook_preferences(home_button = True):
css_file = path.join(path.dirname(__file__), 'notebook.css')
css = open(css_file, "r").read()
display_string = "<style>" + css + "</style>"
if (home_button):
display_string += """
<input type="submit" value='Home' id="initiated" class='home_button' onclick='window.location="../index.ipynb"' style='float: right; margin-right: 40px;'>
<script>
$('.home_button').not('#initiated').remove();
$('.home_button').removeAttr('id');
$(".home_button").insertBefore($("div.cell").first());
"""
else:
display_string += "<script>$('.home_button').remove();"
display_string += """
$('div.input.init_hidden').hide()
$('div.input.init_shown').show()
$('.toggle_button').each(function( index, element ) {
var prefix;
if (this.classList.contains('init_show')) {
prefix = 'Show '
}
else if (this.classList.contains('init_hide')) {
prefix = 'Hide '
};
$(this).val(prefix + $(this).val().substr($(this).val().indexOf(" ") + 1))
});
IPython.OutputArea.prototype._should_scroll = function(lines) {
return false;
}
</script>
"""
display(HTML(display_string))
matplotlib.rcParams['mathtext.fontset'] = 'stix'
matplotlib.rcParams['font.family'] = 'sans-serif'
matplotlib.rc('axes', titlesize=14)
matplotlib.rc('axes', labelsize=14)
matplotlib.rc('xtick', labelsize=12)
matplotlib.rc('ytick', labelsize=12)
def beautify_plot(params):
if not(params.get('title', None) == None):
plt.title(params.get('title'))
if not(params.get('x', None) == None):
plt.xlabel(params.get('x'))
if not(params.get('y', None) == None):
plt.ylabel(params.get('y'))
def sample_weights_from(w1, w2, post):
idx1, idx2 = np.arange(0, post.shape[0]), np.arange(0, post.shape[1])
idx1, idx2 = np.meshgrid(idx1, idx2)
idx = np.stack([idx1, idx2], axis=2)
idx = np.reshape(idx, (-1, 2))
flat_post = np.reshape(post, (-1,)).copy()
flat_post /= flat_post.sum()
sample_idx = np.random.choice(
np.arange(0, flat_post.shape[0]), p=flat_post)
grid_idx = idx[sample_idx]
return w1[grid_idx[0]], w2[grid_idx[1]]
def kNN(X_train, Y_train, X_test, k, p=2):
# clone test points for comparisons as before
X_test_clone = np.stack([X_test]*X_train.shape[0], axis=-2)
distances = np.sum(np.abs(X_test_clone - X_train)**p,
axis=-1) # compute Lp distances
idx = np.argsort(distances, axis=-1)[:, :k] # find k smallest distances
classes = Y_train[idx] # classes corresponding to the k smallest distances
predictions = []
for class_ in classes:
uniques, counts = np.unique(class_, return_counts=True)
if (counts == counts.max()).sum() == 1:
predictions.append(uniques[np.argmax(counts)])
else:
predictions.append(np.random.choice(
uniques[np.where(counts == counts.max())[0]]))
return np.array(predictions)
def sig(x):
return 1/(1 + np.exp(-x))
def logistic_gradient_ascent(x, y, init_weights, no_steps, stepsize):
x = np.append(np.ones(shape=(x.shape[0], 1)), x, axis=1)
w = init_weights.copy()
w_history, log_liks = [], []
for n in range(no_steps):
log_liks.append(np.sum(y*np.log(sig(x.dot(w))) +
(1 - y)*np.log(1 - sig(x.dot(w)))))
w_history.append(w.copy())
sigs = sig(x.dot(w))
dL_dw = np.mean((y - sigs)*x.T, axis=1)
w += stepsize*dL_dw
return np.array(w_history), np.array(log_liks)
def softmax(x):
return (np.exp(x).T/np.sum(np.exp(x), axis=1)).T
def softmax_gradient_ascent(x, y, init_weights, no_steps, stepsize):
x = np.append(np.ones(shape=(x.shape[0], 1)), x, axis=1)
w = init_weights.copy()
w_history, log_liks = [], []
for n in range(no_steps):
log_liks.append(np.sum(y*np.log(softmax(x.dot(w)))))
w_history.append(w.copy())
soft_ = softmax(x.dot(w))
dL_dw = (x.T).dot(y - soft_)/x.shape[0]
w += stepsize*dL_dw
return np.array(w_history), np.array(log_liks)
def PCA(x):
S = ((x - x.mean()).T).dot(x - x.mean())/x.shape[0]
eig_values, eig_vectors = np.linalg.eig(S)
sort_idx = (-eig_values).argsort()
eig_values, eig_vectors = eig_values[sort_idx], eig_vectors[:, sort_idx]
return np.real(eig_values), np.real(eig_vectors)
def PCA_N(x):
S = ((x - x.mean(axis=0)).T).dot(x - x.mean(axis=0))/x.shape[0]
t = time.time()
eig_values, eig_vectors = np.linalg.eig(S)
print('Time taken for high-dimensional approach:',
np.round((time.time() - t), 3), 'sec')
sort_idx = (-eig_values).argsort()
eig_values, eig_vectors = eig_values[sort_idx], eig_vectors[:, sort_idx]
return np.real(eig_values), np.real(eig_vectors)
def k_means(x, K, max_steps, mu_init):
N, D = x.shape
mu = mu_init.copy()
s = np.zeros(shape=(N, K))
assignments = np.random.choice(np.arange(0, K), N)
s[np.arange(s.shape[0]), assignments] = 1
x_stacked = np.stack([x]*K, axis=1)
losses = [np.sum(s*np.sum((x_stacked - mu)**2, axis=2))]
converged = False
for i in range(max_steps):
mus = (s.T).dot(x)
s_sum = s.sum(axis=0).reshape((-1, 1))
s_sum[np.where(s_sum < 1)] = 1
mus /= s_sum
distances = np.sum((x_stacked - mus)**2, axis=2)
min_idx = np.argmin(distances, axis=1)
s_prev = s.copy()
s = np.zeros_like(s)
s[np.arange(s.shape[0]), min_idx] = 1
losses.append(np.sum(s*np.sum((x_stacked - mus)**2, axis=2)))
if np.prod(np.argmax(s, axis=1) == np.argmax(s_prev, axis=1)):
break
return s, mus, losses