I am facing a little trouble with the lab assignment for Week 4. Specifically exercise 3 on the center_data function.
So I filled out the function and it seems to work because the cat picture in the code block right below generates correctly. However, the unit tester says 1 test passed and the other failed and the grader gives me a zero for the exercise.
I am suspicious that it is not the same cat though, and I don’t know if that makes a difference or if all of the operations we have done up to then have scrambled the order of the images.
Here is what I have so far for the function:
# Graded cell
def center_data(Y):
"""
Center your original data
Args:
Y (ndarray): input data. Shape (n_observations x n_pixels)
Outputs:
X (ndarray): centered data
"""
### START CODE HERE ###
# mentor edit: code removed
### END CODE HERE ###
return X
Also note that they make things a lot more complicated than they need to be in the instructions for this section. If you just use keepdims = True when you compute the mean as a vector, then don’t need to use repeat and reshape: just take advantage of “broadcasting” when you subtract the mean to get the final “centered” data.
It’s great news that you found the solution based on those hints!
The “meta” lesson here is that when you’re just 100% sure that your code is right but it still fails, it’s always worth just taking a couple of deep calming breaths and then having a careful look at the instructions again with the thought in mind that maybe you missed something the first time through.
I have tried this both the way it states in the instructions and the simplified approach suggested by Paul and both give me the same return matrix which has the same shape as the input matrix: imgs_flatten. I get an error saying the expected values were not received. I have checked the mean results on a smaller matrix and the math works. I subtract the now mean_vector from the passed in matrix to center the data. It all seems to work!?!
Test case “default_check”. Wrong output on the center_data function.
Expected: [[ 33.8 33.34545455 35.87272727 … -59.83636364
-48.72727273 -38.61818182]
[ 18.8 24.34545455 36.87272727 … 67.16363636
67.27272727 64.38181818]
[ 89.8 71.34545455 53.87272727 … -42.83636364
-15.72727273 19.38181818]
…
[ -11.2 -0.65454545 3.87272727 … -31.83636364
-30.72727273 -33.61818182]
[ 80.8 78.34545455 73.87272727 … -2.83636364
10.27272727 -8.61818182]
[ -99.2 -91.65454545 -97.12727273 … -116.83636364
-120.72727273 -113.61818182]].
Got: [[ 38.78344727 34.78344727 40.78344727 … -21.21655273 -10.21655273
-0.21655273]
[-66.02026367 -64.02026367 -48.02026367 … 15.97973633 15.97973633
12.97973633]
[ 68.83300781 46.83300781 32.83300781 … -30.16699219 -3.16699219
31.83300781]
…
[-44.5793457 -37.5793457 -29.5793457 … -31.5793457 -30.5793457
-33.5793457 ]
[ 97.12231445 91.12231445 90.12231445 … 47.12231445 60.12231445
41.12231445]
[-63.47436523 -59.47436523 -61.47436523 … -47.47436523 -51.47436523
-44.47436523]].
1 Tests passed
1 Tests failed
If you do it the simplified way that I suggested, using “broadcasting”, then there really aren’t very many moving parts here: you compute the mean and then you subtract. So the only thing that I can think of off hand is that you are computing the mean incorrectly. The only variable of importance there is the axis on which you compute the mean.
Yes, I tried making that mistake and I get exactly the same incorrect values that you show. The point is that you want to compute the mean of the pixels, right? The instructions are pretty clear that those are the columns, so the only question is the semantics of the axis parameter to np.mean.
Ok, I just read the docpage for numpy mean and the way they say it is a bit ambiguous or confusing to me at least. They say the axis parameter selects the axis “along which” you are computing the mean. So to me, saying you compute the mean “along the rows” would mean you end up with the mean of each row. But what they really do when you say axis = 0 is compute the mean across the rows, giving you the mean of each column.
So I claim their wording is misleading. I think it should say “across which” not “along which”.
But you can run the experiment yourself and see what happens. The clear way to see this is to start with a matrix that has different row and column dimensions.
this helped thank you. I had my thinking flipped. with images as variables and each pixel as an observation. Not sure why I was thinking like that… the data is not even structured like that. I wish I could claim the ambiguity in the documentation was the reason for the misunderstanding. I think I have working to hard. Anyway on to the next cell!! thanks again.