When does convergence happen?


In the context of this exercise, describing step 4 of K-Means clustering, when does convergence happen?


Convergence happens when the positions of all the centroids are no longer updated, meaning that they must have converged on some clusters.

We do this in our code by calculating the change in distance for each centroid from its previous position and storing these values inside of an array, called errors. When all the values in this array become 0, there has been convergence.


Unfortunately, the exercise suggests to use while error.all() != 0 as the loop condition. However, this will stop the loop as soon as any of the values in error becomes zero, instead of all of them.:

>>> import numpy as np 
>>> error = np.array([1,0,1])
>>> error.all() != 0

Probably a better terminating condition would be while error.any() != 0.


Agree, while error.any() != 0 would be the correct conditional statement in this case.

1 Like

Nice suggestion, error.all() != 0 will stop even if 1 error is zero.

Totally agree !!! :hugs:

Traceback (most recent call last):
File “script.py”, line 67, in
plt.scatter(points[:, 0], points[:, 1], c=colors[i], alpha=0.5)
TypeError: list indices must be integers or slices, not tuple
Why is this error popping up and what do I do to get rid of it?

You hate to include list points in np.array()
points = np.array([sepal_length_width[j] for j in range(len(sepal_length_width)) if labels[j] == i])

It is certainly so. I was ashamed of not noticing it until I saw your post. Thank you!

In addition, as a conditional statement (boolean comparison), != 0 is synonymous with == True, so I thought it was redundant as a condition for the while statement. So we can simply write while error.any():.

Traceback (most recent call last):
  File "script.py", line 48, in <module>
    plt.scatter(points[:, 0], points[:, 1], c=colors[i], alpha=0.5)
IndexError: too many indices for array

why does this error pop up?

In defining the points array you might have missed to include the whole in the np.array( ) function. I face the same error.