I'm getting an error in shapes when computing the cost

I’m working on W2_A2_E5
and I wrote this to compute the cost :
cost = (-1/m) * (np.dot(Y,np.log(A)) + np.dot((1-Y),np.log(1-A)))

but I’m getting this error:
shapes (1,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)

can someone help me please I couldn’t understand the error

Check the shapes of the matrices, Y and Np.log(A) and the second pair, it seems the matrix multiplication cant be achieved because of that.

the shapes of both A and Y are (1,3)

You need to transpose one term.

1 Like

Hey Saif! I have the same question. Both A and Y have the same dimensions. So, why should I transpose it and calculate?

The following code runs properly using the np.dot() function:

import numpy as np
A =  np.array([[1., 2]])

B = np.array([1., -2.])
print(np.dot(A,B))

It outputs -3 which is the correct output.
Apologies for reviving the topic, but I have the same question, so I thought your understanding on this problem can help me better.
Thank you!!

np.dot() is designed for matrices.

A dot product requires that the number of columns in the 1st matrix equal the number of rows in the 2nd matrix.

That’s how a matrix dot product is defined.

So if you have two vectors of the same shape, and you want to compute their dot product, you have to transpose one of them.