UFLDL教程:Exercise:PCA in 2D PCA and Whitening
相關文章
PCA的原理及MATLAB實現
UFLDL教程:Exercise:PCA in 2D & PCA and Whitening
python-A comparison of various Robust PCA implementations
Deep Learning and Unsupervised Feature Learning Tutorial Solutions
統計學的基本概念
統計學里最基本的概念就是樣本的均值、方差、標準差。首先,我們給定一個含有n個樣本的集合,下面給出這些概念的公式描述:
均值:
標準差:
方差:
均值描述的是樣本集合的中間點,它告訴我們的信息是有限的,而標準差給我們描述的是樣本集合的各個樣本點到均值的距離之平均。標準差描述的是“散布度”。
在概率論和統計學中,一個隨機變量的方差描述的是它的離散程度,也就是該變量離其期望值的距離。一個實隨機變量的方差也稱為它的二階矩或二階中心動差,恰巧也是它的二階累積量。
這里的n為樣本個數。
某個變量的方差越大,不確定度越大,就信息論而言,其包含的信息越大.
標準差和方差一般是用來描述一維數據的。
協方差:
協方差(Covariance)在概率論統計學中用于衡量兩個變量的總體誤差。
這里的n為樣本個數。
協方差的結果有什么意義呢?如果結果為正值,則說明兩者是正相關的(從協方差可以引出“相關系數”的定義),也就是說一個人越猥瑣越受女孩歡迎。如果結果為負值, 就說明兩者是負相關,越猥瑣女孩子越討厭。如果為0,則兩者之間沒有關系,猥瑣不猥瑣和女孩子喜不喜歡之間沒有關聯,就是統計上說的“相互獨立”。
協方差的性質
備注
PCA
PCA的具有2個功能,一是維數約簡(可以加快算法的訓練速度,減小內存消耗等),一是數據的可視化。
PCA的直觀目標:
要尋找一個線性變換,使得變換后變量兩兩線性無關,能量集中到較少的幾個變量中,并且按照大小重新排列.,變換后,我們可以相應地舍去后面幾個能量小的分量,達到降維的目的.
在使用PCA前需要對數據進行預處理,首先是均值化,即對每個特征維,都減掉該維的平均值,然后就是將不同維的數據范圍歸一化到同一范圍,方法一般都是除以最大值。
但是比較奇怪的是,在對自然圖像進行均值處理時并不是不是減去該維的平均值,而是減去這張圖片本身的平均值。
因為PCA的預處理是按照不同應用場合來定的。
PCA的計算過程主要是要求2個東西,一個是降維后的各個向量的方向,另一個是原先的樣本在新的方向上投影后的值。
由于相鄰像素間的相關性,PCA算法可以將輸入向量轉換為一個維數低很多的近似向量,而且誤差非常小。
首先需求出訓練樣本的協方差矩陣,如公式所示(輸入數據已經均值化過):
求出訓練樣本的協方差矩陣后,將其進行SVD分解,得出的U向量中的每一列就是這些數據樣本的新的方向向量了,排在前面的向量代表的是主方向,依次類推。用U’*X得到的就是降維后的樣本值z了,即:
備注
Whitening
Whitening的目的是去掉數據之間的相關聯度,是很多算法進行預處理的步驟。
比如說當訓練圖片數據時,由于圖片中相鄰像素值有一定的關聯,所以很多信息是冗余的。這時候去相關的操作就可以采用白化操作。
數據的whitening必須滿足兩個條件:
一、是不同特征間相關性最小,接近0;
二、是所有特征的方差相等(不一定為1)。
常見的白化操作有PCA whitening和ZCA whitening。
PCA whitening是指將數據x經過PCA降維為z后,可以看出z中每一維是獨立的,滿足whitening白化的第一個條件,這是只需要將z中的每一維都除以標準差就得到了每一維的方差為1,也就是說方差相等。公式為:
白化與降維相結合
如果你想要得到經過白化后的數據,并且比初始輸入維數更低,可以僅保留 中前k 個成分。當我們把PCA白化和正則化結合起來時, 中最后的少量成分將總是接近于0,因而舍棄這些成分不會帶來很大的問題.
正則化
實踐中需要實現PCA白化或ZCA白化時,有時一些特征值 在數值上接近于0,這樣在縮放步驟時我們除以 將導致除以一個接近0的值;這可能使數據上溢 (賦為大數值)或造成數值不穩定。因而在實踐中,我們使用少量的正則化實現這個縮放過程,即在取平方根和倒數之前給特征值加上一個很小的常數 :
當 x 在區間 [-1,1] 上時, 一般取值為 。
對圖像來說, 這里加上 ,對輸入圖像也有一些平滑(或低通濾波)的作用。這樣處理還能消除在圖像的像素信息獲取過程中產生的噪聲,改善學習到的特征。
ZCA whitening是指數據x先經過PCA變換為z,但是并不降維,因為這里是把所有的成分都選進去了。這是也同樣滿足whtienning的第一個條件,特征間相互獨立。然后同樣進行方差為1的操作,最后將得到的矩陣左乘一個特征向量矩陣U即可。
ZCA白化,就是在PCA白化的基礎上做了一個旋轉.
ZCA whitening公式為:
PCA Whitening是保證數據各維度的方差為1,而ZCA Whitening是保證數據各維度的方差相等即可,不一定要為1。
并且這兩種whitening的一般用途也不一樣,PCA Whitening主要用于降維且去相關性,而ZCA Whitening主要用于去相關性,且盡量保持原數據的維數。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
備注
1. PCA只有在x各行均值為0的前提下才成立(每個維度的均值為0)。其中x為n*m矩陣,每列為n維特征,每行表示m個樣本。
其實PCA的推導過程中是要求x各行均值為0的,而對于某自然圖像數據x,其各行的均值天生就接近0的,所以就算不把x各行均值歸0也可以使用PCA。
其實pca_2d中的數據雖然不是圖像數據,其各行均值也恰好接近0,所以就算不把各行的均值歸0也可以(勉強)用PCA,但是理論上是應該各行均值歸0的。
2. 協方差矩陣sigma可以用sigma = x * x’ / size(x, 2)來估計其實是在均值為0的條件下才成立
3. 不同的數據均值化(2d數據和圖像)
針對的圖像數據的均值歸0:(每個像素減去的是整個圖像的均值)avg = mean(x, 1); % 對行求和再平均,使各列均值歸0。 x = x - repmat(avg, size(x, 1), 1); 針對2d數據的均值歸0:(均值為每個行的均值,也就是每個維度的均值。x為n行m列,n為維度,m個樣本)mean_x=mean(x,2); % 對列求和再平均,使各行均值歸0。 x=x-repmat(mean_x,1,size(x,2));%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pca_2d MATLAB實現
close all%%================================================================ %% Step 0: Load data % We have provided the code to load data from pcaData.txt into x. % x is a 2 * 45 matrix, where the kth column x(:,k) corresponds to % the kth data point.Here we provide the code to load natural image data into x. % You do not need to change the code below.x = load('pcaData.txt','-ascii'); figure(1); scatter(x(1, :), x(2, :)); title('Raw data');%%================================================================ %% Step 1a: Implement PCA to obtain U % Implement PCA to obtain the rotation matrix U, which is the eigenbasis % sigma. % -------------------- YOUR CODE HERE -------------------- u = zeros(size(x, 1)); % You need to compute this [n m] = size(x); %x = x-repmat(mean(x,2),1,m);%預處理,均值為0,針對2d數據 sigma = (1.0/m)*x*x'; [u s v] = svd(sigma);% -------------------------------------------------------- hold on plot([0 u(1,1)], [0 u(2,1)]); plot([0 u(1,2)], [0 u(2,2)]); scatter(x(1, :), x(2, :)); hold off%%================================================================ %% Step 1b: Compute xRot, the projection on to the eigenbasis % Now, compute xRot by projecting the data on to the basis defined % by U. Visualize the points by performing a scatter plot.% -------------------- YOUR CODE HERE -------------------- xRot = zeros(size(x)); % You need to compute this xRot = u'*x;% -------------------------------------------------------- % Visualise the covariance matrix. You should see a line across the % diagonal against a blue background. figure(2); scatter(xRot(1, :), xRot(2, :)); title('xRot');%%================================================================ %% Step 2: Reduce the number of dimensions from 2 to 1. % Compute xRot again (this time projecting to 1 dimension). % Then, compute xHat by projecting the xRot back onto the original axes % to see the effect of dimension reduction% -------------------- YOUR CODE HERE -------------------- k = 1; % Use k = 1 and project the data onto the first eigenbasis xHat = zeros(size(x)); % You need to compute this xHat = u(:,1:k)*(u(:,1:k)'*x);%這樣寫是為了使特征點落在特征向量所指的方向上而不是原坐標系上 % -------------------------------------------------------- figure(3); scatter(xHat(1, :), xHat(2, :)); title('xHat');%%================================================================ %% Step 3: PCA Whitening % Complute xPCAWhite and plot the results.epsilon = 1e-5; % -------------------- YOUR CODE HERE -------------------- xPCAWhite = zeros(size(x)); % You need to compute this xPCAWhite = diag(1./sqrt(diag(s)+epsilon))*u'*x;% -------------------------------------------------------- figure(4); scatter(xPCAWhite(1, :), xPCAWhite(2, :)); title('xPCAWhite');%%================================================================ %% Step 3: ZCA Whitening % Complute xZCAWhite and plot the results.% -------------------- YOUR CODE HERE -------------------- xZCAWhite = zeros(size(x)); % You need to compute this xZCAWhite = u*diag(1./sqrt(diag(s)+epsilon))*u'*x;% -------------------------------------------------------- figure(5); scatter(xZCAWhite(1, :), xZCAWhite(2, :)); title('xZCAWhite');%% Congratulations! When you have reached this point, you are done! % You can now move onto the next PCA exercise. :)
PCA and Whitening MATLAB實現
第0步:數據準備
UFLDL下載的文件中,包含數據集IMAGES_RAW,它是一個512*512*10的矩陣,也就是10幅512*512的圖像
(a)載入數據
利用sampleIMAGESRAW函數,從IMAGES_RAW中提取numPatches個圖像塊兒,每個圖像塊兒大小為patchSize,并將提取到的圖像塊兒按列存放,分別存放在在矩陣patches的每一列中,即patches(:,i)存放的是第i個圖像塊兒的所有像素值
(b)數據去均值化處理
將每一個圖像塊兒的所有像素值都減去該圖像塊兒的平均像素值,實現數據的去均值化
第一步:執行PCA
該部分分為兩部分
(1)進行PCA計算,這里僅僅對數據x進行旋轉得到xrot,而不進行主成分的提取
(2)對旋轉后的數據求解協方差矩陣covar,并將其可視化,觀察得到的選擇后的數據是否正確
PCA保證選擇后的數據的協方差矩陣是一個對角陣,如果covar是正確的
那么它的圖像應該是一個藍色背景,并且在對角線位置有一斜線.
第二步:滿足條件的主成分個數
本部分,找到滿足條件的主成分的個數k
也就是找到最小的k值,使得(λ1+…+ λk)/(λ1+…+ λn)>某個百分數,如99%
第三步:利用找到的主成分個數,對數據進行降維
在第二步,已經找到了數字k,也就是,保留數據的k個主成分就滿足了要求
在該步,將對數據x進行降維,只留下k個主成分,得到xTidle
同時,為了觀察降維后的數據的好壞,在利用U(:,k)將降維后的數據變換會原來的維數,也就是得到了原數據的近似恢復數據
并利用網格將恢復出的圖像顯示出,與原圖像進行比較.
第四步:PCA白化+正則化
該部分分為兩步
(1)執行具有白化和正則化的PCA
首先,對數據進行旋轉(利用特征矩陣U)
然后,利用特征值對旋轉后的數據進行縮放,實現白化
同時,在利用特征值縮放時,利用參數ε對特征值進行微調,實現正則化
(b)計算百化后的數據的協方差矩陣,觀察該協方差矩陣
如果加入了正則化項,則該協方差矩陣的對角線元素都小于1
如果沒有加入正則項(即僅有旋轉+白化),則該協方差矩陣的對角線元素都為1(實際上,是令ε為一個極小的數)
第五步:ZCA白化
ZCA白化,就是在PCA白化的基礎上做了一個旋轉,即
%%================================================================ %% Step 0a: Load data % Here we provide the code to load natural image data into x. % x will be a 144 * 10000 matrix, where the kth column x(:, k) corresponds to % the raw image data from the kth 12x12 image patch sampled. % You do not need to change the code below.x = sampleIMAGESRAW(); %從IMAGES_RAW中讀取一些圖像patches figure('name','Raw images');%顯示一個figure,標題為raw images randsel = randi(size(x,2),200,1); % A random selection of samples for visualization display_network(x(:,randsel)); %顯示隨機選取的圖像塊兒%%================================================================ %% Step 0b: Zero-mean the data (by row) % You can make use of the mean and repmat/bsxfun functions.% -------------------- YOUR CODE HERE -------------------- x = x-repmat(mean(x,1),size(x,1),1);%求的是每一列的均值 %x的每一列的所有元素都減去該列的均值,這是針對圖像數據。 分別為每個圖像塊計算像素強度的均值。 %x = x-repmat(mean(x,2),1,size(x,2)); 這是針對2d數據的均值處理。%%================================================================ %% Step 1a: Implement PCA to obtain xRot % Implement PCA to obtain xRot, the matrix in which the data is expressed % with respect to the eigenbasis of sigma, which is the matrix U.% -------------------- YOUR CODE HERE -------------------- xRot = zeros(size(x)); % You need to compute this [n m] = size(x); sigma = (1.0/m)*x*x'; %輸入數據的協方差矩陣 [u s v] = svd(sigma); %對協方差矩陣進行特征值分解 xRot = u'*x;% 對數據進行旋轉 %%================================================================ %% Step 1b: Check your implementation of PCA % The covariance matrix for the data expressed with respect to the basis U % should be a diagonal matrix with non-zero entries only along the main % diagonal. We will verify this here. % Write code to compute the covariance matrix, covar. % When visualised as an image, you should see a straight line across the % diagonal (non-zero entries) against a blue background (zero entries).% -------------------- YOUR CODE HERE -------------------- covar = zeros(size(x, 1)); % You need to compute this covar = (1./m)*xRot*xRot';%旋轉數據后的數據對應的協方差矩陣% Visualise the covariance matrix. You should see a line across the % diagonal against a blue background. figure('name','Visualisation of covariance matrix'); imagesc(covar);%%================================================================ %% Step 2: Find k, the number of components to retain % Write code to determine k, the number of components to retain in order % to retain at least 99% of the variance.% -------------------- YOUR CODE HERE -------------------- k = 0; % Set k accordingly ss = diag(s); % for k=1:m % if sum(s(1:k))./sum(ss) < 0.99 % continue; % end %其中cumsum(ss)求出的是一個累積向量,也就是說ss向量值的累加值 %并且(cumsum(ss)/sum(ss))<=0.99是一個向量,值為0或者1的向量,為1表示滿足那個條件 k = length(ss((cumsum(ss)/sum(ss))<=0.99));% k = 0; % Set k accordingly % egis=eig(covar) % egis=sort(egis,'descend') % for i=1:size(covar,1) % if (sum(egis(1:i))/sum(egis)>0.99) % k=i % break; % end % end%%================================================================ %% Step 3: Implement PCA with dimension reduction % Now that you have found k, you can reduce the dimension of the data by % discarding the remaining dimensions. In this way, you can represent the % data in k dimensions instead of the original 144, which will save you % computational time when running learning algorithms on the reduced % representation. % % Following the dimension reduction, invert the PCA transformation to produce % the matrix xHat, the dimension-reduced data with respect to the original basis. % Visualise the data and compare it to the raw data. You will observe that % there is little loss due to throwing away the principal components that % correspond to dimensions with low variation.% -------------------- YOUR CODE HERE -------------------- % 對數據進行降維 xTidle=u(:,1:k)'*x; % 利用降維后的數據xTidle對數據進行恢復 xHat = zeros(size(x)); % You need to compute this xHat = u*[u(:,1:k)'*x;zeros(n-k,m)];% Visualise the data, and compare it to the raw data % You should observe that the raw and processed data are of comparable quality. % For comparison, you may wish to generate a PCA reduced image which % retains only 90% of the variance.figure('name',['PCA processed images ',sprintf('(%d / %d dimensions)', k, size(x, 1)),'']); display_network(xHat(:,randsel)); figure('name','Raw images'); display_network(x(:,randsel));%%================================================================ %% Step 4a: Implement PCA with whitening and regularisation % Implement PCA with whitening and regularisation to produce the matrix % xPCAWhite. epsilon = 0.1; xPCAWhite = zeros(size(x));% -------------------- YOUR CODE HERE -------------------- xPCAWhite = diag(1./sqrt(diag(s)+epsilon))*u'*x; figure('name','PCA whitened images'); display_network(xPCAWhite(:,randsel));%%================================================================ %% Step 4b: Check your implementation of PCA whitening % Check your implementation of PCA whitening with and without regularisation. % PCA whitening without regularisation results a covariance matrix % that is equal to the identity matrix. PCA whitening with regularisation % results in a covariance matrix with diagonal entries starting close to % 1 and gradually becoming smaller. We will verify these properties here. % Write code to compute the covariance matrix, covar. % % Without regularisation (set epsilon to 0 or close to 0), % when visualised as an image, you should see a red line across the % diagonal (one entries) against a blue background (zero entries). % With regularisation, you should see a red line that slowly turns % blue across the diagonal, corresponding to the one entries slowly % becoming smaller.% -------------------- YOUR CODE HERE -------------------- covar = (1./m)*xPCAWhite*xPCAWhite';% Visualise the covariance matrix. You should see a red line across the % diagonal against a blue background. figure('name','Visualisation of covariance matrix'); imagesc(covar);%%================================================================ %% Step 5: Implement ZCA whitening % Now implement ZCA whitening to produce the matrix xZCAWhite. % Visualise the data and compare it to the raw data. You should observe % that whitening results in, among other things, enhanced edges.xZCAWhite = zeros(size(x));% -------------------- YOUR CODE HERE -------------------- xZCAWhite = u*xPCAWhite;%ZCA白化即在PCA白化基礎上做了一個旋轉 % Visualise the data, and compare it to the raw data. % You should observe that the whitened images have enhanced edges. figure('name','ZCA whitened images'); display_network(xZCAWhite(:,randsel)); figure('name','Raw images'); display_network(x(:,randsel));參考文獻
Deep learning:十(PCA和whitening)
UFLDL教程答案(3):Exercise:PCA_in_2D&PCA_and_Whitening
UFLDL教程之(三)PCA and Whitening exercise
主成分分析
白化
深入理解PCA
PCA的原理及MATLAB實現
淺談協方差矩陣
再談協方差矩陣之主成分分析
吳恩達 Andrew Ng 的公開課
總結
以上是生活随笔為你收集整理的UFLDL教程:Exercise:PCA in 2D PCA and Whitening的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 农行信用币怎么还款?信用币还款后多久能用
- 下一篇: UFLDL教程:Exercise:Sof