Skip to content

Commit ce19ee5

Browse files
committed
add and plot vector norms
1 parent bde92e5 commit ce19ee5

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

Diff for: ml.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pandas as pd
88
from pandas.io.formats.style import Styler
99

10-
from .arrays import ArrayLike
10+
from .arrays import ArrayLike, normalize_vector_norms
1111
from .common import with_status
1212
from .console import new_quiet_console, stderr
1313
from .data import (_check_input_dataframe, build_multi_index_dataframe,
@@ -371,6 +371,32 @@ def intra_cluster_variation(
371371
return wss
372372

373373

374+
def get_vector_norms(
375+
target_activities: ArrayLike,
376+
timeline_slice_aligned_models: list,
377+
timeline_slices: ArrayLike,
378+
act2idx: dict) -> ArrayLike:
379+
380+
# NOTE: timeline_slice_aligned_models is
381+
# [dynamic_model.embeddings[t] for t in range(len(dynamic_model.embeddings))]
382+
383+
aligned_activity_norms = []
384+
for act in target_activities:
385+
# norms = []
386+
embeddings = []
387+
for time_slice in timeline_slices:
388+
act_emb_at_week = timeline_slice_aligned_models[timeline_slices.index(time_slice)][act2idx[act], :]
389+
embeddings.append(act_emb_at_week)
390+
# norm = np.linalg.norm(act_emb_at_week)
391+
# norms.append(norm)
392+
393+
# norms = np.array(norms)
394+
# norms = norms / sum(norms)
395+
aligned_activity_norms.append(normalize_vector_norms(embeddings))
396+
return aligned_activity_norms
397+
398+
399+
374400
def build_tsne_projection(
375401
activity: str,
376402
time_slices: ArrayLike,

Diff for: plots.py

+33-3
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,7 @@ def make_dendrogram(*args, **kwargs):
303303
return ddata
304304

305305

306-
def plot_dynamic_activity_embeddings(
307-
annotated_coordinates: ArrayLike,
308-
**kwargs) -> None:
306+
def plot_dynamic_activity_embeddings(annotated_coordinates: ArrayLike, **kwargs) -> None:
309307
pyplot_module = kwargs.get('pyplot_module', None)
310308
if pyplot_module is None:
311309
plt = import_module('matplotlib.pyplot', 'matplotlib')
@@ -329,6 +327,38 @@ def plot_dynamic_activity_embeddings(
329327
ha=ha,
330328
va=va)
331329
plt.show()
330+
331+
332+
def plot_embedding_changes_in_vector_space(
333+
target_activities: ArrayLike,
334+
aligned_activity_norms: ArrayLike,
335+
timeline_slices: ArrayLike, **kwargs) -> None:
336+
337+
pyplot_module = kwargs.get('pyplot_module', None)
338+
if pyplot_module is None:
339+
plt = import_module('matplotlib.pyplot', 'matplotlib')
340+
else:
341+
plt = pyplot_module
342+
343+
if isinstance(timeline_slices, np.ndarray):
344+
timeline_slices = timeline_slices.tolist()
345+
346+
figsize = kwargs.get('figsize', (15,10))
347+
markersize = kwargs.get('markersize', 7)
348+
plt.figure(figsize=figsize)
349+
time_frames = [week for week in timeline_slices]
350+
markers = ['+', 'o', 'x']
351+
plt.clf()
352+
353+
for idx in range(len(aligned_activity_norms)):
354+
norms = aligned_activity_norms[idx]
355+
plt.plot(time_frames, norms, marker=markers[idx], markersize=markersize)
356+
357+
plt.legend(target_activities)
358+
plt.xlabel('week')
359+
plt.ylabel('activity norm')
360+
plt.show()
361+
332362

333363

334364
if __name__ == "__main__":

0 commit comments

Comments
 (0)