Hello! First time posting here. Any thoughts on why I’m getting an error when I compute the centroids in the K-means lab? The values and shape of the matrix are correct.

There are two different tests being done, but the display of the results is overlapping.

You’re getting the correct results for the unit test that is build into the notebook - the one that lists the “Expected Output”.

Your code does not pass the 2nd set of tests in public_tests.py “compute_centroids_test()”. This test uses a different size of X and a different number of centroids than the other tests use. That’s the test that is throwing the assert.

Hello @Haden_Willmuth,

If we focus on just the following two lines of your screenshot:

it is indeed interesting that while the `assert`

tests for `centroids.shape == (K, X.shape[1])`

, the message shows `len(X)`

and `idx.shape`

. I believe the message should have shown you `centroids.shape`

and `(K, X.shape[1])`

instead. This explains why the message doesn’t seem helpful in understanding the failure of that `assert`

test. I will notify the course team about that.

As for the failure of the `assert`

itself, since the message is not providing the exact value the test is comparing for, I suggest you to print it yourself, by adding the following temporary lines:

```
### END CODE HERE ##
print('compute_centroids, added temporarily for inspection purpose, and shall be removed afterwards to avoid conflict with the autograder')
print('Expecting:', centroids.shape)
print('Got:', (K, X.shape[1]))
print()
print('\n')
return centroids
```

Add the lines before the `return`

statement. Then, with the printed message, see if you can debug it yourself.

Cheers,

Raymond

Thanks! I was trying to do something fancy by stacking the idx vector with the X matrix within the compute centroid function. This was creating the problem for me

Hi,

I am getting following error can someone help. I just changed the values of

Error

# Select an initial set of centroids (3 Centroids)

initial_centroids = np.array([[1,2], [2,2], [1,1]])

# Find closest centroids using initial_centroids

idx = find_closest_centroids(X, initial_centroids)

# Print closest centroids for the first three elements

print(“First three elements in idx are:”, idx[:3])

# UNIT TEST

compute_centroids_test(compute_centroids)array in initial_centroid(Anyone can share the right value if you have)

Error Ends Here

Code starts from here Initial centroid value is changed in the code if someone has Pls share to pass it ([[1,2], [2,2], [1,1]])

# Select an initial set of centroids (3 Centroids)

initial_centroids = np.array([[1,2], [2,2], [1,1]])

# Find closest centroids using initial_centroids

idx = find_closest_centroids(X, initial_centroids)

# Print closest centroids for the first three elements

print(“First three elements in idx are:”, idx[:3])

# UNIT TEST

compute_centroids_test(compute_centroids)

Here are the correct test conditions:

{moderator edit: removed image that contained code}

Really appreciating your reply

Please do not post your code on the forum. That’s not allowed by the Code of Conduct.

Hint: Indentation is very important in Python.

Noted

can I post error or not?

One question Can I post error to ask the question.

Yes, you can post any error messages or asserts.

The assertion says your results for “centroids” is not correct.