1 | . |
2.m
· 4.8 KiB · Mathematica
Sin formato
clear; clc;
% Get Data
gbr1 = imread('images/Banana/1.jpg');
gbr2 = imread('images/Banana/2.jpg');
gbr3 = imread('images/Banana/3.jpg');
gbr4 = imread('images/Banana/4.jpg');
gbr5 = imread('images/Banana/5.jpg');
gbr6 = imread('images/Banana/6.jpg');
gbr7 = imread('images/Banana/7.jpg');
gbr8 = imread('images/Banana/8.jpg');
gbr9 = imread('images/Banana/9.jpg');
gbr10 = imread('images/Banana/10.jpg');
gbr11 = imread('images/Kiwi/1.jpg');
gbr12 = imread('images/Kiwi/2.jpg');
gbr13 = imread('images/Kiwi/3.jpg');
gbr14 = imread('images/Kiwi/4.jpg');
gbr15 = imread('images/Kiwi/5.jpg');
gbr16 = imread('images/Kiwi/6.jpg');
gbr17 = imread('images/Kiwi/7.jpg');
gbr18 = imread('images/Kiwi/8.jpg');
gbr19 = imread('images/Kiwi/9.jpg');
gbr20 = imread('images/Kiwi/10.jpg');
gbr21 = imread('images/Orange/1.jpg');
gbr22 = imread('images/Orange/2.jpg');
gbr23 = imread('images/Orange/3.jpg');
gbr24 = imread('images/Orange/4.jpg');
gbr25 = imread('images/Orange/5.jpg');
gbr26 = imread('images/Orange/6.jpg');
gbr27 = imread('images/Orange/7.jpg');
gbr28 = imread('images/Orange/8.jpg');
gbr29 = imread('images/Orange/9.jpg');
gbr30 = imread('images/Orange/10.jpg');
gbr1=double(rgb2gray(gbr1));
gbr2=double(rgb2gray(gbr2));
gbr3=double(rgb2gray(gbr3));
gbr4=double(rgb2gray(gbr4));
gbr5=double(rgb2gray(gbr5));
gbr6=double(rgb2gray(gbr6));
gbr7=double(rgb2gray(gbr7));
gbr8=double(rgb2gray(gbr8));
gbr9=double(rgb2gray(gbr9));
gbr10=double(rgb2gray(gbr10));
gbr11=double(rgb2gray(gbr11));
gbr12=double(rgb2gray(gbr12));
gbr13=double(rgb2gray(gbr13));
gbr14=double(rgb2gray(gbr14));
gbr15=double(rgb2gray(gbr15));
gbr16=double(rgb2gray(gbr16));
gbr17=double(rgb2gray(gbr17));
gbr18=double(rgb2gray(gbr18));
gbr19=double(rgb2gray(gbr19));
gbr20=double(rgb2gray(gbr20));
gbr21=double(rgb2gray(gbr21));
gbr22=double(rgb2gray(gbr22));
gbr23=double(rgb2gray(gbr23));
gbr24=double(rgb2gray(gbr24));
gbr25=double(rgb2gray(gbr25));
gbr26=double(rgb2gray(gbr26));
gbr27=double(rgb2gray(gbr27));
gbr28=double(rgb2gray(gbr28));
gbr29=double(rgb2gray(gbr29));
gbr30=double(rgb2gray(gbr30));
gbr1=reshape(gbr1',1,[]);
gbr2=reshape(gbr2',1,[]);
gbr3=reshape(gbr3',1,[]);
gbr4=reshape(gbr4',1,[]);
gbr5=reshape(gbr5',1,[]);
gbr6=reshape(gbr6',1,[]);
gbr7=reshape(gbr7',1,[]);
gbr8=reshape(gbr8',1,[]);
gbr9=reshape(gbr9',1,[]);
gbr10=reshape(gbr10',1,[]);
gbr11=reshape(gbr11',1,[]);
gbr12=reshape(gbr12',1,[]);
gbr13=reshape(gbr13',1,[]);
gbr14=reshape(gbr14',1,[]);
gbr15=reshape(gbr15',1,[]);
gbr16=reshape(gbr16',1,[]);
gbr17=reshape(gbr17',1,[]);
gbr18=reshape(gbr18',1,[]);
gbr19=reshape(gbr19',1,[]);
gbr20=reshape(gbr20',1,[]);
gbr21=reshape(gbr21',1,[]);
gbr22=reshape(gbr22',1,[]);
gbr23=reshape(gbr23',1,[]);
gbr24=reshape(gbr24',1,[]);
gbr25=reshape(gbr25',1,[]);
gbr26=reshape(gbr26',1,[]);
gbr27=reshape(gbr27',1,[]);
gbr28=reshape(gbr28',1,[]);
gbr29=reshape(gbr29',1,[]);
gbr30=reshape(gbr30',1,[]);
X = [gbr1, gbr2, gbr3, gbr4, gbr5, gbr6, gbr7, gbr8, gbr9, gbr10, gbr11, gbr12, gbr13, gbr14, gbr15, gbr16, gbr17, gbr18, gbr19, gbr20, gbr21, gbr22, gbr23, gbr24, gbr25, gbr26, gbr27, gbr28, gbr29, gbr30];
[n,p] = size(X);
Xrata = mean(X)
%1. mengurangi setiap variable dengan rata2nya (normalisasi data)
for i = 1:n
Z(i, :) = X(i, :)-Xrata;
end
%Menghitung covariance matrix
S = cov(X); % varian
%Standardization Data
for i = 1:n
for j = 1:p
Zr(i,j) = Z(i,j) / sqrt(S(j,j));
end
end
%ZR ADALAH HASIL DARI NORMALISASI DATA X (DATA AWAL)
%2. Menghitung cov dari data yang sudah direduksi
Zcov = cov(Zr); % matrix covarian dari variable hasil normalisasi Zr
%3. Menghitung eigen value dan eigen vector dari Zcov
[VA, DA] = eig(Zcov); % VA = Eigen Vector ; DA = Eigen Value
%4. menghitung komponen utama
% Hasil library matlab eig() --> hasil = diagonal utamanya urut dari kecil ke besar
% yang diperlukan dari besar ke kecil
%mengurutkan hasil eigen (DESC)
m = 1
for i = p:-1:1
DA_urut(m,m) = DA(i,i);
VA_urut(m,:) = VA(i,:);
m = m+1;
end
%menghitung komponen utama
for i = 1:p
eValue(i,1) = DA_urut(i,i);
end
Total = sum(eValue)% total varians dalam data asli yang dapat dijelaskan oleh seluruh komponen utama.
% Proporsi kumulatif = mengambil data seberapa persen secara kumulatif yang
% dianggap perlu
PK = 0;
komponen = 0;%jumlah komponen utama yang dipertahankan
for j = 1:p
proporsiValue(j,:) = eValue(j,1) / Total;%mengukur seberapa besar kontribusi eigenvalue ke-j terhadap varians total data.
if(PK < 0.9) % Nilai PK tergantung peneliti --> 0.8
PK = PK + proporsiValue(j, :);
komponen = komponen + 1;
end
end
% Mengambil eigen vector dari komponen yang terpilih
VA_Komponen = VA_urut(:,1:komponen)
% W = variable komponen utama / principal component variable
W = Z * VA_Komponen % W ADALAH FINAL DATA YANG DISEBUTKAN DI PDF
plot3(W(:,1), W(:,2), W(:,3), '*') % PK < 0.9
%plot(W(:,1), W(:,2), '*') % PK < 0.8
grid on
1 | clear; clc; |
2 | |
3 | % Get Data |
4 | gbr1 = imread('images/Banana/1.jpg'); |
5 | gbr2 = imread('images/Banana/2.jpg'); |
6 | gbr3 = imread('images/Banana/3.jpg'); |
7 | gbr4 = imread('images/Banana/4.jpg'); |
8 | gbr5 = imread('images/Banana/5.jpg'); |
9 | gbr6 = imread('images/Banana/6.jpg'); |
10 | gbr7 = imread('images/Banana/7.jpg'); |
11 | gbr8 = imread('images/Banana/8.jpg'); |
12 | gbr9 = imread('images/Banana/9.jpg'); |
13 | gbr10 = imread('images/Banana/10.jpg'); |
14 | gbr11 = imread('images/Kiwi/1.jpg'); |
15 | gbr12 = imread('images/Kiwi/2.jpg'); |
16 | gbr13 = imread('images/Kiwi/3.jpg'); |
17 | gbr14 = imread('images/Kiwi/4.jpg'); |
18 | gbr15 = imread('images/Kiwi/5.jpg'); |
19 | gbr16 = imread('images/Kiwi/6.jpg'); |
20 | gbr17 = imread('images/Kiwi/7.jpg'); |
21 | gbr18 = imread('images/Kiwi/8.jpg'); |
22 | gbr19 = imread('images/Kiwi/9.jpg'); |
23 | gbr20 = imread('images/Kiwi/10.jpg'); |
24 | gbr21 = imread('images/Orange/1.jpg'); |
25 | gbr22 = imread('images/Orange/2.jpg'); |
26 | gbr23 = imread('images/Orange/3.jpg'); |
27 | gbr24 = imread('images/Orange/4.jpg'); |
28 | gbr25 = imread('images/Orange/5.jpg'); |
29 | gbr26 = imread('images/Orange/6.jpg'); |
30 | gbr27 = imread('images/Orange/7.jpg'); |
31 | gbr28 = imread('images/Orange/8.jpg'); |
32 | gbr29 = imread('images/Orange/9.jpg'); |
33 | gbr30 = imread('images/Orange/10.jpg'); |
34 | gbr1=double(rgb2gray(gbr1)); |
35 | gbr2=double(rgb2gray(gbr2)); |
36 | gbr3=double(rgb2gray(gbr3)); |
37 | gbr4=double(rgb2gray(gbr4)); |
38 | gbr5=double(rgb2gray(gbr5)); |
39 | gbr6=double(rgb2gray(gbr6)); |
40 | gbr7=double(rgb2gray(gbr7)); |
41 | gbr8=double(rgb2gray(gbr8)); |
42 | gbr9=double(rgb2gray(gbr9)); |
43 | gbr10=double(rgb2gray(gbr10)); |
44 | gbr11=double(rgb2gray(gbr11)); |
45 | gbr12=double(rgb2gray(gbr12)); |
46 | gbr13=double(rgb2gray(gbr13)); |
47 | gbr14=double(rgb2gray(gbr14)); |
48 | gbr15=double(rgb2gray(gbr15)); |
49 | gbr16=double(rgb2gray(gbr16)); |
50 | gbr17=double(rgb2gray(gbr17)); |
51 | gbr18=double(rgb2gray(gbr18)); |
52 | gbr19=double(rgb2gray(gbr19)); |
53 | gbr20=double(rgb2gray(gbr20)); |
54 | gbr21=double(rgb2gray(gbr21)); |
55 | gbr22=double(rgb2gray(gbr22)); |
56 | gbr23=double(rgb2gray(gbr23)); |
57 | gbr24=double(rgb2gray(gbr24)); |
58 | gbr25=double(rgb2gray(gbr25)); |
59 | gbr26=double(rgb2gray(gbr26)); |
60 | gbr27=double(rgb2gray(gbr27)); |
61 | gbr28=double(rgb2gray(gbr28)); |
62 | gbr29=double(rgb2gray(gbr29)); |
63 | gbr30=double(rgb2gray(gbr30)); |
64 | |
65 | gbr1=reshape(gbr1',1,[]); |
66 | gbr2=reshape(gbr2',1,[]); |
67 | gbr3=reshape(gbr3',1,[]); |
68 | gbr4=reshape(gbr4',1,[]); |
69 | gbr5=reshape(gbr5',1,[]); |
70 | gbr6=reshape(gbr6',1,[]); |
71 | gbr7=reshape(gbr7',1,[]); |
72 | gbr8=reshape(gbr8',1,[]); |
73 | gbr9=reshape(gbr9',1,[]); |
74 | gbr10=reshape(gbr10',1,[]); |
75 | gbr11=reshape(gbr11',1,[]); |
76 | gbr12=reshape(gbr12',1,[]); |
77 | gbr13=reshape(gbr13',1,[]); |
78 | gbr14=reshape(gbr14',1,[]); |
79 | gbr15=reshape(gbr15',1,[]); |
80 | gbr16=reshape(gbr16',1,[]); |
81 | gbr17=reshape(gbr17',1,[]); |
82 | gbr18=reshape(gbr18',1,[]); |
83 | gbr19=reshape(gbr19',1,[]); |
84 | gbr20=reshape(gbr20',1,[]); |
85 | gbr21=reshape(gbr21',1,[]); |
86 | gbr22=reshape(gbr22',1,[]); |
87 | gbr23=reshape(gbr23',1,[]); |
88 | gbr24=reshape(gbr24',1,[]); |
89 | gbr25=reshape(gbr25',1,[]); |
90 | gbr26=reshape(gbr26',1,[]); |
91 | gbr27=reshape(gbr27',1,[]); |
92 | gbr28=reshape(gbr28',1,[]); |
93 | gbr29=reshape(gbr29',1,[]); |
94 | gbr30=reshape(gbr30',1,[]); |
95 | |
96 | X = [gbr1, gbr2, gbr3, gbr4, gbr5, gbr6, gbr7, gbr8, gbr9, gbr10, gbr11, gbr12, gbr13, gbr14, gbr15, gbr16, gbr17, gbr18, gbr19, gbr20, gbr21, gbr22, gbr23, gbr24, gbr25, gbr26, gbr27, gbr28, gbr29, gbr30]; |
97 | [n,p] = size(X); |
98 | Xrata = mean(X) |
99 | |
100 | %1. mengurangi setiap variable dengan rata2nya (normalisasi data) |
101 | for i = 1:n |
102 | Z(i, :) = X(i, :)-Xrata; |
103 | end |
104 | |
105 | %Menghitung covariance matrix |
106 | S = cov(X); % varian |
107 | |
108 | %Standardization Data |
109 | for i = 1:n |
110 | for j = 1:p |
111 | Zr(i,j) = Z(i,j) / sqrt(S(j,j)); |
112 | end |
113 | end |
114 | %ZR ADALAH HASIL DARI NORMALISASI DATA X (DATA AWAL) |
115 | |
116 | %2. Menghitung cov dari data yang sudah direduksi |
117 | Zcov = cov(Zr); % matrix covarian dari variable hasil normalisasi Zr |
118 | |
119 | %3. Menghitung eigen value dan eigen vector dari Zcov |
120 | [VA, DA] = eig(Zcov); % VA = Eigen Vector ; DA = Eigen Value |
121 | |
122 | %4. menghitung komponen utama |
123 | % Hasil library matlab eig() --> hasil = diagonal utamanya urut dari kecil ke besar |
124 | % yang diperlukan dari besar ke kecil |
125 | %mengurutkan hasil eigen (DESC) |
126 | m = 1 |
127 | for i = p:-1:1 |
128 | DA_urut(m,m) = DA(i,i); |
129 | VA_urut(m,:) = VA(i,:); |
130 | m = m+1; |
131 | end |
132 | |
133 | %menghitung komponen utama |
134 | for i = 1:p |
135 | eValue(i,1) = DA_urut(i,i); |
136 | end |
137 | |
138 | Total = sum(eValue)% total varians dalam data asli yang dapat dijelaskan oleh seluruh komponen utama. |
139 | |
140 | % Proporsi kumulatif = mengambil data seberapa persen secara kumulatif yang |
141 | % dianggap perlu |
142 | PK = 0; |
143 | komponen = 0;%jumlah komponen utama yang dipertahankan |
144 | |
145 | for j = 1:p |
146 | proporsiValue(j,:) = eValue(j,1) / Total;%mengukur seberapa besar kontribusi eigenvalue ke-j terhadap varians total data. |
147 | if(PK < 0.9) % Nilai PK tergantung peneliti --> 0.8 |
148 | PK = PK + proporsiValue(j, :); |
149 | komponen = komponen + 1; |
150 | end |
151 | end |
152 | |
153 | % Mengambil eigen vector dari komponen yang terpilih |
154 | VA_Komponen = VA_urut(:,1:komponen) |
155 | % W = variable komponen utama / principal component variable |
156 | W = Z * VA_Komponen % W ADALAH FINAL DATA YANG DISEBUTKAN DI PDF |
157 | |
158 | plot3(W(:,1), W(:,2), W(:,3), '*') % PK < 0.9 |
159 | %plot(W(:,1), W(:,2), '*') % PK < 0.8 |
160 | grid on |