C1W4-Assignment - Exercise 3

Hi,
Can somebody help me debug exercise 3 error as shown below?
ValueError: cannot reshape array of size 4096 into shape (55,4096)
I can DM my script but I this error is without using np.repeat(), but I still get an error when I add np.repeat. I have been trying to look through the question and answers for this exercise for 3 days but I couldn’t find anything close to my error.

hi @Lakelife04

kindly post a screenshot of your complete error without posting any codes.

you probably are not using the input data correctly for mean matrix.

the arguments states y is the input data with shape ( n_observations and n_pixels.

So when you are calculating the mean matrix, it tells you to use np.repeat to repeat the mean matrix n_observations times. So using Y isn’t enough, use Y in terms of shape position of n_observatiobs which will index. so it would mean_matrix, Y.shape[0], apply this using np.repeat()

The error says that the array of of size 4096 cannot be shaped (55,4096), which makes sense! The size of your first arra neeeds be much larger something like 55*4096…
I dont have access to this course but you can send me the code in private and I can have a look at it if I find any problems!

One potential cause of dimension mismatch errors would be that you have hard-coded the dimensions of the objects in some way. That is almost always a mistake, unless you have no other choice or the explicitly tell you to do it that way. The point is that the code should work with any sized input objects, not just the sizes of the test case that they happen to give you here.

This is the error message. @gent.spah how does private messages work? I haven’t used it before

in your mean_matrix first code calculation you have Y[0] instead of Y.shape[0]

next while reshaping, you are suppose to mean_matrix.reshape instead of np.reshape(mean_matrix) as the instructions mentions use np.reshape to reshape the repeated values into a matrix of shape the same size as your input data. Although both function gets you the same value, the difference would be syntactic and semantics based on the metadata used behind these codes.

Also make sure to cross check if you used Y.mean to calculate the mean_vector, and used the correct axis argument as per instructions given before the exercise.

1 Like

In addition to Deepti’s points, note that they make this more complicated than it really needs to be. You don’t need to repeat and then reshape anything. You can just directly subtract the mean vector from the original matrix and “broadcasting” kicks in and gives you the correct answer.

1 Like

Thank you both @Deepti_Prasad & @paulinpaloalto I tried both your methods and they worked. I see what you mean now @paulinpaloalto as the mean_matrix method is unnecessarily long.

2 Likes

Pun intended I hope! :laughing: :nerd_face:

Now that I think of it, the word “mean” has quite a few different meanings :scream_cat: