C1_W3 assignment compute_pca got complexed-value eigenvectors

When computing the PCA, I always got complexed-value eigenvectors and the direction of the eigenvector is reversed so i cannot pass the test. Can anyone help? Thanks in advanced.

below is (pseudo?) code I used to compute eigenvectors for data X

  1. calculate meaned data by X_mean = X - X.mean(axis = 0)
  2. calculate the covariance matrix: M_cov = X_mean.T.dot(X_mean) / X_mean.shape[0]
  3. eigenvalues, eigenvectors = np.linalg.eig(M_cov)

Below is output from grader.

Wrong accuracy output.
Expected: [[ 0.43437323 0.49820384]
[ 0.42077249 -0.50351448]
[-0.85514571 0.00531064]].
Got: [[-0.43437323+0.j -0.49820384+0.j]
[-0.42077249+0.j 0.50351448+0.j]
[ 0.85514571+0.j -0.00531064+0.j]].

I used np.cov to compute the covariance matrix and did not have this problem. :nerd_face:

I also used np.linalg.eigh, not np.linalg.eig. That was mentioned in the instructions FWIW …

In C1_W3 assignment I got Grader Error: Grader feedback not found despite that all tests passed, and all of my code is correct.

Hello @Mariam_Mohammed_Hame

Always created a new topic when you encounter any issue or raising any query or doubt.

Your issue could be related to code, or renaming your notebook, or doing assignment on older version, or so make sure you have revised your codes once again and not edited any of the code cell or added any codes where it is not required.

To confirm from us, one needs to check your codes.


That type of error usually means there is something structural that is wrong with your notebook. But there is one easy experiment to try first:

Kernel -> Restart and Clear Output
Submit again

Please try that and let us know if it helps. The point is that the grader does not need to see your output and sometimes the output can contain syntax that confuses the grader.

If it still fails in the same way after that, then I suggest you start with a fresh copy. Rename your current notebook to save your work and then do the “Get a Clean Copy” procedure. Then carefully “copy/paste” just your code from the “YOUR CODE HERE” segments over to the clean copy of the notebook and submit that.

Let us know if that helps.

1 Like

I get an error when I try to do (X - X.mean(axis=1))
ValueError: operands could not be broadcast together with shapes (3,10) (3,)

If I do it on X - X.mean(axis=0) it works but I get 4/6 correct so 2 of the check are wrong. Can you be more specific on how to do the mean on axis=1. I’m not very familiar with the numpy library yet.

Sorry it’s been a while since I looked at that function in detail. You do want the mean across the rows, since the rows are the samples. So you are taking the mean using axis = 0 to get the mean of the columns (the features). So you should end up with a mean vector of dimension (10,), which means it is a 1D vector. (In my previous post from 15 days ago I forgot to notice that I had done the transpose of X first. I’ve deleted that misleading part of the post. Sorry!)

Then when you subtract (3, 10) - (10,) that should work with “broadcasting”. But then subtracting (3,10) - (3,) should also work because of “broadcasting”, but will give the wrong answer. So I’m not sure what is going on with that error.

If you use axis = 0, that is correct for the “demeaning” stage. If you then get the wrong answer later, my guess is that something is wrong with either your covariance calculation or how you handle the eigenvalues and the final reduction of the matrix. There are quite a few moving parts there. Make sure to use np.cov for the covariance and np.linalg.eigh for the eigenvalues.

Thank you so much for your prompt reply.

I read a few posts and got the below calculations:

{moderator edit - solution code removed}

I then use the argsort and reverse it using -1

{moderator edit - solution code removed}

I then do this:

{moderator edit - solution code removed}

both of which give me the same mostly correct answer.

I’ve tried to be conscientious and determine where I am making a mistake but I just don’t see it.

That all looks and sounds correct. Maybe it’s time to look at your full notebook to see if I can spot the difference. We aren’t supposed to show code in a public way. I will send you a DM (Direct Message) about how to share the code privately.

To close the loop on the public thread, everything was correct except one key line of code: when you sort the eigenvectors, remember that they are the columns of the eigen_vecs matrix, not the rows. Check the documentation for np.linalg.eigh to confirm!

Could you actually share the screenshot of the error you are getting???

In future, try to create a fresh new topic whenever you have issue, query or doubt.


The problem has been solved. The “expected values” were all the correct shapes, but the values were incorrect. The reason was what I described in my most recent previous reply on this thread.

1 Like

Hi i passed all test cells in programming assignment as shown below but when i submit the notebook i got 0 in two cells tests even it display on test cell function that i passed how to solve this

Passing the tests in the notebook does not prove your code is perfect.
The notebook’s tests just cover some fundamentals, and the grader uses entirely different tests.

The first things to check are: Does your code use any global variables incorrectly, and does your code use fixed index values where it should use a variable?

Can you share screenshot of the submission grader output where it shows why you failed the assignment

This has already been solved on a parallel thread.