Week2_Assignment1_ Logistic_Regression_with_a_Neural_Network_mindset

I have a question from part 2. Overview of the Problem set. I didn’t get how reading the dataset (this line) is works.
index = 25
print ("y = " + str(train_set_y[:, index]) + “, it’s a '” + classes[np.squeeze(train_set_y[:, index])].decode(“utf-8”) + “’ picture.”)
Especially I didn’t get train_set_y[:, index], and the decode process. I appreciate if you can give me some explanation about that. Thanks.

The way indexing works in python, you can specify a “range” with the syntax “start:end:step” or “start:end” if the step is 1. And if you just leave out start and end and have “:” it means “take all possible values in this index position”.

So if I have an array called myArray with two dimensions and I say:

out = myArray[:, 42]

That’s saying I want all rows of the 43rd column of myArray, so the result is a column vector. Note that the 43rd is because indexing in python is “zero based”.

So train_set_y[:, index] is just the label for the sample selected by index. Then you need to understand what the classes array is. You can dump it out, but it’s a list of string names. The decode method just tells it how to format the string.

And you need the np.squeeze there because you are using the value of the label (either 0 or 1) as an index into an array. If you don’t do the squeeze, you’ve got a 2D array and that doesn’t make sense as an index.

Thank you so much. Now, it makes sense.