3 alt.data_transformers.disable_max_rows()
4 alt.data_transformers.enable('data_server')
6 from numpy import random
8 from sklearn.manifold import TSNE
10 df = pd.read_csv("reddit_term_similarity_3000.csv")
11 df = df.sort_values(['i','j'])
13 n = max(df.i.max(),df.j.max())
17 grp = grp.sort_values('j')
18 return np.concatenate([np.zeros(n-p),np.zeros(1),np.array(grp.value)])
20 col_names = df.sort_values('j').loc[:,['subreddit_j']].drop_duplicates()
21 first_name = list(set(df.subreddit_i) - set(df.subreddit_j))[0]
22 col_names = [first_name] + list(col_names.subreddit_j)
23 mat = df.groupby('i').apply(zero_pad)
24 mat.loc[n] = np.concatenate([np.zeros(n),np.ones(1)])
27 # plot the matrix using the first and second eigenvalues
28 mat = mat + np.tril(mat.transpose(),k=-1)
30 tsne_model = TSNE(2,learning_rate=500,perplexity=40,n_iter=2000)
31 tsne_fit_model = tsne_model.fit(mat)
32 tsne_fit_whole = tsne_fit_model.fit_transform(mat)
34 plot_data = pd.DataFrame({'x':tsne_fit_whole[:,0],'y':tsne_fit_whole[:,1], 'subreddit':col_names})
36 plot_data.to_feather("tsne_subreddit_fit.feather")
38 slider = alt.binding_range(min=1,max=100,step=1,name='zoom: ')
39 selector = alt.selection_single(name='zoomselect',fields=['zoom'],bind='scales',init={'zoom':1})
41 xrange = plot_data.x.max()-plot_data.x.min()
42 yrange = plot_data.y.max()-plot_data.y.min()
44 chart = alt.Chart(plot_data).mark_text().encode(
45 alt.X('x',axis=alt.Axis(grid=False)),
46 alt.Y('y',axis=alt.Axis(grid=False)),
49 #chart = chart.add_selection(selector)
51 chart = chart.configure_view(
52 continuousHeight=xrange/20,
53 continuousWidth=yrange/20
56 amount_shown = lambda zoom: {'width':xrange/zoom,'height':yrange/zoom}
58 alt.data_transformers.enable('default')
59 chart = chart.properties(width=1000,height=1000)
60 chart = chart.interactive()
61 chart.save("test_tsne_whole.html")
62 chart = chart.properties(width=10000,height=10000)
63 chart.save("test_tsne_whole.svg")