Wo2 assignment forward and backward prop

Hello! I might have a silly question but I am new to ML. I dont understand why in the code (w2) it is written that m=X.shape[1].

m are the m training examples?
why we use [1] in the shape? Is it because X has shape (nx,m)

Exactly! The shape attribute of a numpy array is a “tuple” or “list” of values giving the size of each dimension. You can index into the tuple to select the dimension you want. Because indexing is “0-based” in python, the first element is shape[0] (the number of rows for a 2D array) and the second is shape[1] (the number of columns for a 2D array). The way Prof Ng formats the input “sample” matrix X, there are nx rows (the number of input features in each sample) and m columns (the number of independent input samples).

1 Like

Thank you for your help!

@LilandiGeo ,
Adding to @paulinpaloalto excellent answer, another viewpoint is

  1. represent a single input as column vector with n dimensions/features. ([n, 1])
  2. Now, if multiple inputs(say m) are stacked parallel vertically, it results in a [n, m] matrix.

Now, np.shape will return a tuple (n, m).

To access m in a zero-indexed tuple, we need to use index value as 1

2 Likes