1.m
· 1.3 KiB · Mathematica
Surowy
clear; clc;
X = xlsread('DATA1B');
X=X(1:13,1:4)
[n,p] = size(X);
k = 3;
idx = [3 9 12];
%
C = X(idx,:)
%
temp = zeros(n,1);
it=0
while 1,
for i =1:n
for l = 1:k
sum=0;
for j = 1:p
sum=sum+(X(i,j)-C(l,j))^2;
end
d(i,l)=sqrt(sum);
end
end
[z,g]=min(d,[],2);
if(g==temp)
break;
else
temp = g;
end
for i=1:k
f=find(g==i);
if f
C(i,:)=mean(X(find(g==i),:),1);
end
end
it=it+1;
end
y = [X,g];
for i=1:n
no(i) = norm(X(i,:));
end
y1 = [(1:n)', no',g];
k1 = X(find(g==1),:)
k2 = X(find(g==2),:)
k3 = X(find(g==3),:)
[n1,p1] = size(k1);
[n2,p2] = size(k2);
[n3,p3] = size(k3);
for i=1:n1
norm1(i) = norm(k1(i,:));
end
x1 = [k1 norm1']
for i=1:n2
norm2(i) = norm(k2(i,:));
end
x2 = [k2 norm2']
for i=1:n3
norm3(i) = norm(k3(i,:));
end
x3 = [k3 norm3']
cov1 = cov(k1)
cov2 = cov(k2)
cov3 = cov(k3)
det1 = det(cov1)
det2 = det(cov2)
det3 = det(cov3)
c1 = y1(find(g==1),:);
c2 = y1(find(g==2),:);
c3 = y1(find(g==3),:);
plot(c1(:,1),norm1, 'r*', c2(:,1),norm2, 'b*', c3(:,1),norm3, 'g*')
hold on
grid on
legend({'Cluster 1','Cluster 2', 'Cluster 3'},'Location','northeast')
title('Norm Distance Plot');
1 | clear; clc; |
2 | |
3 | X = xlsread('DATA1B'); |
4 | X=X(1:13,1:4) |
5 | [n,p] = size(X); |
6 | |
7 | k = 3; |
8 | |
9 | idx = [3 9 12]; |
10 | % |
11 | C = X(idx,:) |
12 | % |
13 | temp = zeros(n,1); |
14 | |
15 | it=0 |
16 | while 1, |
17 | for i =1:n |
18 | for l = 1:k |
19 | sum=0; |
20 | for j = 1:p |
21 | sum=sum+(X(i,j)-C(l,j))^2; |
22 | end |
23 | d(i,l)=sqrt(sum); |
24 | end |
25 | end |
26 | [z,g]=min(d,[],2); |
27 | if(g==temp) |
28 | break; |
29 | else |
30 | temp = g; |
31 | end |
32 | for i=1:k |
33 | f=find(g==i); |
34 | if f |
35 | C(i,:)=mean(X(find(g==i),:),1); |
36 | end |
37 | end |
38 | it=it+1; |
39 | end |
40 | |
41 | |
42 | y = [X,g]; |
43 | for i=1:n |
44 | no(i) = norm(X(i,:)); |
45 | end |
46 | y1 = [(1:n)', no',g]; |
47 | |
48 | k1 = X(find(g==1),:) |
49 | k2 = X(find(g==2),:) |
50 | k3 = X(find(g==3),:) |
51 | [n1,p1] = size(k1); |
52 | [n2,p2] = size(k2); |
53 | [n3,p3] = size(k3); |
54 | for i=1:n1 |
55 | norm1(i) = norm(k1(i,:)); |
56 | end |
57 | |
58 | x1 = [k1 norm1'] |
59 | |
60 | for i=1:n2 |
61 | norm2(i) = norm(k2(i,:)); |
62 | end |
63 | |
64 | x2 = [k2 norm2'] |
65 | |
66 | for i=1:n3 |
67 | norm3(i) = norm(k3(i,:)); |
68 | end |
69 | |
70 | x3 = [k3 norm3'] |
71 | |
72 | cov1 = cov(k1) |
73 | cov2 = cov(k2) |
74 | cov3 = cov(k3) |
75 | det1 = det(cov1) |
76 | det2 = det(cov2) |
77 | det3 = det(cov3) |
78 | c1 = y1(find(g==1),:); |
79 | c2 = y1(find(g==2),:); |
80 | c3 = y1(find(g==3),:); |
81 | |
82 | plot(c1(:,1),norm1, 'r*', c2(:,1),norm2, 'b*', c3(:,1),norm3, 'g*') |
83 | hold on |
84 | grid on |
85 | legend({'Cluster 1','Cluster 2', 'Cluster 3'},'Location','northeast') |
86 | title('Norm Distance Plot'); |
2.m
· 4.9 KiB · Mathematica
Surowy
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);
%1. mengurangi setiap variable dengan rata2nya (normalisasi data)
Z = zeros(size(X));
for i = 1:n
Z(i, :) = X(i, :)-Xrata;
end
%Menghitung covariance matrix
S = cov(X); % varian
%Standardization Data
Zr = zeros(size(Z));
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
eValue = zeros(p, 1);
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
proporsiValue = zeros(p, 1);
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 = Zr * 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, 1); |
99 | |
100 | %1. mengurangi setiap variable dengan rata2nya (normalisasi data) |
101 | Z = zeros(size(X)); |
102 | for i = 1:n |
103 | Z(i, :) = X(i, :)-Xrata; |
104 | end |
105 | |
106 | %Menghitung covariance matrix |
107 | S = cov(X); % varian |
108 | |
109 | %Standardization Data |
110 | Zr = zeros(size(Z)); |
111 | for i = 1:n |
112 | for j = 1:p |
113 | Zr(i,j) = Z(i,j) / sqrt(S(j,j)); |
114 | end |
115 | end |
116 | % Zr ADALAH HASIL DARI NORMALISASI DATA X (DATA AWAL) |
117 | |
118 | %2. Menghitung cov dari data yang sudah direduksi |
119 | Zcov = cov(Zr); % matrix covarian dari variable hasil normalisasi Zr |
120 | |
121 | %3. Menghitung eigen value dan eigen vector dari Zcov |
122 | [VA, DA] = eig(Zcov); % VA = Eigen Vector ; DA = Eigen Value |
123 | |
124 | %4. menghitung komponen utama |
125 | % Hasil library matlab eig() --> hasil = diagonal utamanya urut dari kecil ke besar |
126 | % yang diperlukan dari besar ke kecil |
127 | %mengurutkan hasil eigen (DESC) |
128 | m = 1; |
129 | for i = p:-1:1 |
130 | DA_urut(m,m) = DA(i,i); |
131 | VA_urut(m,:) = VA(:,i)'; |
132 | m = m+1; |
133 | end |
134 | |
135 | %menghitung komponen utama |
136 | eValue = zeros(p, 1); |
137 | for i = 1:p |
138 | eValue(i,1) = DA_urut(i,i); |
139 | end |
140 | |
141 | Total = sum(eValue); % total varians dalam data asli yang dapat dijelaskan oleh seluruh komponen utama. |
142 | |
143 | % Proporsi kumulatif = mengambil data seberapa persen secara kumulatif yang |
144 | % dianggap perlu |
145 | PK = 0; |
146 | komponen = 0; % jumlah komponen utama yang dipertahankan |
147 | |
148 | proporsiValue = zeros(p, 1); |
149 | for j = 1:p |
150 | proporsiValue(j) = eValue(j,1) / Total; % mengukur seberapa besar kontribusi eigenvalue ke-j terhadap varians total data. |
151 | if(PK < 0.9) % Nilai PK tergantung peneliti --> 0.8 |
152 | PK = PK + proporsiValue(j); |
153 | komponen = komponen + 1; |
154 | end |
155 | end |
156 | |
157 | % Mengambil eigen vector dari komponen yang terpilih |
158 | VA_Komponen = VA_urut(:,1:komponen); |
159 | % W = variable komponen utama / principal component variable |
160 | W = Zr * VA_Komponen; % W ADALAH FINAL DATA YANG DISEBUTKAN DI PDF |
161 | |
162 | plot3(W(:,1), W(:,2), W(:,3), '*'); % PK < 0.9 |
163 | %plot(W(:,1), W(:,2), '*'); % PK < 0.8 |
164 | grid on |