6

行列の 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)
4

2 に答える 2

13

共分散行列は対称であるため、実固有値を持ちます。数値誤差により、一部の固有値に小さな虚数部分が見られる場合があります。通常、虚数部は無視できます。

于 2012-05-05T14:38:26.257 に答える
1

PCA用のscikitspythonライブラリを使用できます。これは、その使用方法の例です。

于 2012-05-05T14:32:10.143 に答える