princomp.m

本文深入解析了主成分分析(PCA)算法的实现原理及应用。通过MATLAB中的princomp函数源代码,详细介绍了如何从数据集计算主成分向量、协方差矩阵、重构向量等关键步骤,并给出了平均平方误差的计算方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

%princomp源程序
function P = princomp(X, q) 
%PRINCOMP Obtain principal-component vectors and related quantities. 
%   P = PRINCOMP(X, Q) Computes the principal-component vectors of 
%   the vector population contained in the rows of X, a matrix of 
%   size K-by-n where K is the number of vectors and n is their 
%   dimensionality. Q, with values in the range [0, n], is the number 
%   of eigenvectors used in constructing the principal-components 
%   transformation matrix. P is a structure with the following 
%   fields: 
% 
%     P.Y      K-by-Q matrix whose columns are the principal- 
%              component vectors. 
%     P.A      Q-by-n principal components transformation matrix 
%              whose rows are the Q eigenvectors of Cx corresponding 
%              to the Q largest eigenvalues.  
%     P.X      K-by-n matrix whose rows are the vectors reconstructed 
%              from the principal-component vectors. P.X and P.Y are 
%              identical if Q = n.  
%     P.ems    The mean square error incurred in using only the Q 
%              eigenvectors corresponding to the largest 
%              eigenvalues. P.ems is 0 if Q = n.   
%     P.Cx     The n-by-n covariance matrix of the population in X. 
%     P.mx     The n-by-1 mean vector of the population in X. 
%     P.Cy     The Q-by-Q covariance matrix of the population in 
%              Y. The main diagonal contains the eigenvalues (in 
%              descending order) corresponding to the Q eigenvectors. 
 
%   Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins 
%   Digital Image Processing Using MATLAB, Prentice-Hall, 2004 
%   $Revision: 1.4 $  $Date: 2003/10/26 23:16:16 $ 
 
[K, n] = size(X); 
X = double(X); 
 
% Obtain the mean vector and covariance matrix of the vectors in X. 
[P.Cx, P.mx] = covmatrix(X); 
P.mx = P.mx'; % Convert mean vector to a row vector. 
 
% Obtain the eigenvectors and corresponding eigenvalues of Cx.  The 
% eigenvectors are the columns of n-by-n matrix V.  D is an n-by-n 
% diagonal matrix whose elements along the main diagonal are the 
% eigenvalues corresponding to the eigenvectors in V, so that X*V = 
% D*V. 
[V, D] = eig(P.Cx); 
 
% Sort the eigenvalues in decreasing order.  Rearrange the 
% eigenvectors to match.  
d = diag(D); 
[d, idx] = sort(d); 
d = flipud(d); 
idx = flipud(idx); 
D = diag(d); 
V = V(:, idx); 
 
% Now form the q rows of A from first q columns of V. 
P.A = V(:, 1:q)'; 
 
% Compute the principal component vectors. 
Mx = repmat(P.mx, K, 1); % M-by-n matrix.  Each row = P.mx. 
P.Y = P.A*(X - Mx)'; % q-by-K matrix. 
 
% Obtain the reconstructed vectors. 
P.X = (P.A'*P.Y)' + Mx; 
 
% Convert P.Y to K-by-q array and P.mx to n-by-1 vector. 
P.Y = P.Y'; 
P.mx = P.mx'; 
 
% The mean square error is given by the sum of all the  
% eigenvalues minus the sum of the q largest eigenvalues. 
d = diag(D); 
P.ems = sum(d(q + 1:end)); 
 
% Covariance matrix of the Y's: 
P.Cy = P.A*P.Cx*P.A'; 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值