W2_A2_Ex-5_ValueError

Hi,

I am stuck on the assignment problem (exercise 5, specifically the cost function) and keep getting the following error message:

ValueError: shapes (1,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)

My dimensions are the same (1,3) for both A and Y, so I’m not sure why I’m getting this error.

Thanks!

It sounds like you used the dot product to compute the cost. That’s a perfectly fine way to do it, but you need to understand the rules for dot products: the “inner” dimensions need to match, right? The dot product here is just a special case of matrix multiply. In fact np.dot implements full matrix multiply if you check the documentation. But the same “inner” dimensions rule applies in all cases.

If you are new to linear algebra, you might want to spend some time looking at other online educational resources on that. It is assumed as a prerequisite here that you are comfortable with the fundamentals of Linear Algebra: matrices and vectors and operations on them, including things like transpose (hint for this case). You don’t need to know what a determinant or eigenvalue is, but you need to be familiar with basic algebraic operations on matrices and vectors.

Note that there is also another valid way to compute the cost: do “elementwise” multiply of the two vectors (np.multiply or the overloaded “*” operator) and then do a sum (np.sum). The dot product method is more efficient, but either way works. The grader doesn’t care how you write the code, as long as it gives the correct answer.