%行代表样本数目,列代表特征
B=xlsread('rapeseed.xls','B2:M402');
X=B'; %转置矩阵
XZ = zscore(X);%数据标准化
[COEFF,SCORE,LATENT]=princomp(XZ);
% 为了直观,定义元胞数组result1,用来存放特征值、贡献率和累积贡献率等数据
explained = 100*LATENT/sum(LATENT);%将latent总和统一为100,便于观察贡献率
[m, n] = size(X);
result1 = cell(n+1, 4);
result1(1,:) = {'特征值', '差值', '贡献率', '累积贡献率'};
result1(2:end,1) = num2cell(LATENT);
result1(2:end-1,2) = num2cell(-diff(LATENT));
result1(2:end,3:4) = num2cell([explained, cumsum(explained)]);
% 为了直观,定义元胞数组result2,用来存放前2个主成分表达式的系数数据
result2 = cell(n+1, 3);
result2(1,:) = {'标准化变量', '特征向量t1', '特征向量t2'};
result2(2:end, 2:end) = num2cell(COEFF(:,1:2));
% 为了直观,定义元胞数组result2,用来存放前2个主成分表达式的系数数据
oilname = {'菜籽油1','菜籽油2','菜籽油3','菜籽油4','山茶油1','山茶油2','山茶油3','山茶油4','葵花油1','葵花油2','葵花油3','葵花油4'}';
sumXZ = sum(XZ,2);
[s1, id] = sortrows(SCORE,1);
result3 = cell(m+1, 3);
result3(1,:) = {'油样', '第一主成分得分y1', '第二主成分得分y2'};
result3(2:end, 1) = oilname(id);
result3(2:end, 2:end) = num2cell([s1(:,1:2)]);
plot(SCORE(:,1),SCORE(:,2),'ro');%k是黑色o是空心圆
本内容试读结束,登录后可阅读更多
下载后可阅读完整内容,剩余1页未读,立即下载