]> code.communitydata.science - cdsc_reddit.git/blob - visualization/tsne_vis.py
Add file to plot related subreddits using tsne.
[cdsc_reddit.git] / visualization / tsne_vis.py
1 import pyarrow
2 import altair as alt
3 alt.data_transformers.disable_max_rows()
4 alt.data_transformers.enable('data_server')
5 import pandas as pd
6 from numpy import random
7 import numpy as np
8 from sklearn.manifold import TSNE
9
10 df = pd.read_csv("reddit_term_similarity_3000.csv")
11 df = df.sort_values(['i','j'])
12
13 n = max(df.i.max(),df.j.max())
14
15 def zero_pad(grp):
16     p = grp.shape[0]
17     grp = grp.sort_values('j')
18     return np.concatenate([np.zeros(n-p),np.zeros(1),np.array(grp.value)])
19
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)])
25 mat = np.stack(mat)
26
27 # plot the matrix using the first and second eigenvalues
28 mat = mat + np.tril(mat.transpose(),k=-1)
29
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)
33
34 plot_data = pd.DataFrame({'x':tsne_fit_whole[:,0],'y':tsne_fit_whole[:,1], 'subreddit':col_names})
35
36 plot_data.to_feather("tsne_subreddit_fit.feather")
37
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})
40
41 xrange = plot_data.x.max()-plot_data.x.min()
42 yrange = plot_data.y.max()-plot_data.y.min()
43
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)),
47     text='subreddit')
48
49 #chart = chart.add_selection(selector)
50
51 chart = chart.configure_view(
52     continuousHeight=xrange/20,
53     continuousWidth=yrange/20
54 )
55
56 amount_shown = lambda zoom: {'width':xrange/zoom,'height':yrange/zoom}
57
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")

Community Data Science Collective || Want to submit a patch?