Last active
April 5, 2016 15:10
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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