主成分分析(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
作者:太一吾鱼水
文章未经说明均属原创,学习笔记可能有大段的引用,一般会注明参考文献。
欢迎大家留言交流,转载请注明出处。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
2018-03-06 Ethzasl MSF源码阅读(2):百川汇海
2014-03-06 CSLA.Net学习(3)INotifyPropertyChanged和IDataErrorInfo
2013-03-06 [地图代数]ArcGIS栅格计算器的应用
2012-03-06 BindingSource组件使用