C3_W1 Assignment 1.2 - UNIT TEST does not accept correct values

Hi everybody,

I am trying to solve the assignment “1.2 Computing centroid means” and as far as I can see the new centroid values are correctly calculated - however, the UNIT Test will not accpet it (s. screenshot)

Only thing I can see is that the “K” value in the UNIT TEST is set to “2” although it should be “3”, since I do not touch this value anywhere in my code, not sure if that is a hint.

Anyway, I cannot see the error, can anybody help?

Many Thanks!

Your code must work for any value of K.

Hi @hendrik ,

The problem is not in the value of K, it is in the compute_centroids() function. The error is in line 50 from your screenshot. This error is raised because you are using a divide operator on an operand that is a list datatype, and and another that is an integer datatype. To compute the means of data points, you can do np.mean().
Here is a write up on np.mean().

Hi Kic,

many thanks for your fast reply!

I changed the code accordingly, but the problem is not solved yet.

I still think it is related to the fact, that the UNIT TEST (for whatever reason) sets the “K” value to “2”

although I do not touch that “K” parameter in my code.

The returned centroid values are Ok, but is maybe the format of the centroid array not correct?? The shape is (3, 2) - as it should be, or?

Thanks for any ideas/help/support!

Hi @hendrik ,

Your code should be able to deal with any value set for K. There are two tests running one after another, the first one is when K=3, the second one is the public test, calling compute_centroids_test().

I suggest you check your code against the instructions from the hints. Think about what your code needed to do based on the implementation instruction given at the start of ex2.

The problem is related to K, but not in the way you think. Your code only seems to work for a specific value, ie K=3. When K=2 it works twice, but then fails on the third calculation. The clue is that the implementation works only for the first unit test, but throws an exception when the new value for K is less than the one you tried first. This is a signal that an invalid assumption was hard coded into your algorithm. So how could you change that and accommodate a different requirement, the one that @Kic points out, which is that the code should work correctly not just when K is exactly 3, but for any value. Would you really want a compute_centroids() function that only worked when the number of centroids was known apriori to be a specific number?

Testing frameworks should always test with a variety of different values for exactly this reason. And, spoiler alert, when you submit your solution for grade, expect still more different values as test parameters. If you hard code solutions just pass the unit tests but that aren’t general, they will still fail the grader. The forum is replete with threads reflecting this error.

Hi all,

I took your advice, implemented a more flexible code - and it works …

Big THANKS for your support!!! :slight_smile:

1 Like