Question on Exercise 5 Week 5 Programming Assignment

You don’t need dot products to compute the cost. You can use them, but if you do, then you need to be cognizant of the rules for dot products: the inner dimensions need to agree, right? So if you have two (1,3) vectors, then you need to transpose one of them. And the order of the operation matters: please have a look at this thread to understand why.

But if you use np.dot, then the advantage of that is that it does both the multiplication and the addition in one shot. So you don’t need the np.sum in that case. And you never need np.dot to multiply by a constant like -\frac {1}{m}.

The other way to solve the cost is to use elementwise multiply (“*”) between the pairs of vectors and then use np.sum to add up all the products. That might be more straightforward and that way does not require any transposes.

4 Likes