Sign difference in eigenvectors taken form Matlab and Python

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 21:50:54

问题


Could you please explain why there is a sign difference in some of the eigenvectors (2-4)? Does this difference affect further calculation in further calculations, e.g dimensionality reduction?

Matlab:

N = 5000;
dataA = rand(N,5);
covA = cov(dataA);
%covA = dataA*dataA'/(length(dataA)-1);
covA = covA + eps.*eye(size(covA));
[~,pA] = chol(covA);
assert(pA==0,'A is not possitive definite')

dataB = rand(N,5);
covB = cov(dataB);
%covB = dataB*dataB'/(length(dataB)-1);
covB = covB + eps.*eye(size(covB));
[~,pB] = chol(covB);
assert(pB==0,'B is not possitive definite')

[V,D] = eig(covA, covB);

V =

   -0.4241   -1.0891    1.8175    2.4067   -1.3032
    1.4445   -1.8960   -1.4118   -0.6514   -2.0075
    0.1214   -2.5039    0.3332   -0.1705    2.3609
   -2.1235   -0.7007    1.1632   -2.1532   -1.0554
   -2.2599   -0.4405   -2.2236    1.2545    0.0760

Python:

from scipy.linalg import eigh
import scipy.io
import numpy as np

cov_mat = scipy.io.loadmat('cov.mat')
covA = cov_mat['covA']
covB = cov_mat['covB']
eigvals, eigvecs = eigh(covA, covB, eigvals_only=False)

np.savetxt('eigvals.txt', eigvals, fmt='%.4f')
np.savetxt('eigvecs.txt', eigvecs, fmt='%.4f')

eigvecs =

   0.4241 1.0891 -1.8175 -2.4067 -1.3032
  -1.4445 1.8960  1.4118  0.6514 -2.0075
  -0.1214 2.5039 -0.3332  0.1705  2.3609
   2.1235 0.7007 -1.1632  2.1532 -1.0554
   2.2599 0.4405  2.2236 -1.2545  0.0760

回答1:


They are the same eigenvectors, as flipping the signs on eigenvectors does not change their formulation - they will have the same eigenvalue. It will not affect further calculations as a result.

Why are they calculated differently? Most likely because the subroutines that are run vary based on the matrix that is being operated on, and don't care what 'sign' they return because eigenvalues don't have one.


A trivial mathematical proof:

If x is an eigenvector of matrix A with eigenvalue q, then by definition we have that Ax = qx.

It follows that A(-x) = -(Ax) = -(qx) = q(-x) such that -x is an eigenvector with the same eigenvalue.



来源:https://stackoverflow.com/questions/35429459/sign-difference-in-eigenvectors-taken-form-matlab-and-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!