Hi all,
I am working on Week 3 assignment for NLP. I have got all tests passed for get country and get accuracy but in grades I got zero. Also, I am unable to workaround error for computation of PCA. Any idea how to proceed? Thanks.
Hi all,
I am working on Week 3 assignment for NLP. I have got all tests passed for get country and get accuracy but in grades I got zero. Also, I am unable to workaround error for computation of PCA. Any idea how to proceed? Thanks.
Let us know what you find out?
HI, I was able to solve the issue, turns out I was using global variable word_embeddings. Instead I should have used embeddings which was parameter in the function. Seems to be a pretty dumb mistake from my side.
Anyway, I am still stuck in Compute PCA section. I have already passed but I would like to know where I am wrong in the last section. I think you have completed that so can you help me?
Hey @Rakshat_Ranjan,
Welcome, and we are glad that your could be a part of our community First of all, you have posted this topic in the wrong category. Let me correct that for you. You can use the little pencil icon next to the title, if needed in future. Now, for the compute_pca
function, can you please share your error stack with us?
Cheers,
Elemento
Hi Elemento,
APologies for inconvenience, and let me know which section should I post in.
For the error, I am trying different ways to get cross product of:
{Moderator’s Edit: Solution Code Removed}
error receiving either
ValueError: incompatible dimensions for cross product
(dimension must be 2 or 3)
or
ValueError: shape mismatch: objects cannot be broadcast to a single shape
Hey @Rakshat_Ranjan,
No worries at all, I have corrected that. As for the error, by any chance, did you miss out the comment just above where you have to write this code. Let me mention here for your reference.
# transform the data by multiplying the transpose of the eigenvectors with the transpose of the de-meaned data
# Then take the transpose of that product.
These instructions tell us step-by-step, which variables to take, whether to take their transpose or not, and so on. By the way, I didn’t know that a method like cross
existed in numpy
. For your reference, you will find that np.matmul()
and np.dot()
will be enough for all your needs throughout this specialization. Let us know if this helps.
P.S. - For taking transpose of some variable x
, a more concise form of writing np.transpose(x)
is x.T
.
Cheers,
Elemento
Hi Elemento,
Thanks for correcting the section for the post.
I didnt know about matmul, I used cross instead. Using matmul, helps in getting error part resolved.
However, I am getting my tests failed, and I know why. There is dimensions mismatch.
My output gives 10x2 matrix, instead it should give 3x2 matrix.
This means I need to eliminate 10 of 3, but for that my eigen vectors matrix should have 10 dimensions, but instead it is giving 3. I dont understand where I am going wrong.
Summary:
X_demeaned: 3x10
Covariance matrix: 3x3
Eigen_vecs: 3x3
Matmul: 10x2
Output:2x10
Expected Output: 3x2
Hey @Rakshat_Ranjan,
I believe your covariance matrix is incorrect. There are 10
features in X
corresponding to the test case, next to the code cell. For this, don’t you think the covariance matrix should be a 10 x 10
matrix, since the covariance matrix tells us the covariance between every 2 possible features based on all the samples?
I believe you haven’t defined the correct value for rowvar
. Give it a thought as to what should be the correct value for it. Off-course, it’s a boolean parameter, so, it’s the opposite from what you have written now, but make sure you understand why it is the other one. I suggest you to read the markdown prior to the code cell once again. Let us know if this helps.
Cheers,
Elemento
Hi Elemento,
I am using np.cov formula for covariance matrix. This gives me 3x3 when I take covariance of X_demeaned; but gives me 10x10 when I take covariance of X_demeaned.Transpose.
Please help which one is right, I understand the there is some issue with cov matrix only.
Okay, so rowvar default value is true, so I mentioned false. This gave me desired output in terms of correct shape, but now I cant get correct values.
Hey @Rakshat_Ranjan,
I am assuming you have understood as to why rowvar
should be False
. As I mentioned before, you can take a look at the markdown. As to incorrect values, can you please share the error stack with us, so that we have something to begin with.
Cheers,
Elemento
There is no error, the output is different than expected:
Your original matrix was (3, 10) and it became:
[[ 0.04960136 -0.13858967]
[ 0.25766933 -0.24819667]
[-0.10781832 -0.4321358 ]]
Expected:
Your original matrix was: (3,10) and it became:
0.43437323 | 0.49820384 |
---|---|
0.42077249 | -0.50351448 |
-0.85514571 | 0.00531064 |
Hey @Rakshat_Ranjan,
Can you please DM me your implementation of compute_pca
, so that we can try to figure out the issue? For DM, click on my name and select “Message”.
Cheers,
Elemento
Hey @Rakshat_Ranjan,
The error (in this case as well) rises because of your interpretability of the different dimensions. In this case, you have taken the mean of the data along the wrong axis.
In this implementation, X
(which has been passed as the input) has word vectors as it’s rows, and the distinct features of the word vectors as it’s columns. Now, the key-point is that the mean needs to be taken for the different features (i.e., columns), and not for the rows. And if you give it a thought, taking the mean of the individual word vectors and subtracting that from X
doesn’t make any sense, since in that case, all the word vectors will have a 0-mean.
Once again, I urge you to read the instructions (in the markdown) carefully, before proceeding on implementing the functions. This way, you will be able to avoid most of the typical errors. I hope this resolves your query.
Cheers,
Elemento
Hey @Elemento
Could you please help me, I have a similar problem, but I checked both the dimension and the optional values, but two tests still fail
Thanks,
Igor
Hey @Igor_Pereverzev,
Welcome, and we are glad that you could be a part of our community Can you please share your error stack with us, so that, we can try to figure out the issue?
Cheers,
Elemento
Hey @Igor_Pereverzev,
If you take a look at the documentation of numpy.linalg.eigh
here, you will find the following mentioned:
The column
v[:, i]
is the normalized eigenvector corresponding to the eigenvaluew[i]
. Will return a matrix object if a is a matrix object.
This indicates that the eigen_vecs
which you have obtained contains the eigen-vectors as it’s columns, and not as it’s rows. With this information, recheck the step in which you have obtained eigen_vecs_sorted
from eigen_vecs
. Let us know if this helps you.
Cheers,
Elemento
I’d done the exact same thing! took me lots of time thank you for sharing your opinion
Many thanks for this explanation, it finally helped me resolve the mystery of incorrect values after transformation!