 # When does convergence happen?

### Question

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.

4 Likes

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
False
``````

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

1 Like

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 !!! 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])