[Moderator edit: solution code removed]
I don’t understand what is wrong with my function, even though I think I wrote the formula correctly
[Moderator edit: solution code removed]
Hi, @Patricio_Ramirez_Tor. First, be aware that posting you solution code is a violation of the honor code. It is OK, if it shows up in the traceback.
The ValueError
indicates that you have a dimensionality problem. At first glance, it suggests that you are trying to take a dot product of two misshaped arrays. The inner dimensions must agree: 3 does not equal 1, and vice versa. If you are unfamiliar with that notion, it is time to review that concept along with matrix multiplication more generally.
Note that the cost you are trying to compute is a scalar, i.e. a 1 x 1 matrix, if you will.
And yes, that means that you have not written the cost function correctly. For starters, you are trying to take the dot product of a scalar, -1/m
, with something else. There is no point to that. Please study the formula carefully, and try again. Repeat that process until you succeed.
To get you going, you may want to try something conforming to the following template:
cost = -(1/m) * (np.dot( ? , ? ) + np.dot( ? , ? ))
And, FWIW, you have a tendency to use superfluous parenthesis. Computationally, that’s OK if they are truly redundant, but it helps the readability of the code–for you and others–if there are not so many. Have at it!
Hi @kenb
Thanks for explaining this, I was having the same issue. I am now seeing the following syntax error for dw:
File “”, line 42
dw = (1/m)*np.dot(X,(A-Y).T)
^
SyntaxError: invalid syntax
P.S. I am having trouble searching through the discourse forum, is there a searchbar anywhere?
This particular line is correct. I guess the problem is in the previous line. Compiler/interpreter typically scan the pair of parenthesis and single/double quotation to the end of the line. This looks like a “close” (right) parenthesis could not be found in the previous line. So, please check one line above.
For the query, you can find a magnifier at the top right.
Hi, I tried it in the format you wrote, but it gives the same error again on the same line.
The shape of Y is (1,3) and the shape of A is (1,3) as well. For a matrix operation (dot product), the shape of the 2nd term needs to be (3,1). So, what you want to do is “transpose” A.
Thanks for the catch- I was missing a closing parenthesis in the previous line, and I appreciate the advice on querying as well. I fixed the parenthesis and modified my cost function: replacing A with A.T. This fixes the dimensionality problem, but I’m getting incorrect values for cost still:
What am I doing wrong?
It is very close, but, (1/m) should be for all terms, not just for the first term.
By the way, please remove your code. Thank you.
Thank you so much, and I removed my code, sorry about that!