# DLS C1 W2 Exercice 5

Hi,
I have a problem in the function propagate(w, b, X, Y) for calculating the cost. In my code I use np.dot and an error is raised informing me of a problem of ‘shapes’ which are of the form (3, 1) and (3, 1). I do not understand why.
However I managed to solve my problem by converting the matrices (3, 1) into vectors (3,). But that’s not satisfactory and I’m sure there’s a simpler solution.
Here is an example:

``````x = np.array([[1, 2, 3]])
y = np.array([[4, 5, 6]])
print(x.shape)
print(y.shape)
# d = np.dot(x, y)    # ERROR with shapes
# print(f"d={d}")
x_v = x[0]
y_v = y[0]
print(x_v.shape)
print(y_v.shape)
d = np.dot(x_v, y_v)  # NO ERROR
print(f"d={d}")
``````
``````(1, 3)
(1, 3)
(3,)
(3,)
d=32
``````

Regards,

Pierre (from France)

I use np.dot and an error is raised informing me of a problem of ‘shapes’ which are of the form (3, 1) and (3, 1). I do not understand why.

Dot product works for two matrices` (n,m), (m,s)` to create` (n,s)` matrix. In your case, you need to transpose either like `np.dot(x, y.T)`

OK, if `x.shape=(3, 1)` and `y.shape=(3, 1)` the dot product can be calculated with:

``````np.dot(x, y.T)
``````

But the result is not a number, it is a (1, 1) matrix.
Maybe it’s not important and Python/Numpy identifies scalar and matrix (1, 1). I do not know.

Regards,

Pierre

You should understand that a dot product you selected is a matrix operation. The result should be a matrix.
Understanding the difference between a scalar and a vector/array is quite important. There will be some exercises to check the type of variable. If you mix up, then you will fail all.
You will use several transformation operations like squeeze, cast, and others to get required type.

If you are new to Python, this may be a good starting point. This is for MLS learners, but should be accessible by anyone.

Hi, Pierre Bejian.

Yes, Nobu Asai is very right. We implement ‘np.dot’ to do both the multiplication and the sum in a single operation. In order to make it work, we need a transpose where the inner dimensions have to agree its rules. So, how we do that? We do 1x3 dotted with 3x1 to give a 1x1 output or nxm dotted with mxs to give nxs. Right!

Just through once and you will figure out, what is happening here.