@@ -251,7 +251,7 @@ def _m_step(x, z ,k):
251
251
return centers
252
252
253
253
254
- def _e_step (x , centers ):
254
+ def _e_step (x , centers , precompute_distances = True , x_squared_norms = None ):
255
255
"""E step of the K-means EM algorithm
256
256
257
257
Computation of the input-to-cluster assignment
@@ -276,22 +276,15 @@ def _e_step(x, centers):
276
276
n_samples = x .shape [0 ]
277
277
k = centers .shape [0 ]
278
278
279
- there_is_memory_to_compute_distances_matrix = True
280
-
281
- if there_is_memory_to_compute_distances_matrix :
282
- distances = (
283
- (x ** 2 ).sum (axis = 1 )
284
- + (centers ** 2 ).sum (axis = 1 ).reshape ((k ,1 ))
285
- - 2 * np .dot (centers , x .T ))
286
- # distances is a matrix of shape (k, n_samples)
287
-
279
+ if precompute_distances :
280
+ distances = all_pairs_l2_distance_squared (centers , x , x_squared_norms )
288
281
z = - np .ones (n_samples ).astype (np .int )
289
282
mindist = np .infty * np .ones (n_samples )
290
283
for q in range (k ):
291
- if there_is_memory_to_compute_distances_matrix :
284
+ if precompute_distances :
292
285
dist = distances [q ]
293
286
else :
294
- dist = np .sum ((x - centers [q ]) ** 2 , 1 )
287
+ dist = np .sum ((x - centers [q ]) ** 2 , axis = 1 )
295
288
z [dist < mindist ] = q
296
289
mindist = np .minimum (dist , mindist )
297
290
inertia = mindist .sum ()
0 commit comments