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]);

效果图

matlab实现PCA降维_第1张图片

测试数据

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

 

你可能感兴趣的:(机器学习)