FAQ: K-Means Clustering - Implementing K-Means: Step 3

This community-built FAQ covers the “Implementing K-Means: Step 3” exercise from the lesson “K-Means Clustering”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Science

Machine Learning

FAQs on the exercise Implementing K-Means: Step 3

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Don’t know if something changed in numpy but it should be axis = 1 now

We may be confused if we are used to Pandas’ some methods such as .drop(), but here axis = 0 seems to be correct. The following page is about Pandas mean, but it will be helpful.

why every timmei ryn the code i got different list of points???thanks for your answer:)

Recall that we have first placed 3 centroids randomly.

# Step 1: Place K random centroids

k = 3

centroids_x = np.random.uniform(min(x), max(x), size=k)
centroids_y = np.random.uniform(min(y), max(y), size=k)

centroids = np.array(list(zip(centroids_x, centroids_y)))

So the initial value of centroids (and centroids_old) differs randomly every time.

1 Like

thanks you for your insightful answer:)

for i in range(0,k):
points =
for j in range(len(labels)):
if labels[j] == i:
points.append(sepal_length_width[j])
centroids[i] = np.mean(points, axis = 0)

Says this in the Hint for Q3:
“If you don’t have axis=0 parameter, the default is to compute the mean of the flattened array. We need the axis=0 here to specify that we want to compute the means along the rows.”

With reference to the last line of codes, why do we only want to compute means along the rows? Why can’t mean be calculated for axis = 1 too to get the new centroids? Appreciate any clarification.