?? pca.html
字號(hào):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"><html><head> <title>Description of pca</title> <meta name="keywords" content="pca"> <meta name="description" content="pca: Principal component analysis"> <meta http-equiv="Content-Type" content="text/html; charset=big5"> <meta name="generator" content="m2html © 2003 Guillaume Flandin"> <meta name="robots" content="index, follow"> <link type="text/css" rel="stylesheet" href="../m2html.css"></head><body><a name="_top"></a><div><a href="../index.html">Home</a> > <a href="index.html">dcpr</a> > pca.m</div><!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png"> Master index</a></td><td align="right"><a href="index.html">Index for dcpr <img alt=">" border="0" src="../right.png"></a></td></tr></table>--><h1>pca</h1><h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="box"><strong>pca: Principal component analysis</strong></div><h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="box"><strong>function [DS2, eigVec, eigValue] = pca(DS, eigVecNum) </strong></div><h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="fragment"><pre class="comment">pca: Principal component analysis
Usage: [DS2, eigVec, eigValue] = pca(DS, eigVecNum)
DS: DS.input is the data matrix, where each column is a data vector
Please try "DS=dcData(1)" to get an example of DS.
eigVecNum: No. of selected eigenvectors
DS2: output data set, where DS2.input is the data after projection
eigVec: Each column of this matrix is a eigenvector of DS.input*DS.input' sorted by its decending order of eigen values
eigValue: Eigenvalues of (DS.input*DS.input') corresponding to eigVec
Note that DS.input must be zero-mean'ed before calling this function.
Type "pca" for a self-demo.</pre></div><!-- crossreference --><h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>This function calls:<ul style="list-style-image:url(../matlabicon.gif)"><li><a href="dcprDataPlot.html" class="code" title="function dcprDataPlot(DS, plotTitle, displayAnnotation)">dcprDataPlot</a> dcprDataPlot: Plot of 2D data for data clustering or pattern recognition</li><li><a href="prData.html" class="code" title="function [DS, TS]=prData(dataName)">prData</a> prData: Various data set for PR</li></ul>This function is called by:<ul style="list-style-image:url(../matlabicon.gif)"><li><a href="pcaKnnrLoo.html" class="code" title="function recogRate=pcaKnnrLoo(DS, plotOpt)">pcaKnnrLoo</a> ldaKnnrLoo: PCA analysis using KNNR and LOO</li></ul><!-- crossreference --><h2><a name="_subfunctions"></a>SUBFUNCTIONS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><ul style="list-style-image:url(../matlabicon.gif)"><li><a href="#_sub1" class="code">function selfdemo</a></li></ul><h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function [DS2, eigVec, eigValue] = pca(DS, eigVecNum)</a>0002 <span class="comment">%pca: Principal component analysis</span>0003 <span class="comment">% Usage: [DS2, eigVec, eigValue] = pca(DS, eigVecNum)</span>0004 <span class="comment">% DS: DS.input is the data matrix, where each column is a data vector</span>0005 <span class="comment">% Please try "DS=dcData(1)" to get an example of DS.</span>0006 <span class="comment">% eigVecNum: No. of selected eigenvectors</span>0007 <span class="comment">% DS2: output data set, where DS2.input is the data after projection</span>0008 <span class="comment">% eigVec: Each column of this matrix is a eigenvector of DS.input*DS.input' sorted by its decending order of eigen values</span>0009 <span class="comment">% eigValue: Eigenvalues of (DS.input*DS.input') corresponding to eigVec</span>0010 <span class="comment">%</span>0011 <span class="comment">% Note that DS.input must be zero-mean'ed before calling this function.</span>0012 <span class="comment">%</span>0013 <span class="comment">% Type "pca" for a self-demo.</span>0014 0015 <span class="comment">% Roger Jang, 970406, 990612, 991215, 20060506</span>0016 0017 <span class="keyword">if</span> nargin<1, <a href="#_sub1" class="code" title="subfunction selfdemo">selfdemo</a>; <span class="keyword">return</span>; <span class="keyword">end</span>0018 <span class="keyword">if</span> ~isstruct(DS)0019 fprintf(<span class="string">'Please try "DS=prData(1)" to get an example of DS.\n'</span>);0020 error(<span class="string">'The input DS should be a structure variable!'</span>);0021 <span class="keyword">end</span>0022 <span class="keyword">if</span> nargin<2, eigVecNum = min(size(DS.input)); <span class="keyword">end</span>0023 0024 m = size(DS.input,1); <span class="comment">% Dimension of data point</span>0025 n = size(DS.input,2); <span class="comment">% No. of data point</span>0026 A = DS.input;0027 0028 <span class="keyword">if</span> n>=m0029 [eigVec, eigValue] = eig(A*A');0030 eigValue = diag(eigValue);0031 <span class="comment">% ====== Sort based on descending order</span>0032 [junk, index] = sort(-eigValue);0033 eigValue = eigValue(index);0034 eigVec = eigVec(:, index);0035 <span class="keyword">if</span> eigVecNum<m0036 eigValue = eigValue(1:eigVecNum);0037 eigVec = eigVec(:, 1:eigVecNum);0038 <span class="keyword">end</span>0039 <span class="keyword">else</span> <span class="comment">% This is an efficient method which computes the eigvectors of A*A' when size(A,1)>size(A,2)</span>0040 <span class="comment">% A*A'*x=lambda*x ===> A'*A*A'*x=lambda*A'*x ===> eigVec of A'*A is A'*x, multiply these eigVec by A, we have A*A'*x=lambda*x ===> Got it!</span>0041 [eigVec, eigValue] = eig(A'*A);0042 eigValue = diag(eigValue);0043 <span class="comment">% ====== Sort based on descending order</span>0044 [junk, index] = sort(-eigValue);0045 eigValue = eigValue(index);0046 eigVec = eigVec(:, index); <span class="comment">% Eigenvectors of A'*A</span>0047 eigVec = A*eigVec; <span class="comment">% Eigenvectors of A*A'</span>0048 eigVec = eigVec*diag(1./(sum(eigVec.^2).^0.5)); <span class="comment">% Normalization</span>0049 <span class="keyword">if</span> eigVecNum<n0050 eigValue = eigValue(1:eigVecNum);0051 eigVec = eigVec(:, 1:eigVecNum);0052 <span class="keyword">end</span>0053 <span class="keyword">end</span>0054 0055 DS2=DS;0056 DS2.input=eigVec'*A;0057 0058 0059 <span class="comment">% ====== Self demo</span>0060 <a name="_sub1" href="#_subfunctions" class="code">function selfdemo</a>0061 <span class="comment">% ====== Demo for 2D data</span>0062 dataNum = 1000;0063 data = randn(1,dataNum)+j*randn(1,dataNum)/3;0064 data = data*exp(j*pi/6); <span class="comment">% ∮???30?⊙</span>0065 data = data-mean(data); <span class="comment">% ??”????????s</span>0066 plot(real(data), imag(data), <span class="string">'.'</span>); axis image;0067 DS.input=[real(data); imag(data)];0068 [DS2, v, eigValue] = feval(mfilename, DS);0069 v1 = v(:, 1);0070 v2 = v(:, 2);0071 arrow = [-1 0 nan -0.1 0 -0.1]+1+j*[0 0 nan 0.1 0 -0.1];0072 arrow1 = 2*arrow*(v1(1)+j*v1(2))*eigValue(1)/dataNum;0073 arrow2 = 2*arrow*(v2(1)+j*v2(2))*eigValue(2)/dataNum;0074 line(real(arrow1), imag(arrow1), <span class="string">'color'</span>, <span class="string">'r'</span>, <span class="string">'linewidth'</span>, 4);0075 line(real(arrow2), imag(arrow2), <span class="string">'color'</span>, <span class="string">'k'</span>, <span class="string">'linewidth'</span>, 4);0076 title(<span class="string">'Axes for PCA'</span>);0077 0078 <span class="comment">% ====== Demo for Iris data</span>0079 DS=<a href="prData.html" class="code" title="function [DS, TS]=prData(dataName)">prData</a>(<span class="string">'iris'</span>);0080 dataNum = size(DS.input, 2);0081 DS.input = DS.input-mean(DS.input, 2)*ones(1, dataNum); <span class="comment">% Make data zero-mean</span>0082 DS2=feval(mfilename, DS);0083 figure; <a href="dcprDataPlot.html" class="code" title="function dcprDataPlot(DS, plotTitle, displayAnnotation)">dcprDataPlot</a>(DS2); title(<span class="string">'IRIS projected on the first 2D of LDA'</span>);0084 DS2.input=DS2.input(3:4, :);0085 figure; <a href="dcprDataPlot.html" class="code" title="function dcprDataPlot(DS, plotTitle, displayAnnotation)">dcprDataPlot</a>(DS2); title(<span class="string">'IRIS projected on the last 2D of LDA'</span>);</pre></div><hr><address>Generated on Thu 30-Oct-2008 12:53:56 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> © 2003</address></body></html>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -