主成分分析与白化,这部分很简单,当然,其实是用Matlab比较简单,要是自己写SVD分解算法,足够研究好几个月的了。下面是我自己实现的练习答案,不保证完全正确,不过结果和网站上面给出的基本一致。
1.PCA in 2D
1.1 Step 1a: Implement PCA to obtain U
u = zeros(size(x, 1)); % You need to compute this sigma = x * x' / size(x, 2); [u,s,v]=svd(sigma);
1.2 Step 1b: Compute xRot, the projection on to the eigenbasis
xRot = zeros(size(x)); % You need to compute this xRot=u'*x;
1.3 Step 2: Reduce the number of dimensions from 2 to 1.
k = 1; % Use k = 1 and project the data onto the first eigenbasis xHat = zeros(size(x)); % You need to compute this x_ap=u(:,1:k)'*x; xHat(1:k,:)=x_ap; xHat=u*xHat;
1.4 Step 3: PCA Whitening
xPCAWhite = zeros(size(x)); % You need to compute this xPCAWhite = diag(1./sqrt(diag(s) + epsilon)) * u' * x;
1.5 Step 3: ZCA Whitening
xZCAWhite = zeros(size(x)); % You need to compute this xZCAWhite=u * diag(1./sqrt(diag(s) + epsilon)) * u' * x;
2.PCA and Whitening
2.1 Step 0b: Zero-mean the data (by row)
avg = mean(x, 1); x = x - repmat(avg, size(x, 1), 1);
2.2 Step 1a: Implement PCA to obtain xRot
xRot = zeros(size(x)); % You need to compute this sigma = x * x' / size(x, 2); [U,S,V]=svd(sigma); xRot=U'*x;
2.3 Step 1b: Check your implementation of PCA
covar = zeros(size(x, 1)); % You need to compute this covar = xRot * xRot' / size(xRot, 2);
2.4 Step 2: Find k, the number of components to retain
k = 0; % Set k accordingly sum_k=0; sum=trace(S); for k=1:size(S,1) sum_k=sum_k+S(k,k); if(sum_k/sum>=0.99) %0.9 break; end end
2.5 Step 3: Implement PCA with dimension reduction
xHat = zeros(size(x)); % You need to compute this xTilde = U(:,1:k)' * x; xHat(1:k,:)=xTilde; xHat=U*xHat;
2.6 Step 4a: Implement PCA with whitening and regularisation
xPCAWhite = diag(1./sqrt(diag(S) + epsilon)) * U' * x;
2.7 Step 4b: Check your implementation of PCA whitening
covar = zeros(size(xPCAWhite, 1)); covar = xPCAWhite * xPCAWhite' / size(xPCAWhite, 2);
2.8 Step 5: Implement ZCA whitening
xZCAWhite=U * diag(1./sqrt(diag(S) + epsilon)) * U' * x;