zoukankan      html  css  js  c++  java
  • 主成分分析(PCA)之再理解

    PCA2D

    function p = PCA2d( X, Y)
    %PCA2d Principal components analysis in two-dimensions
    %
    %   Input:
    %       X    -- data points x - coordinates
    %       Y    -- data points y - coordinates
    %
    %   Output:
    %       structure with fields
    %       xm, ym   -- average location
    %       s1, s2   -- stdev in principal axes 
    %       th1, th2 -- principal axes inclination angles (in degrees)
    %       S        -- PCA matrix
    %       C        -- PC coefficients
    %
    %   Matlab functions called:
    %       atan2d, maen, isvector, isequal, isnan, length, size, sqrt
    %
        p.xm  = [];
        p.ym  = [];
        p.s1  = [];
        p.s2  = [];
        p.th1 = [];
        p.th2 = [];
        p.S   = [];
        p.C   = [];
        
        % check input
        narginchk(2,2)
        nargoutchk(0,7)
        
        % check input    
        validateattributes(X,   {'numeric'}, {'real',   'vector'});    
        validateattributes(Y,   {'numeric'}, {'real',   'vector'});
        if ~isequal(length(X),length(Y))
            error('The length of X and Y must be equal.')
        end
        % mean values
        p.xm = mean(X);
        p.ym = mean(Y);
        
        % calculate sums
        nd  = length(X);
        sx2 = 0;
        sxy = 0;
        sy2 = 0;
        nn  = 0;
        for n = 1:nd
            if isnan(X(n)) || isnan(Y(n))
                continue
            end
            nn = nn + 1;
            sx2 = sx2 + (X(n) - p.xm)^2;
            sxy = sxy + (X(n) - p.xm)*(Y(n) - p.ym);
            sy2 = sy2 + (Y(n) - p.ym)^2;
        end
        
        if nn <= 1
            warning('*** PCA2d error: invalid data. Principal directions are not calculated.');
            return
        end
        
        sx2 = sx2/(nn - 1);
        sxy = sxy/(nn - 1);
        sy2 = sy2/(nn - 1);
        
        % principal dierctions    
        p.th1  = 0.5*atan2d(2*sxy,(sx2 - sy2));
        p.th2  = p.th1 + 90;
        
        % stdev in principal directions
        s12  = ((sx2 + sy2) + sqrt((sx2 - sy2)^2 + 4*sxy^2))/2;
        s22  = ((sx2 + sy2) - sqrt((sx2 - sy2)^2 + 4*sxy^2))/2;    
        p.s1   = sqrt(s12);
        p.s2   = sqrt(s22);
        
        % pack output
        p.S   = [sx2, sxy; sxy, sy2];
        p.C   = [cosd(p.th1) sind(p.th1); cosd(p.th2) sind(p.th2)];
      
    end
    文章未经说明均属原创,学习笔记可能有大段的引用,一般会注明参考文献。 欢迎大家留言交流,转载请注明出处。
  • 相关阅读:
    sed&awk 资料汇总 全是链接
    LeetCode Path 3Sum
    C++ mem_fun
    递归绑定
    查询当天数据
    清除script注入
    防注入查询
    我的最新分页
    群发邮件
    利用缓存
  • 原文地址:https://www.cnblogs.com/yhlx125/p/14490762.html
Copyright © 2011-2022 走看看