4 from numpy import random
6 from sklearn.manifold import TSNE
8 similarities = "/gscratch/comdata/output/reddit_similarity/subreddit_author_tf_similarities_10000.parquet"
10 def fit_tsne(similarities, output, learning_rate=750, perplexity=50, n_iter=10000, early_exaggeration=20):
12 similarities: feather file with a dataframe of similarity scores
13 learning_rate: parameter controlling how fast the model converges. Too low and you get outliers. Too high and you get a ball.
14 perplexity: number of neighbors to use. the default of 50 is often good.
17 df = pd.read_feather(similarities)
20 mat = np.array(df.drop('subreddit',1),dtype=np.float64)
21 mat[range(n),range(n)] = 1
23 dist = 2*np.arccos(mat)/np.pi
24 tsne_model = TSNE(2,learning_rate=750,perplexity=50,n_iter=10000,metric='precomputed',early_exaggeration=20,n_jobs=-1)
25 tsne_fit_model = tsne_model.fit(dist)
27 tsne_fit_whole = tsne_fit_model.fit_transform(dist)
29 plot_data = pd.DataFrame({'x':tsne_fit_whole[:,0],'y':tsne_fit_whole[:,1], 'subreddit':df.subreddit})
31 plot_data.to_feather(output)
33 if __name__ == "__main__":