10

私は Python を使用しており、このチュートリアルを使用して PCA を実装しました。

すべてがうまく機能し、変換に成功した共分散を取得し、問題なく元の次元に戻しました。

しかし、どうやってホワイトニングを行うのですか?固有ベクトルを固有値で割ってみました:

S, V = numpy.linalg.eig(cov)
V = V / S[:, numpy.newaxis]

Vを使用してデータを変換しましたが、これにより奇妙なデータ値が得られました。誰かがこれについて少し光を当ててくれませんか?

4

4 に答える 4

24

hereから取得したマトリックスホワイトニング用のいくつかのMatlabコードのnumpy実装を次に示します。

import numpy as np

def whiten(X,fudge=1E-18):

   # the matrix X should be observations-by-components

   # get the covariance matrix
   Xcov = np.dot(X.T,X)

   # eigenvalue decomposition of the covariance matrix
   d, V = np.linalg.eigh(Xcov)

   # a fudge factor can be used so that eigenvectors associated with
   # small eigenvalues do not get overamplified.
   D = np.diag(1. / np.sqrt(d+fudge))

   # whitening matrix
   W = np.dot(np.dot(V, D), V.T)

   # multiply by the whitening matrix
   X_white = np.dot(X, W)

   return X_white, W

SVD を使用して行列を白くすることもできます。

def svd_whiten(X):

    U, s, Vt = np.linalg.svd(X, full_matrices=False)

    # U and Vt are the singular matrices, and s contains the singular values.
    # Since the rows of both U and Vt are orthonormal vectors, then U * Vt
    # will be white
    X_white = np.dot(U, Vt)

    return X_white

2 番目の方法は少し遅くなりますが、おそらく数値的に安定しています。

于 2012-07-04T23:01:39.057 に答える
13

これに python の scikit-learn ライブラリを使用する場合は、組み込みのパラメータを設定するだけです

from sklearn.decomposition import PCA
pca = PCA(whiten=True)
whitened = pca.fit_transform(X)

ドキュメントを確認してください。

于 2016-06-28T12:55:14.947 に答える
1

V を転置して S の平方根を取る必要があると思います。式は

matrix_to_multiply_with_data = 転置 ( v ) * s^(-1/2 )

于 2011-07-06T21:54:52.610 に答える