SPOILER: Practice lab - K-means - order of complexity of the code

My code (see below) passed all the Grader’s tests. I wonder, though, if the order of complexity of my code is similar to (or, at least, not much worse than) the one suggested in the Hints (commented # at the bottom)!? In my code I used nested loops. I suspect, though, that, under the hood, the boolean (X[idx == j]) and the numpy method used in the suggested shorter implementation (Hints) are also employing a loop (making it a nested loop as well). Is my understanding correct?

Firstly, I am not sure if it is ok to post your code in the discussion forum.

Secondly, the hint encourages us to use numpy functions which perform vectorization (mentioned by Prof. Ng in previous courses). Under the hood, I do believe that the functions (boolean comparison, np.sum, etc…) also loop through the tensors. However, the implementations are done in C/C++ thus the run time is much faster than in Python - due to the nature of the data structure.

Hi @Robert_Ascan,

If you want to estimate the execution time different between using a loop or the vectorized approach, you may want to know about the %timeit or the %%timeit magic provided in the jupyter environment, and please this out if so.

Then you can see for yourself which one is actually better. As for how people implement numpy to make the most of our CPU to optimize vectorized calculation, I am not an expert, but if you want to find some relevant google keywords, you may start from here.

Lastly, as @luongtd said, we are not allowed to post assignment code here, so I am going to remove it.