W2_A2_Ex-2_Flattening of the images

I had
problem understanding the method of reshaping the given matrix. I thought that we have to convert each image to a vector of dimension (num_pxnum_px3,1) and m_train examples mean a matrix of (num_pxnum_px3,m_train), so I did that but was not getting the correct answer. Could you please shed light on both the things.
I have attached the image for my code snippet.

I actually think your method of flattening should work, cause I tried it too, but maybe the test checks that you have used the method recommend above that cell, which includes transposing.
See picture below;

1 Like

Good evening, I also had a question on this.

What’s the point of the assert lines of code? The first 10 are not the same doe

Hello everyone,

Here’s a brilliant thread that could actually shed some light related to your queries.

The assert statement in python is frequently used for testing correctness of code. It checks that a certain condition is true and if not then it throws an exception. In this particular case, the assert is checking 10 particular pixel values in two of the different unrolled images: one from the training set and one from the test set. If your code is not correct, then the pixel values will not match, which is what happened in the case of the original post on this thread. They didn’t use the “transpose” and that causes the results to be incorrect. If you read the thread that Rashmi linked, it will explain why the transpose is required.

Failed to flatten, l used the recommended trick!

I agree that your code looks right, but the answers do not. The only thing I can think of is that you modified the inputs somehow. Are you sure you didn’t make any changes to train_set_x_orig before this cell?

I haven’t edited it , Can l edit the data since its being loaded from an external libarary
*from lr_utils import load_dataset

Sure, you can write assignment statements that have train_set_x_orig on the LHS of the assignment. But if you have not done that, then that’s not the problem.

You can also modify the code for load_data by opening the file lr_utils.py, but presumably you did not do that.

Ok, we need to think just a little bit harder about this. Here are the assertions in that code block that check your outputs:

# Check that the first 10 pixels of the second image are in the correct place
assert np.alltrue(train_set_x_flatten[0:10, 1] == [196, 192, 190, 193, 186, 182, 188, 179, 174, 213]), "Wrong solution. Use (X.shape[0], -1).T."
assert np.alltrue(test_set_x_flatten[0:10, 1] == [115, 110, 111, 137, 129, 129, 155, 146, 145, 159]), "Wrong solution. Use (X.shape[0], -1).T."

Notice that you simply printed the whole array with no indexing. But what the assertion is showing is just the first 10 elements of the second element of the array indexed along the second dimension of the array (the index 1 in position 1).

Try this print statement instead and I’ll bet it shows the correct outputs:

print(train_set_x_flatten[0:10, 1])

Or you could just run the cell without any print statements and see if any of the assertions “throws” or not.

Ok, those values look correct as far as we can see them. So if you just run the test cases, they should pass, right?

Yea the test cases passed ,but l am still trying to figure out why am failing to flatten the train cases!

Why do you think it is failing? Please show us the error.

Here am sharing screenshot of packages and the exercise 2 results .My question is why are the train cases not flattened?

Hi @Macdaniel_Tatenda ,

You have added print statement to print the content of train_set_x_flatten, instead of the shape. If you change your print statement to print the shape, it should show the flattened shape.

Thank you it helped l didn’t notice ,almost spend the whole day yesterday.

That can happen to anyone. Glad to hear you are moving forward.

Also notice that those print statements were just given to you as part of the template code, so you must have modified them. Generally speaking, you should only need to change the code in the sections with the comments about “YOUR CODE HERE”. It’s not illegal to change other parts of the code, but you need to make sure you know what you are doing when you “go there”. Or you can end up wasting lots of time :nerd_face:

If at any point you want to get a clean copy of one of the assignments to see what it originally looked like, the first topic on the DLS FAQ Thread shows how to do that.

1 Like

only integer scalar arrays can be converted to a scalar index - I get this error when doing following - can anyone tell what I am doing wrong?