5 from sklearn.cluster import AffinityPropagation
8 def affinity_clustering(similarities, output, damping=0.9, max_iter=100000, convergence_iter=30, preference_quantile=0.5, random_state=1968, verbose=True):
10 similarities: feather file with a dataframe of similarity scores
11 preference_quantile: parameter controlling how many clusters to make. higher values = more clusters. 0.85 is a good value with 3000 subreddits.
12 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.
15 df = pd.read_feather(similarities)
17 mat = np.array(df.drop('subreddit',1))
18 mat[range(n),range(n)] = 1
20 preference = np.quantile(mat,preference_quantile)
22 print(f"preference is {preference}")
26 clustering = AffinityPropagation(damping=damping,
28 convergence_iter=convergence_iter,
30 preference=preference,
31 affinity='precomputed',
33 random_state=random_state).fit(mat)
36 print(f"clustering took {clustering.n_iter_} iterations")
37 clusters = clustering.labels_
39 print(f"found {len(set(clusters))} clusters")
41 cluster_data = pd.DataFrame({'subreddit': df.subreddit,'cluster':clustering.labels_})
43 cluster_sizes = cluster_data.groupby("cluster").count()
44 print(f"the largest cluster has {cluster_sizes.subreddit.max()} members")
46 print(f"the median cluster has {cluster_sizes.subreddit.median()} members")
48 print(f"{(cluster_sizes.subreddit==1).sum()} clusters have 1 member")
50 cluster_data.to_feather(output)
52 if __name__ == "__main__":
53 fire.Fire(affinity_clustering)