0

I have a matrix X of which, upon centering, I perform SVD. While I get the same magnitude of the loadings, some of the columns produce opposite signs between MATLAB and Python. I understand the opposite signs do not matter as far as the PCA is concerned, however, I need to perform summation of the loading values later on. This causes difference in the results between MATLAB and Python. Therefore, I need same signs of loadings across both MATLAB and Python. What could I do to get the same signs in both the platforms? I am using np.linalg.svd() function in Python and simple svd() function in MATLAB. Please help.

Intriguingly, when X is not centered, I get same signs. I tried sklearn.decomposition.PCA() function in Python as well but to no avail.

  • Please provide enough code so others can better understand or reproduce the problem. – Community May 03 '23 at 22:03
  • You can, for example, flip the vector if the first component is negative. That way you can enforce the same signs in both code bases. – Cris Luengo May 03 '23 at 22:15
  • The problem is I assume that I "wouldn't" know which columns have opposite signs between MATLAB and Python. Most of the columns have the same sign. Flipping was ideal if I knew "in advance" which columns to flip. – Shafa'at Moosavi May 03 '23 at 22:23
  • Flip the vector **if the first component is negative**. Do that in both code bases, and both code bases will have the same signs. – Cris Luengo May 03 '23 at 22:39
  • You might need to special-case for the first component being zero, since that doesn't have a sign. It being identical to zero is highly unlikely, but everything is possible, and you might want to take that into account. – Cris Luengo May 03 '23 at 22:40
  • That is an interesting solution and should work. But then again, there is another issue - I am translating MATLAB code into Python and I CANNOT tamper with the MATLAB code. I am highly restrained here. – Shafa'at Moosavi May 03 '23 at 22:53

0 Answers0