-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCA.m
122 lines (100 loc) · 3.11 KB
/
PCA.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
% =============================================
% Problem: Apply PCA to a pilot-plant data set.
% =============================================
%
% Hit any key to load the pilot-plant data.
pause
[data] = PCA_data;
xo=data(:,1); yo=data(:,2);
% Hit any key to subtract the mean of the data dimension from each data value
% in this dimension and plot the normalised pilot-plant data.
pause
x=xo-mean(xo); y=yo-mean(yo);
plot(x,y,'bo','markersize',5);
axis('equal'); axis([-100 80 -80 80]);
title('A plot of the normalised pilot-plant data');
xlabel('Acid number (normalised)');
ylabel('Organic acid content (normalised)');
hold on
% Hit any key to calculate the covariance matrix.
pause
C=cov(x,y)
% Hit any key to calculate eigenvalues and eigenvectors of the covariance matrix.
pause
lambda=eig(C); % the eigenvalues
lambda=sort(lambda,'descend')
variance=lambda*100/sum(lambda)
[V,D] = eig(C); % V is the matrix with the eigenvectors
V=V(:,2:-1:1)
e1=V(:,1)
e2=V(:,2)
% Hit any key to plot of the normalised pilot-plant data within the axes formed
% by the eigenvectors e1 and e2.
pause
xx1=-100;
yy1=xx1*V(2,1)/V(1,1);
xx2=80;
yy2=xx2*V(2,1)/V(1,1);
line([xx1 xx2],[yy1 yy2]);
hold on
xx1=-100;
yy1=xx1*V(2,2)/V(1,2);
xx2=80;
yy2=xx2*V(2,2)/V(1,2);
line([xx1 xx2],[yy1 yy2]);
line([-100 100],[0 0],'Color',[.8 .8 .8]);
line([0 0],[-80 80],'Color',[.8 .8 .8]);
text(-95,-20,'e1');
text(25,-70,'e2');
% Hit any key to plot of the normalised data projected onto the feature space
% formed by both eigenvectors, e1 and e2
pause
xy=[x y];
fin=xy*V;
figure
plot(fin(:,1),fin(:,2),'bo','markersize',5);
axis('equal'); axis([-80 100 -80 80]);
line([-80 100],[0 0]);
line([0 0],[-80 80]);
title('Normalised data projected on the feature space formed by e1 and e2');
xlabel('Acid number (normalised)');
ylabel('Organic acid content (normalised)');
text(90,8,'e1');
text(3,70,'e2');
% Hit any key to plot of the normalised data projected onto the feature space
% formed by a single eigenvector, e1
pause
V1=V(:,1)
fin1=xy*V1; y=0;
figure
plot(fin1,y,'bo','markersize',5);
axis('equal'); axis([-80 100 -80 80]);
line([-80 100],[0 0]);
title('Normalised data projected on the feature space formed by e1');
xlabel('Acid number (normalised)');
ylabel('Organic acid content (normalised)');
text(90,8,'e1');
% Hit any key to plot the reconstructed pilot-plant data derived using both
% eigenvectors, e1 and e2
pause
figure
data=fin*V';
data(:,1)=data(:,1)+mean(xo); data(:,2)=data(:,2)+mean(yo);
plot(data(:,1),data(:,2),'bo','markersize',5);
axis('equal'); axis([10 180 -40 120]);
title('Reconstructed data derived using eigenvectors e1 and e2');
xlabel('Acid number (normalised)');
ylabel('Organic acid content (normalised)');
% Hit any key to plot the reconstructed pilot-plant data derived using a single
% eigenvector, e1
pause
figure
data1=fin1*V1';
data1(:,1)=data1(:,1)+mean(xo); data1(:,2)=data1(:,2)+mean(yo);
plot(data1(:,1),data1(:,2),'bo','markersize',5);
axis('equal'); axis([10 180 -40 120]);
title('Reconstructed data derived using eigenvector e1');
xlabel('Acid number (normalised)');
ylabel('Organic acid content (normalised)');
echo off
disp('end of PCA')