Complex eigenvalues ​​in calculating ATP

I am trying to calculate the PCA matrix.

Sometimes the resulting eigenvalues ​​/ vectors are complex values, so when I try to project a point onto a lower dimensional plan, multiplying the matrix of eigenvectors by the coordinates of the point, I get the following warning

ComplexWarning: Casting complex values to real discards the imaginary part

In this line of code np.dot(self.u[0:components,:],vector)

All the code I used to calculate the PCA

import numpy as np
import numpy.linalg as la

class PCA:
    def __init__(self,inputData):
        data = inputData.copy()
        #m = no of points
        #n = no of features per point
        self.m = data.shape[0]
        self.n = data.shape[1]
        #mean center the data
        data -= np.mean(data,axis=0)

        # calculate the covariance matrix
        c = np.cov(data, rowvar=0)

        # get the eigenvalues/eigenvectors of c
        eval, evec = la.eig(c)
        # u = eigen vectors (transposed)
        self.u = evec.transpose()

    def getPCA(self,vector,components):
        if components > self.n:
            raise Exception("components must be > 0 and <= n")
        return np.dot(self.u[0:components,:],vector)
+3
source share
2 answers

The covariance matrix is ​​symmetric and, therefore, has real eigenvalues. Due to a numerical error, you can see a small imaginary part in some eigenvalues. Imaginary parts can usually be ignored.

+7
source

python scikits PCA, example ,

+2

All Articles