1. 程式人生 > >matlab實現PCA降維

matlab實現PCA降維

利用PCA,把二維資料降為一維資料

load ('ex7data1.mat');
%變成一維
K = 1;
%對資料歸一化
means = mean(X);
X_means = bsxfun(@minus, X, means);
sigma = std(X_means);
X_std = bsxfun(@rdivide, X_means, sigma);
%繪製原始資料
scatter(X_std(:,1), X_std(:,2),'ro');
hold on;
[m n] = size(X);
%計算二維到一維的變換矩陣
sigma = 1/m * X_std' * X_std;
U = zeros(n);
%U為特徵向量構成的n*n矩陣,S為對角矩陣,對角線上的元素為特徵值
[U S D] = svd(sigma);
U_reduce = U(:,1:K);
Z = X_std * U_reduce;
X_rec = Z * U_reduce';
scatter(X_rec(:,1), X_rec(:,2),'bo');
%在原資料和降維後的資料間連線
for i = 1 : m
    plot([X_std(i,1) X_rec(i, 1)], [X_std(i, 2) X_rec(i, 2)], 'k');
end;
[c, cc] = min(X(:,1));
[d, dd] = max(X(:,1));
plot([X_rec(cc,1) X_rec(dd, 1)], [X_rec(cc,2) X_rec(dd, 2)], 'b');
axis([-3 3 -3 3]);

效果圖

測試資料

3.381563 3.389113
4.527875 5.854178
2.655682 4.411995
2.765235 3.715414
2.846560 4.175506
3.890672 6.488381
3.475805 3.632849
5.911298 6.680769
3.928894 5.098447
4.561835 5.623299
4.574072 5.397651
4.371734 5.461165
4.191694 4.954694
5.244085 4.661488
2.835840 3.768017
5.635270 6.312114
4.686330 5.665241
2.850513 4.626456
5.110157 7.363197
5.182564 4.646509
5.707328 6.681040
3.579685 4.802781
5.639378 6.120436
4.263469 4.689429
2.536517 3.884491
3.223829 4.942556
4.929488 5.955020
5.792958 5.108393
2.816848 4.818958
3.888824 5.100366
3.343234 5.893013
5.879734 5.521417
3.103919 3.857102
5.331506 4.680742
3.375427 4.565379
4.776679 6.254350
2.675746 3.730970
5.500277 5.679481
1.797097 3.247539
4.322515 5.111105
4.421004 6.025640
3.179299 4.436860
3.033541 3.978793
4.609348 5.879792
2.963789 3.300248
3.971762 5.407737
1.180233 2.878694
1.918950 5.071078
3.955247 4.505327
5.117955 6.085074