3

I've a matrix x (1000*25) that contains random floats in the interval (-5,5). nFeatures=25 and nPoints=1000. I'm using this code to find the eigenvalues of the covariance matrix, but I'm getting negative eigenvalues.

#centering the data
for i in range(0,nFeatures):
    sum=0
    for j in range(0,nPoints):
        sum+=x[j][i]
    for j in range(0,nPoints):
        x[j][i]-=sum/nPoints

#covariance matrix and its eigenvalues & eigenvectors
c=np.dot(x.T,x)
eValue,eVector=np.linalg.eig(c)
print(eValue)

I get the output to be:

[ 1.47374029e+02  8.84275505e-13 -8.01150077e-13 -6.77987718e-13
  5.19228948e-13 -4.01775609e-13 -3.55055652e-13  3.55433578e-13
 -2.54817200e-13  2.51137659e-13  2.23836773e-13  1.77611044e-13
  1.57643867e-13 -1.34409360e-13  1.04358065e-13 -9.31186264e-14
 -8.05736392e-14 -5.69664362e-14  4.39721071e-14  3.59268864e-14
 -2.84466680e-14  2.42670536e-14  2.30979465e-15 -7.18313504e-15
 -9.35335475e-15]

Now since my covariance matrix is a semi-positive definite matrix, I the eigenvalues shouldn't be negative. Please help me in identifying what I'm doing wrong.

Also, I read this post, and no, there are no missing values in my dataset.

Ankit Kumar
  • 1,145
  • 9
  • 30
  • This is definitely not a covariance matrix you're computing since there are no products, only sums in your operations. How about: `sum+=x[j][i]*x[i][j]` ??? – ma3oun Apr 22 '19 at 06:06
  • @ma3oun I'm not using `sum` to calculate the covariance matrix. I've used that to center the data about origin. I've used dot product, `dot(x.T,x)` to calculate cov matrix. – Ankit Kumar Apr 22 '19 at 06:10
  • 3
    your eigen values are really close to 0 (except for the first one). I think this is due to numerical imprecision (this can come from the accumulations you're using in your loop). You should consider clipping the values using their absolute values. Try replacing your centering loop with a numpy operation using np.average – ma3oun Apr 22 '19 at 06:29
  • @ma3oun I'll try it in an hour and let you know. Thanks – Ankit Kumar Apr 22 '19 at 08:45
  • Oh well thanks a lot @ma3oun , it actually helped! :D – Ankit Kumar Apr 22 '19 at 09:17

0 Answers0