clear; clc;
% data are stored as rows.
points = [1.4000 1.55000;
3.0000 3.2000;
0.6000 0.7000;
2.2000 2.3000;
1.8000 2.1000;
2.0000 1.6000;
1.0000 1.1000;
2.5000 2.4000;
1.5000 1.6000;
1.2000 0.8000;
2.1000 2.5000];
figure(1)
plot(points(: ,1), points(: ,2), 'b+'); hold on;
axis([-1, 4, -1, 4])
title('original data')
[rows, cols] = size(points);
% calculate the average of each row.
meanPoints = mean(points);
% subtract the mean.
points = points - ones(rows, 1) * meanPoints;
% calculate the covariance matrix.
covMatrix = cov(points);
% get the eigenvalues and eigenvectors of the covariance matrix.
[E D] = eig(covMatrix)
line([0, E(2) * 4], [0, E(4) * 4])
line([0, E(1) * 4], [0, E(3) * 4])
[机器学习] PCA matlab代码
最新推荐文章于 2023-05-21 11:52:00 发布