W2_Small difference in numpy dot multiplication vs for loop

In Professor Andrew Ng’s Week 2 topic on vectorization, he compared the speed of vectorization vs using explicit for loops. I just wanted to ask why when I printed the c output of both vectorized and explicit for loop multiplication, there is a very small difference. is this due to rounding off errors? can someone please enlighten me thank you.
PS: im running code on colab

From your screenshot, the vectorized version : loop version ~ 2.72 : 521.16 ~ 1:191, which is a >190 times difference. It is pretty significant, isn’t it?

sorry just to clarify cause I didnt make it clear but im asking about the print(c) outputs of both verctorized and for loop multiplication which have very small difference

Yes, that is because of different rounding behavior in the two cases. We are doing 64 bit floating point here and the resolution of that is on the order of 10^{-17} in the mantissa. If you convert the two numbers to scientific notation, you’ll see that the error is in the 14th significant digit.

1 Like

thanks for this wonderful clarification!

No worries @jgcanete :wink: Your doubt get cleared up is all that matters.

1 Like