complex eigen values in PCA calculation
Asked Answered
T

2

6

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)
Thready answered 2/5, 2012 at 19:49 Comment(0)
S
14

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.

Selfpossessed answered 5/5, 2012 at 14:38 Comment(1)
@Micael J. Barber what kind of numerical errors can lead to such anomaly?Trost
H
1

You can use scikits python library for PCA, this is an example of how to use it

Hebetude answered 5/5, 2012 at 14:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.