3 from numpy import random
5 from sklearn.manifold import TSNE
7 df = pd.read_feather("reddit_term_similarity_3000.feather")
8 df = df.sort_values(['i','j'])
10 n = max(df.i.max(),df.j.max())
14 grp = grp.sort_values('j')
15 return np.concatenate([np.zeros(n-p),np.ones(1),np.array(grp.value)])
17 col_names = df.sort_values('j').loc[:,['subreddit_j']].drop_duplicates()
18 first_name = list(set(df.subreddit_i) - set(df.subreddit_j))[0]
19 col_names = [first_name] + list(col_names.subreddit_j)
20 mat = df.groupby('i').apply(zero_pad)
21 mat.loc[n] = np.concatenate([np.zeros(n),np.ones(1)])
24 mat = mat + np.tril(mat.transpose(),k=-1)
25 dist = 2*np.arccos(mat)/np.pi
27 tsne_model = TSNE(2,learning_rate=500,perplexity=50,n_iter=10000,metric='precomputed',early_exaggeration=20,n_jobs=-1)
29 tsne_fit_model = tsne_model.fit(dist)
31 tsne_fit_whole = tsne_fit_model.fit_transform(dist)
33 plot_data = pd.DataFrame({'x':tsne_fit_whole[:,0],'y':tsne_fit_whole[:,1], 'subreddit':col_names})
35 plot_data.to_feather("tsne_subreddit_fit.feather")