Menyambung dari artikel sebelumnya tentang konsep k-means data mining focus pada tulisan sekarang adalah bagaimana cara mengimplementasikan perhitungan algoritma k-means centroid dan jarak kedalam source code program. Berikut source code langkah-langkah menghitung nilai centroid dan jarak algoritma k-means:
Menyiapkan data training untuk membentuk centriod
FSample
: TFoatArray2D;
FCentroidData : array of TFoatArray2D;
FDistance : array
of TFoatArray2D;
const
GRID_COL1 = 3;
GRID_ROW1 = 6;
procedure InitArray2D(var aValue : TFoatArray2D; aCol, aRow
: integer);
var i : integer;
begin
SetLength(aValue,
aCol);
for i := 0 to aCol-1
do
SetLength(aValue[i], aRow);
end;
procedure ClearArray(var aValue : TFoatArray2D);
var i, j : integer;
begin
j := length(aValue);
for i := j-1 downto
0 do
SetLength(aValue[i], 0);
SetLength(aValue,
0);
end;
// contoh data training dalam bentuk array 2D
SAMPLE_DATA : array
[0..GRID_COL1-1, 0.. GRID_ROW1-1] of double =
((459, 356, 493, 265, 297, 834),
(631, 489, 187, 614, 244, 402),
(288, 539, 629, 379, 573, 106));
Dibawah ini merupakan kode program untuk perhitungan
centroid
procedure CalcCentoid(dtSample, dtCentroid : TFoatArray2D;
Step : array of integer{TIntArray});
var
i, j, k : integer;
row_pos, old_row_pos
: integer;
begin
old_row_pos := 0;
for i := 0 to
length(dtCentroid) -1 do
begin
if (i > 0)
and ((i mod 3) = 0) then
old_row_pos :=
0;
for j := 0 to
length(dtCentroid[i]) -1 do
begin
row_pos :=
old_row_pos;
for k := 0
to step[i]-1 do
begin
dtCentroid[i, j] := (dtCentroid[i, j] + dtSample[j, row_pos]);
inc(row_pos);
end;
dtCentroid[i, j] := dtCentroid[i, j] / step[i];
end;
old_row_pos :=
old_row_pos + step[i];
end;
end;
berikut contoh program menghitung jarak k-means algoritma
data mining
procedure CalcDistance(dtSample, dtCentroid, dtDistance :
TFoatArray2D);
var
i, j, k : integer;
Value : double;
begin
for i := 0 to
length(dtDistance) - 1 do
for j := 0 to
length(dtDistance[i]) - 1 do
begin
Value := 0;
for k := 0 to
length(dtCentroid[i]) - 1 do
Value :=
Value + ((dtSample[k, j]-dtCentroid[i,k])*(dtSample[k, j]-dtCentroid[i,k]));
dtDistance[i,
j] := sqrt(Value);
end;
end;
Contoh pemanggilan procedure CalcCentoid dan CalcDistance untuk menghitung centroid dan jarak
procedure TForm1.Button1Click(Sender: TObject);
var
i, j, k : integer;
begin
// Inisialisasi data
array
InitArray2D(FSample,
GRID_COL1, GRID_ROW1);
SetLength(FCentroidData, 1);
InitArray2D(FCentroidData[0], GRID_ROW1-1, GRID_COL1);
// Isi data sample
ke array
for i := 0 to
length(FSample) -1 do
for j := 0 to
length(FSample[i]) -1 do
FSample[i, j] :=
SAMPLE_DATA[i,j];
// Perhitungan
Centroid Awal
CalcCentoid(FSample,FCentroidData[0],[2,2,2,3,3]);
//Init Data distance
centroid
SetLength(FDistance,
1);
InitArray2D(FDistance[0], GRID_ROW1-1, GRID_ROW1);
//Perhitungan Jarak
CalcDistance(FSample, FCentroidData[0], FDistance[0]);
// test 2
iTERASI --> 2 TERASI Tok....
for i := 1 to 2 do
begin
SetLength(FCentroidData, length(FCentroidData)+1);
InitArray2D(FCentroidData[i], GRID_ROW1-1, GRID_COL1);
SetLength(FDistance, length(FDistance)+1);
InitArray2D(FDistance[i], GRID_ROW1-1, GRID_ROW1);
CalcCentoid(FSample,FCentroidData[i],[2,2,2,2,4]);
CalcDistance(FSample, FCentroidData[i], FDistance[i]);
end;
// Hapus data Array
ClearArray(FSample);
for i :=
length(FCentroidData)-1 downto 0 do
ClearArray(FCentroidData[i]);
for i :=
length(FDistance)-1 downto 0 do
ClearArray(FDistance[i]);
Setlength(FCentroidData, 0);
SetLength(FDistance,
0);
end;
end.
demikian contoh program prototipe perhitungan centroid dan jarak Algoritma
k-means, semoga source code diatas dapat menjadi referensi belajar cara
menerapkan perhitungan metode k-means.
No comments:
Post a Comment