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

calculate meaned data by X_mean = X - X.mean(axis = 0)

calculate the covariance matrix: M_cov = X_mean.T.dot(X_mean) / X_mean.shape[0]

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
Save
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.

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.

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!

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.

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?