教程地址:http://deeplearning.stanford.edu/wiki/index.php/UFLDL_Tutorial
Exercise地址:http://deeplearning.stanford.edu/wiki/index.php/Exercise:PCA_and_Whitening
代码
Step 0b: Zero-mean the data (by row)
avg = mean(x,1); %分别对每个图像块计算像素强度的平均值,生成一个10000维行向量
x = x - repmat(avg, size(x,1),1); %分别对每个图像块中的每个像素减去相应的图像块的亮度均值
Step 1a: Implement PCA to obtain xRot
xRot = zeros(size(x)); % You need to compute this
sigma = x * x' / size(x,2); %计算协方差矩阵sigma;x 是一个n*m的矩阵,每列表示一个训练样本
[U,S,V] = svd(sigma); %矩阵 U 将包含 Sigma 的特征向量(一个特征向量一列,从主向量开始排序),
%矩阵S 对角线上的元素将包含对应的特征值(同样降序排列)。
%矩阵V等于U的转置,可以忽略
xRot = U' * x; %计算旋转后的数据Rot
Step 1b: Check your implementation of PCA
covar = xRot * xRot' / size(x,2); %计算xRot的covariance matrix :协方差矩阵
Step 2: Find k, the number of components to retain
lambda = diag(S);
for i = 1:size(x,1)
if((sum(lambda(1:i))/sum(lambda)) >= 0.99)
k = i;
break;
end
end
Step 3: Implement PCA with dimension reduction
xHat = zeros(size(x)); % You need to compute this
xTlide = zeros(size(x));
xTlide(1:k,:) = U(:,1:k)' * x; %数据降维后的结果,k为希望保留的特征向量的数目
xHat = U * xTlide; %还原近似数据
Step 4a: Implement PCA with whitening and regularisation
xPCAWhite = diag(1./sqrt(diag(S) + epsilon)) * U' * x;
Step 4b: Check your implementation of PCA whitening
covar = xPCAWhite * xPCAWhite' / size(x,2); %计算xPCAWhite的covariance matrix :协方差矩阵
Step 5: Implement ZCA whitening
xZCAWhite = U * diag(1./sqrt(diag(S) + epsilon)) * U' * x;