Exercise:PCA and Whitening 代码示例
Exercise:PCA and Whitening 代码示例
练习参考PCA and Whitening,与上一个练习基本一致。
更改图像数据的均值
- avg = mean(x,1);
- x = x - repmat(avg,size(x,1),1);
Implement PCA to obtain xRot
- xRot = zeros(size(x));
- sigma = x * x' / size(x,2);
- [u,s,v] = svd(sigma);
- xRot = u' * x;
Check your implementation of PCA
- covar = zeros(size(x, 1));
- covar = xRot * xRot' / size(xRot,2);
Find k, the number of components to retain
- k = 0; % Set k accordingly
- all = sum(diag(s));
- for i=1:size(s,1)
- if sum(diag(s(1:i,1:i))) / all >= 0.99
- k = i;
- break;
- end
- end
Implement PCA with dimension reduction
- xHat = zeros(size(x));
- xTilde = u(:,1:k)' * x;
- xHat = u(:,1:k) * xTilde;
Implement PCA with whitening and regularisation
- epsilon = 0.1;
- xPCAWhite = zeros(size(x));
- xPCAWhite = diag(sqrt(1./(diag(s) + epsilon))) * xRot;
Check your implementation of PCA whitening
- covar = xPCAWhite * xPCAWhite' / size(xPCAWhite,2)
Implement ZCA whitening
- xZCAWhite = zeros(size(x));
- xZCAWhite = u * xPCAWhite;