Distances: Array;
The Distances property returns distances of objects to cluster centers, to which they belong.
To determine the variant of selection of cluster initial centers, use the ISmKmeansClusterAnalysis.InitCenters property.
Add a link to the Stat system assembly.
Sub UserProc;
Var
KCA: SmKmeansClusterAnalysis;
Obj: ISlSeries;
Cls: IClustersType;
x1: Array[2] Of Double;
masDouble: Array Of Double;
masInteger: Array Of Integer;
res, i, j: Integer;
str: string;
Begin
KCA := New SmKmeansClusterAnalysis.Create;
// Set objects for consideration
Obj := KCA.Objects;
x1[0]:= 27; x1[1]:= 19;
Obj.Add.Value := x1;
x1[0]:= 11; x1[1]:= 46;
Obj.Add.Value := x1;
x1[0]:= 25; x1[1]:= 15;
Obj.Add.Value := x1;
x1[0]:= 36; x1[1]:= 27;
Obj.Add.Value := x1;
x1[0]:= 35; x1[1]:= 25;
Obj.Add.Value := x1;
x1[0]:= 10; x1[1]:= 43;
Obj.Add.Value := x1;
x1[0]:= 11; x1[1]:= 44;
Obj.Add.Value := x1;
x1[0]:= 36; x1[1]:= 24;
Obj.Add.Value := x1;
x1[0]:= 26; x1[1]:= 14;
Obj.Add.Value := x1;
x1[0]:= 26; x1[1]:= 14;
Obj.Add.Value := x1;
x1[0]:= 9; x1[1]:= 45;
Obj.Add.Value := x1;
x1[0]:= 33; x1[1]:= 23;
Obj.Add.Value := x1;
x1[0]:= 27; x1[1]:= 16;
Obj.Add.Value := x1;
x1[0]:= 10; x1[1]:= 47;
Obj.Add.Value := x1;
// Set the number of clusters
KCA.ClusterCount := 3;
// Set maximum number of iterations
KCA.MaxIt := 100;
// Set selection of initial centers for clusters
KCA.InitCenters := ClusterCentersType.FirstObject;
// Do not exclude repeated elements before clustering
KCA.ExcludeRepeated := False;
res := KCA.Execute;
If res <> 0 Then
Debug.WriteLine(KCA.Errors);
Else
Cls := KCA.Clusters;
Debug.WriteLine("=== Cluster centers ===");
For i := 0 To KCA.Clusters.Count-1 Do //by clusters
Debug.WriteLine(" " + "Cluster #" + (i+1).ToString + " ");
masDouble := Cls.Item(i).Center;
str := " " + "(" + masDouble[0].ToString;
For j := 1 To masDouble.Length-1 Do //by element size
str := str + ";" + masDouble[j].ToString;
End For;
str := str + ")";
Debug.WriteLine(str);
End For;
Debug.WriteLine("=== List of objects by clusters ===");
For i := 0 To KCA.ClusterCount - 1 Do //by clusters
Debug.WriteLine(" " + "Cluster #" + (i+1).ToString + " ");
masInteger := Cls.Item(i).ObjectsList;
str := " " + masInteger[0].ToString;
For j := 1 To Cls.Item(i).Size - 1 Do //by all objects in cluster
str := str + ", " + masInteger[j].ToString;
End For;
Debug.WriteLine(str);
End For;
Debug.WriteLine("=== Distances from objects to cluster centers ===");
Debug.WriteLine("Object - Distance");
For i := 0 To KCA.Distances.Length-1 Do
Debug.WriteLine(" " + i.ToString + " - " + KCA.Distances[i].ToString)
End For;
End If;
End Sub UserProc;
After executing the example the console window displays the following:
Cluster centers
List of objects by clusters
Distances from objects to cluster centers.
See also: