【问题标题】:complex eigen values in PCA calculationPCA 计算中的复特征值
【发布时间】:2012-05-12 08:01:53
【问题描述】:

我正在尝试计算矩阵的 PCA。

有时得到的特征值/向量是复值,因此当尝试通过将特征向量矩阵与点坐标相乘来将点投影到低维计划时,我会收到以下警告

ComplexWarning: Casting complex values to real discards the imaginary part

在那行代码np.dot(self.u[0:components,:],vector)

我用来计算 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)

【问题讨论】:

    标签: python numpy pca eigenvector eigenvalue


    【解决方案1】:

    协方差矩阵是对称的,因此具有实特征值。由于数值误差,您可能会在某些特征值中看到一个小的虚部。虚部一般可以忽略。

    【讨论】:

    • @Micael J. Barber 什么样的数值错误会导致这种异常?
    【解决方案2】:

    您可以使用 scikits python 库进行 PCA,这是一个 example 如何使用它

    【讨论】:

      猜你喜欢
      • 2014-05-02
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多