2 # TODO: replace prints with logging.
6 from sklearn.cluster import AffinityPropagation
8 from pathlib import Path
10 def read_similarity_mat(similarities, use_threads=True):
11 df = pd.read_feather(similarities, use_threads=use_threads)
12 mat = np.array(df.drop('_subreddit',1))
14 mat[range(n),range(n)] = 1
15 return (df._subreddit,mat)
17 def affinity_clustering(similarities, *args, **kwargs):
18 subreddits, mat = read_similarity_mat(similarities)
19 return _affinity_clustering(mat, subreddits, *args, **kwargs)
21 def _affinity_clustering(mat, subreddits, output, damping=0.9, max_iter=100000, convergence_iter=30, preference_quantile=0.5, random_state=1968, verbose=True):
23 similarities: feather file with a dataframe of similarity scores
24 preference_quantile: parameter controlling how many clusters to make. higher values = more clusters. 0.85 is a good value with 3000 subreddits.
25 damping: parameter controlling how iterations are merged. Higher values make convergence faster and more dependable. 0.85 is a good value for the 10000 subreddits by author.
27 print(f"damping:{damping}; convergenceIter:{convergence_iter}; preferenceQuantile:{preference_quantilne}")
29 preference = np.quantile(mat,preference_quantile)
31 print(f"preference is {preference}")
34 clustering = AffinityPropagation(damping=damping,
36 convergence_iter=convergence_iter,
38 preference=preference,
39 affinity='precomputed',
41 random_state=random_state).fit(mat)
44 print(f"clustering took {clustering.n_iter_} iterations")
45 clusters = clustering.labels_
47 print(f"found {len(set(clusters))} clusters")
49 cluster_data = pd.DataFrame({'subreddit': subreddits,'cluster':clustering.labels_})
51 cluster_sizes = cluster_data.groupby("cluster").count()
52 print(f"the largest cluster has {cluster_sizes.subreddit.max()} members")
54 print(f"the median cluster has {cluster_sizes.subreddit.median()} members")
56 print(f"{(cluster_sizes.subreddit==1).sum()} clusters have 1 member")
59 cluster_data.to_feather(output)
60 print(f"saved {output}")
63 if __name__ == "__main__":
64 fire.Fire(affinity_clustering)