⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pca2.m

📁 一个利用Matlab实现PCA(主成分分析)
💻 M
字号:
function [Y, lbd, cos2, CC] = pca2(X, P, labels)%PCA Principal Components Analysis%%       Y = PCA(X, P) returns the first P principal components of X.%       X must be a NxL matrix of N vectors in L dimensions.%%       Y = PCA(X, P, LABELS) displays several plots related to the PCA:%         . projection of data on the first two principal axes,%         . correlation circle,%         . % inertia of the principal components,%         . angles of the data w.r.t. the PC subspace.%       If LABELS is a matrix containing N rows of text, each row is used%       to label the corresponding data point, otherwise data go unlabelled.%%       Additional outputs: [Y, LBD, C2, CC] = PCA(X, P [, LABELS]), where%       Y is the projection of X on the first P principal components,%       LBD contains the % inertia of the required P components,%       C2 is the squared cosine between projected data and original data,%       CC is the correlation between the input features and the first P P.C.%% See also: .if (nargin < 2) or (nargin > 4)  error('PCANA: wrong number of arguments.')end[N, L] = size(X) ;if (P < 1)  error('PCANA: must return at least 1 principal component.')endif (P > L)  error('PCANA: can''t get more principal components than dimensions.')endif (nargin == 2)  verbose = 0 ;end%%%%%%%%%%R = corrcoef(X) ;                    %计算关系矩阵% XM= ones(N,1) * mean(X') ;% Xc = X' - XM ;% [U, S, V] = svd(Xc, 0) ;[V, S] = eig(R) ;                   %进行特征值特征向量分解[l, idx] = sort(diag(S)') ;l = fliplr(l) ;                    %按照将顺序排列idx = fliplr(idx) ;% Coordonees centrees reduites.XCR = (X - ones(N,1)*mean(X)) / diag(std(X)) ; % 将数据进行标准化,%PC = (Xc * V(:,idx)' + XM)' ;PC = (XCR * V(:,idx) ) ;                       %得到主成分% P first principal components:Y = PC(:, 1:P) ;                              % 得到第p个主成分% P first portions of inertia:lbd = l(1:P) / sum(l) ;                      %第p个主成分所占的分量,也就是贡献率      % Squared cosines of projection vs. original data:cos2 = sum((Y.^2)') ./ sum((XCR.^2)') ;% Correlations of features with PCs:CC = V(:,idx) * diag(sqrt(l)) ;               % eigenvector * sqrt(eigenvalue)                                              % in fact, it dispayes the p                                              % vector                                                                                                                                            if (nargin == 3)  figure  subplot(2,2,1) ;  plot(PC(:,1), PC(:,2), '+') ;  if (size(labels, 1) == N)    for i = 1:N      text(PC(i,1), PC(i,2), [' ', labels(i,:)]) ;    end  end    xlabel('First principal axis') ;  ylabel('Second principal axis') ;  title('Data projection on the first two principal axes')  subplot(2,2,2) ;  plot(cos(2*pi*(0:100)/100), sin(2*pi*(0:100)/100)) ;  hold on, plot([-1, 1], [0, 0]), plot([0, 0], [-1, 1]) ;  plot(CC(:,1), CC(:,2), 'x') ;  for k = 1:L    text(CC(k,1)+.03, CC(k,2)+.03, num2str(k)) ;  end  xlabel('First PC') ;  ylabel('Second PC') ;  title('Correlation of features with first 2 PCs')  subplot(2,2,3) ;  hh = plot(l/sum(l), '--') ;  set(hh, 'LineWidth', 3) ;  hold on, hh = plot(cumsum(l)/sum(l)) ;  set(hh, 'LineWidth', 3) ;  axis([1 L 0 1]) ;  plot([2 2], [0 1]) ;  xlabel('PC no.') ;  ylabel('Inertia') ;  legend('PC inertia', 'Cumulated inertia') ;  title('Repartition of inertia on the PCs') ;  subplot(2,2,4) ;  hold on  hh = plot(acos(sqrt(cos2))/pi*180, 1:N, '+') ;  set(hh, 'LineWidth', 3) ;  hold on  for i = 1:N    hh = plot([0, acos(sqrt(cos2(i)))/pi*180], [i, i], '-') ;    set(hh, 'LineWidth', 3) ;  end  set(gca, 'Box', 'on') ;  set(gca, 'YLim', [1 N]) ;  if (size(labels, 1) == N)    set(gca, 'YTick', 1:N) ;    set(gca, 'YTickLabels', labels) ;  end  xlabel('Angle (deg)')  title('Angle of data with the PC plan')endCC = CC(:,1:P) ;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -