Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save armgilles/18687545d9f0e081ebd8 to your computer and use it in GitHub Desktop.
Save armgilles/18687545d9f0e081ebd8 to your computer and use it in GitHub Desktop.
searching the number of cluster find by AffinityPropagation algo by moving preference value (checking if there is convergence and number of iteration to converge)
from sklearn.cluster import AffinityPropagation
import pandas as pd
import sys
import cStringIO
# You already have your feature in X
aff_eps = []
for i in [x for x in range(-50, 0, 5)]:
# To know caputre the output of verbose
tdout_ = sys.stdout #Keep track of the previous value.
stream = cStringIO.StringIO()
sys.stdout = stream
af = AffinityPropagation(preference=i, max_iter=300, verbose=True).fit(X)
sys.stdout = sys.__stdout__ # restore the previous stdout.
output = stream.getvalue()
if output.split()[0]== "Converged": # converge
converge = 1
iteration = float(output.split()[2])
else: # "Did not converge"
converge = 0
iteration = 0
n_clusters_ = len(af.cluster_centers_indices_)
print "preference = " +" "+ str(i) +" "+ " cluster = " + str(n_clusters_) +" "+ " Nb iteration " +" "+ str(iteration)
aff_eps.append({'preference' : i,
'n_cluster' : n_clusters_,
'verbose': output,
'converge' : converge,
'iteration' : iteration})
df_aff_eps = pd.DataFrame(aff_eps)
df_aff_eps.set_index('preference', inplace=True)
ax = df_aff_eps.n_cluster.plot(legend=True, x_compat=True)
ax.set_ylabel("Number of Cluster")
ax = df_aff_eps.iteration.plot(secondary_y=True, style='--', color='r', legend=True)
ax.set_ylabel("Convergence with number of iteration, O = no convergence")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment