文章标题:UFLDL:练习三

来源:互联网 发布:西安软件新城楼盘 编辑:程序博客网 时间:2024/05/16 15:54

1.PCA in 2D

1.1 Step 1a: Implement PCA to obtain U

u = zeros(size(x, 1)); % You need to compute thissigma = 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 thisxRot=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 eigenbasisxHat = zeros(size(x)); % You need to compute thisx_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 thisxPCAWhite = diag(1./sqrt(diag(s) + epsilon)) * u' * x;

1.5 Step 3: ZCA Whitening

xZCAWhite = zeros(size(x)); % You need to compute thisxZCAWhite=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 thissigma = 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 thiscovar = xRot * xRot' / size(xRot, 2);

2.4 Step 2: Find k, the number of components to retain

k = 0; % Set k accordinglysum_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;    endend

2.5 Step 3: Implement PCA with dimension reduction

xHat = zeros(size(x));  % You need to compute thisxTilde = 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;
0 0