Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

complex eigen values in PCA calculation

Iam trying to calculate PCA of a matrix.

Sometimes the resulting eigen values/vectors are complex values so when trying to project a point to a lower dimension plan by multiplying the eigen vector matrix with the point coordinates i get the following Warning

ComplexWarning: Casting complex values to real discards the imaginary part

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

The whole code i used to calculate 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)
like image 240
Ahmed Kotb Avatar asked May 02 '12 19:05

Ahmed Kotb


1 Answers

The covariance matrix is symmetric, and thus has real eigenvalues. You may see a small imaginary part in some eigenvalues due to numerical error. The imaginary parts can generally be ignored.

like image 142
Michael J. Barber Avatar answered Sep 22 '22 22:09

Michael J. Barber