Matlab编程实现主成分分析.程序结构及函数作用在软件Matlab中实现主成分分析可以采取两种方式实现:一是通过编程来实现;二是直接调用Matlab种自带程序实现。
下面主要主要介绍利用Matlab的矩阵计算功能编程实现主成分分析。
1程序结构2函数作用Cwstd.m——用总和标准化法标准化矩阵Cwfac.m——计算相关系数矩阵;计算特征值和特征向量;对主成分进行排序;计算各特征值贡献率;挑选主成分(累计贡献率大于85%),输出主成分个数;计算主成分载荷Cwscore.m——计算各主成分得分、综合得分并排序Cwprint.m——读入数据文件;调用以上三个函数并输出结果3.源程序3.1 cwstd.m总和标准化法标准化矩阵%cwstd.m,用总和标准化法标准化矩阵function std=cwstd(vector)cwsum=sum(vector,1); %对列求和[a,b]=size(vector); %矩阵大小,a为行数,b为列数for i=1:afor j=1:bstd(i,j)= vector(i,j)/cwsum(j);endend3.2 cwfac.m计算相关系数矩阵%cwfac.mfunction result=cwfac(vector);fprintf('相关系数矩阵:\n')std=CORRCOEF(vector) %计算相关系数矩阵fprintf('特征向量(vec)及特征值(val):\n')[vec,val]=eig(std) %求特征值(val)及特征向量(vec)newval=diag(val) ;[y,i]=sort(newval) ; %对特征根进行排序,y为排序结果,i为索引fprintf('特征根排序:\n')for z=1:length(y)newy(z)=y(length(y)+1-z);endfprintf('%g\n',newy)rate=y/sum(y);fprintf('\n贡献率:\n')newrate=newy/sum(newy)sumrate=0;newi=[];for k=length(y):-1:1sumrate=sumrate+rate(k);newi(length(y)+1-k)=i(k);if sumrate>0.85 break;endend %记下累积贡献率大85%的特征值的序号放入newi中fprintf('主成分数:%g\n\n',length(newi));fprintf('主成分载荷:\n')for p=1:length(newi)for q=1:length(y)result(q,p)=sqrt(newval(newi(p)))*vec(q,newi(p));endend %计算载荷disp(result)3.3 cwscore.m%cwscore.m,计算得分function score=cwscore(vector1,vector2);sco=vector1*vector2;csum=sum(sco,2);[newcsum,i]=sort(-1*csum);[newi,j]=sort(i);fprintf('计算得分:\n')score=[sco,csum,j]%得分矩阵:sco为各主成分得分;csum为综合得分;j为排序结果3.4 cwprint.m%cwprint.mfunction print=cwprint(filename,a,b);%filename为文本文件文件名,a为矩阵行数(样本数),b为矩阵列数(变量指标数)fid=fopen(filename,'r')vector=fscanf(fid,'%g',[a b]);fprintf('标准化结果如下:\n')v1=cwstd(vector)result=cwfac(v1);cwscore(v1,result);4.程序测试例题4.1原始数据中国大陆35个大城市某年的10项社会经济统计指标数据见下表。
4.2运行结果>> cwprint('cwbook.txt',35,10)fid =6数据标准化结果如下:v1 =0.0581 0.0356 0.0435 0.0680 0.0557 0.1112 0.1194 0.1184 0.1083 0.13920.0423 0.0346 0.0354 0.0770 0.0089 0.0642 0.0483 0.0499 0.0534 0.05440.0407 0.0139 0.0688 0.0234 0.0080 0.0047 0.0151 0.0314 0.0252 0.01830.0139 0.0391 0.0056 0.0093 0.0053 0.0290 0.0087 0.0174 0.0234 0.01580.0097 0.0263 0.0086 0.0028 0.0064 0.0064 0.0045 0.0062 0.0111 0.00750.0315 0.0375 0.0305 0.0198 0.0213 0.0376 0.0243 0.0398 0.0357 0.02780.0253 0.0295 0.0443 0.0286 0.0295 0.0468 0.0304 0.0334 0.0248 0.02330.0321 0.0242 0.0437 0.0203 0.0132 0.0233 0.0153 0.0212 0.0270 0.02130.0431 0.0276 0.0628 0.0142 0.0184 0.0184 0.0206 0.0285 0.0455 0.03160.0610 0.0440 0.0488 0.1853 0.0176 0.1086 0.1848 0.1148 0.0888 0.13520.0250 0.0318 0.0233 0.0444 0.0391 0.0273 0.0284 0.0251 0.0300 0.03270.0286 0.0212 0.0334 0.0408 0.0490 0.0285 0.0192 0.0328 0.0255 0.02850.0250 0.0152 0.0337 0.0361 0.0609 0.0251 0.0215 0.0232 0.0164 0.01990.0200 0.0190 0.0148 0.0085 0.0134 0.0037 0.0100 0.0072 0.0125 0.00890.0271 0.0163 0.0508 0.0223 0.0243 0.0175 0.0200 0.0222 0.0183 0.01640.0060 0.0290 0.0079 0.0195 0.0102 0.0063 0.0179 0.0093 0.01240.01590.0197 0.0237 0.0162 0.0078 0.0101 0.0078 0.0072 0.0117 0.0164 0.01160.0259 0.0243 0.0350 0.0214 0.0162 0.0287 0.0197 0.0182 0.0220 0.01820.0327 0.0220 0.0562 0.0391 0.0367 0.0416 0.0282 0.0220 0.0273 0.02320.0286 0.0204 0.0160 0.0180 0.0286 0.0165 0.0166 0.0227 0.0223 0.01680.0344 0.0349 0.0286 0.0255 0.0268 0.0377 0.0259 0.0254 0.0393 0.03170.0271 0.0185 0.0270 0.0105 0.0239 0.0140 0.0139 0.0153 0.0183 0.01440.0318 0.0370 0.0377 0.0793 0.0603 0.0582 0.0754 0.0901 0.0482 0.07350.0056 0.0472 0.0071 0.0692 0.0240 0.0104 0.0791 0.0421 0.0240 0.04560.0133 0.0242 0.0170 0.0039 0.0141 0.0080 0.0064 0.0097 0.0119 0.00900.0025 0.0497 0.0011 0.0024 0.0146 0.0057 0.0049 0.0072 0.0050 0.00480.1428 0.0123 0.0983 0.0292 0.1437 0.0613 0.0385 0.0402 0.0590 0.03870.0466 0.0199 0.0456 0.0200 0.1100 0.0479 0.0240 0.0331 0.0350 0.02900.0149 0.0271 0.0085 0.0076 0.0430 0.0101 0.0085 0.0079 0.0146 0.01010.0220 0.0230 0.0187 0.0123 0.0154 0.0294 0.0224 0.0182 0.0232 0.02030.0313 0.0244 0.0174 0.0125 0.0283 0.0238 0.0175 0.0259 0.0300 0.02130.0134 0.0324 0.0061 0.0100 0.0050 0.0116 0.0073 0.0117 0.0173 0.01330.0062 0.0311 0.0016 0.0024 0.0048 0.0036 0.0021 0.0038 0.0072 0.00530.0044 0.0340 0.0040 0.0022 0.0058 0.0029 0.0032 0.0036 0.0063 0.00430.0074 0.0491 0.0019 0.0063 0.0073 0.0221 0.0109 0.0105 0.0146 0.0125相关系数矩阵:std =1.0000 -0.3444 0.8425 0.3603 0.7390 0.6215 0.4039 0.4967 0.6761 0.4689-0.3444 1.0000 -0.4750 0.3096 -0.3539 0.1971 0.3571 0.2600 0.1570 0.30900.8425 -0.4750 1.0000 0.3358 0.5891 0.5056 0.3236 0.4456 0.5575 0.37420.3603 0.3096 0.3358 1.0000 0.1507 0.7664 0.9412 0.8480 0.7320 0.86140.7390 -0.3539 0.5891 0.1507 1.0000 0.4294 0.1971 0.3182 0.3893 0.25950.6215 0.1971 0.5056 0.7664 0.4294 1.0000 0.8316 0.8966 0.9302 0.90270.4039 0.3571 0.3236 0.9412 0.1971 0.8316 1.0000 0.9233 0.8376 0.95270.4967 0.2600 0.4456 0.8480 0.3182 0.8966 0.9233 1.0000 0.9201 0.97310.6761 0.1570 0.5575 0.7320 0.3893 0.9302 0.8376 0.92011.0000 0.93960.4689 0.3090 0.3742 0.8614 0.2595 0.9027 0.9527 0.9731 0.9396 1.0000特征向量(vec):vec =-0.1367 0.2282 -0.2628 0.1939 0.6371 -0.2163 0.3176 -0.1312 -0.4191 0.2758-0.0329 -0.0217 0.0009 0.0446 -0.1447 -0.4437 0.4058 -0.5562 0.5487 0.0593-0.0522 -0.0280 0.2040 -0.0492 -0.5472 -0.4225 0.3440 0.3188 -0.4438 0.2401 0.0067 -0.4176 -0.2856 -0.2389 0.1926 -0.4915 -0.4189 0.2726 0.2065 0.3403 0.0404 0.1408 0.0896 0.0380 -0.1969 -0.0437 -0.4888 -0.6789 -0.4405 0.1861-0.0343 0.2360 0.0640 -0.8294 0.0377 0.2662 0.1356 -0.1290 0.0278 0.37820.2981 0.4739 0.5685 0.2358 0.1465 -0.1502 -0.2631 0.1245 0.2152 0.36440.1567 0.3464 -0.6485 0.2489 -0.4043 0.2058 -0.0704 0.0462 0.1214 0.38120.4879 -0.5707 0.1217 0.1761 0.0987 0.3550 0.3280 -0.0139 0.0071 0.3832-0.7894 -0.1628 0.1925 0.2510 -0.0422 0.2694 0.0396 0.04560.1668 0.3799特征值(val)val =0.0039 0 0 0 0 0 0 0 0 00 0.0240 0 0 0 0 0 0 0 00 0 0.0307 0 0 0 0 0 0 00 0 0 0.0991 0 0 0 0 0 00 0 0 0 0.1232 0 0 0 0 00 0 0 0 0 0.2566 0 0 0 00 0 0 0 0 0 0.3207 0 0 00 0 0 0 0 0 0 0.5300 0 00 0 0 0 0 0 0 0 2.3514 00 0 0 0 0 0 0 0 0 6.2602特征根排序:6.260222.351380.5300470.3206990.2566390.1232410.09909150.03070880.02403550.00393387各主成分贡献率:newrate =0.6260 0.2351 0.0530 0.0321 0.0257 0.0123 0.0099 0.00310.0024 0.0004第一、二主成分的载荷:0.690 1 -0.64270.148 3 0.84140.600 7 -0.68050.851 5 0.31670.465 6 -0.67540.946 3 0.04260.911 7 0.32990.953 7 0.18620.958 9 0.01090.950 6 0.2558第一、二、三、四主成分的得分:score =0.718 5 0.049 9 0.768 4 2.00000.380 6 0.038 6 0.419 2 4.00000.184 8 -0.043 3 0.141 4 21.00000.118 6 0.031 1 0.149 7 20.00000.054 9 0.011 5 0.066 4 33.00000.228 8 0.007 0 0.235 8 7.000 00.2364 -0.0081 0.2283 10.00000.1778 -0.0167 0.1611 16.00000.2292 -0.0337 0.1955 14.00000.8382 0.1339 0.9721 1.00000.2276 0.0064 0.2340 8.00000.2279 -0.0222 0.2056 12.00000.1989 -0.0382 0.1607 18.00000.0789 -0.0061 0.0728 32.00000.1711 -0.0317 0.1394 23.00000.0926 0.0266 0.1192 25.00000.0900 -0.0000 0.0899 28.00000.1692 -0.0082 0.1610 17.00000.2441 -0.0318 0.2124 11.00000.1507 -0.0108 0.1399 22.00000.2316 0.0012 0.2328 9.00000.1294 -0.0211 0.1083 27.00000.4716 0.0328 0.5045 3.00000.2737 0.0834 0.3570 5.00000.0754 -0.0013 0.0741 31.0000标准实用文案大全0.0448 0.0349 0.0797 30.0000 0.4759 -0.2028 0.2731 6.0000 0.2907 -0.0883 0.2024 13.0000 0.0944 -0.0118 0.0826 29.0000 0.1546 0.0035 0.1581 19.0000 0.1718 -0.0092 0.1626 15.0000 0.0865 0.0230 0.1095 26.0000 0.0349 0.0216 0.0566 35.0000 0.0343 0.0228 0.0572 34.0000 0.0889 0.0422 0.1310 24.0000。