Skip to main content

Using PCA on Three Dimensional Dataset

In this work, We use PCA three dimensional data. 

Matlab Code

% PCA Model

clear all, clc , close all
hold on
axis equal
axis([-2 2 -2 2 -2 2])
% Step 1: Get some data
X = [1 2 -1 -2 0; 0.2 0 0.1 0.2 -0.4; 1.2 0.3 -1 -0.1 -0.4]';
% Step 2: Substract the mean
plot3(X(:,1),X(:,2),X(:,3),'ko');
XAdjust = X-repmat(mean(X),size(X,1),1);
plot3(XAdjust(:,1),XAdjust(:,2),XAdjust(:,3),'ro');
% Step 3: Calculate the covariance matrix
CM = cov(X);
% Step 4: Eigenvalue and Eigenvector
[V D]= eig(CM);
% Step 5: Choosing component
f1 = V(:,1)';
f2 = V(:,2)';
f3 = V(:,3)';
F=[f1; f2; f3];
A = 2*V;
plot3([-A(1,1),A(1,1)],[-A(1,2),A(1,2)],[-A(1,3) A(1,3)],'b:');
plot3([-A(2,1),A(2,1)],[-A(2,2),A(2,2)],[-A(2,3) A(2,3)],'b:');
plot3([-A(3,1),A(3,1)],[-A(3,2),A(3,2)],[-A(3,3) A(3,3)],'b:');
title('Orjinal data and Mean Adjust Data')
% Step 6: Deriving the dataset
Pc1 = f1*XAdjust';
Pc2 = f2*XAdjust';
Pc3 = f3*XAdjust';
Y = [Pc1' Pc2' Pc3'];

figure
hold on
axis equal
axis([-2 2 -2 2 -2 2])
plot3(Y(:,1),Y(:,2),Y(:,3),'gd');

Cy = cov(Y);
[Vy Dy] = eig(Cy);
A = 2*V;
plot3([-A(1,1),A(1,1)],[-A(1,2),A(1,2)],[-A(1,3) A(1,3)],'b:');
plot3([-A(2,1),A(2,1)],[-A(2,2),A(2,2)],[-A(2,3) A(2,3)],'b:');
plot3([-A(3,1),A(3,1)],[-A(3,2),A(3,2)],[-A(3,3) A(3,3)],'b:');

OUTPUTS

Original data ve Adjusted Data

PCA Data

From a different Angle

Comments

Popular posts from this blog

Use PCA in Machine Learning

Matlab Code % Source % http://www.dcs.gla.ac.uk/~srogers/firstcourseml/matlab/chapter7/pcaexample.html#2 clear all;close all; % Generate the data Y = [randn(20,2);randn(20,2)+5;randn(20,2)-5]; % Add 5 random dimensions N = size(Y,1); Y = [Y randn(N,5)];

Plot a Sinusoidal Wave in Matlab

In this example, we use sinusoidal wave and plot data several different figure.