@@ -182,6 +182,7 @@ def fit(
182182 directional_variables : List [str ] = [],
183183 custom_scale_factor : dict = {},
184184 min_number_of_points : int = None ,
185+ max_number_of_iterations : int = 10 ,
185186 normalize_data : bool = True ,
186187 ) -> None :
187188 """
@@ -206,6 +207,10 @@ def fit(
206207 min_number_of_points : int, optional
207208 The minimum number of points to consider a cluster.
208209 Default is None.
210+ max_number_of_iterations : int, optional
211+ The maximum number of iterations for the K-Means algorithm.
212+ This is used when min_number_of_points is not None.
213+ Default is 10.
209214 normalize_data : bool, optional
210215 A flag to normalize the data. Default is True.
211216 """
@@ -248,9 +253,10 @@ def fit(
248253 if np .all (counts >= min_number_of_points ):
249254 stable_kma_child = True
250255 number_of_tries += 1
251- if number_of_tries > 10 :
256+ if number_of_tries > max_number_of_iterations :
252257 raise ValueError (
253- "Failed to find a stable K-Means configuration after 10 attempts."
258+ f"Failed to find a stable K-Means configuration after { max_number_of_iterations } attempts."
259+ "Change max_number_of_iterations or min_number_of_points."
254260 )
255261 self .logger .info (
256262 f"Found a stable K-Means configuration after { number_of_tries } attempts."
@@ -318,6 +324,7 @@ def fit_predict(
318324 directional_variables : List [str ] = [],
319325 custom_scale_factor : dict = {},
320326 min_number_of_points : int = None ,
327+ max_number_of_iterations : int = 10 ,
321328 normalize_data : bool = True ,
322329 ) -> Tuple [pd .DataFrame , pd .DataFrame ]:
323330 """
@@ -337,6 +344,10 @@ def fit_predict(
337344 min_number_of_points : int, optional
338345 The minimum number of points to consider a cluster.
339346 Default is None.
347+ max_number_of_iterations : int, optional
348+ The maximum number of iterations for the K-Means algorithm.
349+ This is used when min_number_of_points is not None.
350+ Default is 10.
340351 normalize_data : bool, optional
341352 A flag to normalize the data. Default is True.
342353
@@ -352,6 +363,7 @@ def fit_predict(
352363 directional_variables = directional_variables ,
353364 custom_scale_factor = custom_scale_factor ,
354365 min_number_of_points = min_number_of_points ,
366+ max_number_of_iterations = max_number_of_iterations ,
355367 normalize_data = normalize_data ,
356368 )
357369
0 commit comments