Course 1, Week 2, Ex 5

In addition to Ken’s excellent explanation, it’s also worth calling attention to the significance of the square brackets in those declarations of X and Y. You need to learn to focus on those. You can also run your own experiments and use the “shape” attribute of numpy arrays to see the resulting dimensions.

Here’s a way to concretely see what Ken pointed out about the shapes:

X = np.array([[1., -2., -1.], [3., 0.5, -3.2]])
Y = np.array([[1, 1, 0]])
print(f"X.shape = {X.shape}")
print(f"Y.shape = {Y.shape}")

Running that gives this:

X.shape = (2, 3)
Y.shape = (1, 3)

Because there are two nested sets of square brackets, both arrays end up having two dimensions. Now watch the difference if I only use one set of brackets to define Y:

Y = np.array([1, 1, 0])
print(f"Y.shape = {Y.shape}")
Y.shape = (3,)

So the shape ends up having only 1 element, which means that Y in that case has only one dimension. Prof Ng discusses the difference between 1D and 2D arrays briefly in the lectures, but then points out that we only use 2D arrays for everything in these courses.

As just one more experiment, let’s see what it would look like to create a 3 x 2 array instead:

Z = np.array([[1., -2.], [-1., 3.], [0.5, -3.2]])
print(f"Z.shape = {Z.shape}")
Z.shape = (3, 2)

So we end up with the same values, but in a different shape. It’s all determined by the placement of the brackets.

2 Likes