1616
1717
1818def affinity_propagation (S , preference = None , p = None , convergence_iter = 15 ,
19- convit = None , max_iter = 200 ,
20- damping = 0.5 , copy = True , verbose = False ):
19+ convit = None , max_iter = 200 , damping = 0.5 , copy = True ,
20+ verbose = False ):
2121 """Perform Affinity Propagation Clustering of data
2222
2323 Parameters
@@ -80,16 +80,15 @@ def affinity_propagation(S, preference=None, p=None, convergence_iter=15,
8080 n_samples = S .shape [0 ]
8181
8282 if S .shape [0 ] != S .shape [1 ]:
83- raise ValueError ("S must be a square array (shape=%r )" % S .shape )
83+ raise ValueError ("S must be a square array (shape=%s )" % repr ( S .shape ) )
8484
8585 if not p is None :
8686 warnings .warn ("p is deprecated and will be removed in version 0.14."
87- " Use ``preference`` instead." , DeprecationWarning )
87+ " Use ``preference`` instead." , DeprecationWarning )
8888 preference = p
8989
9090 if preference is None :
9191 preference = np .median (S )
92-
9392 if damping < 0.5 or damping >= 1 :
9493 raise ValueError ('damping must be >= 0.5 and < 1' )
9594
@@ -102,8 +101,8 @@ def affinity_propagation(S, preference=None, p=None, convergence_iter=15,
102101 R = np .zeros ((n_samples , n_samples )) # Initialize messages
103102
104103 # Remove degeneracies
105- S += (np .finfo (np .double ).eps * S + np .finfo (np .double ).tiny * 100 ) * \
106- random_state .randn (n_samples , n_samples )
104+ S += (( np .finfo (np .double ).eps * S + np .finfo (np .double ).tiny * 100 ) *
105+ random_state .randn (n_samples , n_samples ) )
107106
108107 # Execute parallel affinity propagation updates
109108 e = np .zeros ((n_samples , convergence_iter ))
@@ -148,8 +147,8 @@ def affinity_propagation(S, preference=None, p=None, convergence_iter=15,
148147
149148 if it >= convergence_iter :
150149 se = np .sum (e , axis = 1 )
151- unconverged = np .sum ((se == convergence_iter ) + \
152- ( se == 0 )) != n_samples
150+ unconverged = ( np .sum ((se == convergence_iter ) + ( se == 0 ))
151+ != n_samples )
153152 if (not unconverged and (K > 0 )) or (it == max_iter ):
154153 if verbose :
155154 print "Converged after %d iterations." % it
@@ -246,8 +245,8 @@ class AffinityPropagation(BaseEstimator, ClusterMixin):
246245 """
247246
248247 def __init__ (self , damping = .5 , max_iter = 200 , convergence_iter = 15 ,
249- convit = None , copy = True ,
250- preference = None , p = None , affinity = 'euclidean' , verbose = False ):
248+ convit = None , copy = True , preference = None , p = None ,
249+ affinity = 'euclidean' , verbose = False ):
251250
252251 if convit is not None :
253252 warnings .warn ("``convit`` is deprectaed and will be removed in "
@@ -262,7 +261,7 @@ def __init__(self, damping=.5, max_iter=200, convergence_iter=15,
262261 self .verbose = verbose
263262 if not p is None :
264263 warnings .warn ("p is deprecated and will be removed in version 0.14"
265- ". Use ``preference`` instead." , DeprecationWarning )
264+ ". Use ``preference`` instead." , DeprecationWarning )
266265 preference = p
267266
268267 self .preference = preference
@@ -295,7 +294,7 @@ def fit(self, X):
295294 self .affinity_matrix_ = - euclidean_distances (X , squared = True )
296295 else :
297296 raise ValueError ("Affinity must be 'precomputed' or "
298- "'euclidean'. Got %s instead" % str (self .affinity ))
297+ "'euclidean'. Got %s instead" % str (self .affinity ))
299298
300299 self .cluster_centers_indices_ , self .labels_ = affinity_propagation (
301300 self .affinity_matrix_ , self .preference ,
0 commit comments