package example;
import Cluster.KMeansCluster;
/**
* @author Marios Michailidis 2013
* @Goal The purpose of this example class is to show you
* how to use the K-Means Cluster algorithm from
* the KazAClasses package. In this example we will find the best 3 clusters
* for a set of 2 variables.
*/
public class KMeans_Cluster_Example {
/**
* @param args This is the main method that runs the example
*/
public static void main(String[] args) {
/*
* Initialize a double Array for the 2 variables
*/
double variables [][]={{4,12},{10,12},{5,10},{8,7},{5,3},{5,4},
{2,11},{5,4},{3,8},{6,2},{7,4},{10,8},{8,9},
{10,9}};
/*
* Initiate the K-Means Cluster class
*/
KMeansCluster KMeans= new KMeansCluster();
/*
* Initiate the KMeansCluster main method
* that computes all the important statistics. First
* we need to specify 3 things as this is an iterative
* process :
* 1) The number of clusters we want
* 2)The maximum iterations we allow the algorithm to run
* in case it has not found the optimum solution.
* 3)The precision-how strict we are in defining the optimum solution
*/
int number_of_custers=3;
int maximum_iterations_to_stop=20;
double precision=0.001;
/*
* Now we run the method
*/
KMeans.CreateKClusters(variables, number_of_custers, maximum_iterations_to_stop, precision);
/*
* Now we can get the details of the 3 clusters (cendroids)
* in terms of means.
*/
double means [][]=KMeans.Getcluster_means();
/*
* We'll print the results of the 3
* cendroids.
*/
for (int i=0; i< means.length; i++) {
System.out.println("The Cendroid " + i + " has mean of variable 1: " +means[i][0] + " and mean of variable 2: " +means[i][1]);
}
/*
* It will print:
* The Cendroid 0 has mean of variable 1: 9.2 and mean of variable 2: 9.0
The Cendroid 1 has mean of variable 1: 3.5 and mean of variable 2: 10.25
The Cendroid 2 has mean of variable 1: 5.6 and mean of variable 2: 3.4
*/
/*
* We can get the sum of the minimum euclidean distances
*/
double minimum_euclidean_distances=KMeans.GetMinimumSquaredDifference();
/*
* Print it
*/
System.out.println(" The minimum Euclidian Distance is " + minimum_euclidean_distances);
/*
* It will print:
* The minimum Euclidian Distance is 21.437838875988845
*/
/*
* We can see the classification results
* of each observation, that is the closest
* cluster for each observation.
*/
String Classification []=KMeans.GetClassification();
/*
* Ultimately we can get the probability of each observation
* to belong in each cluster. That is similar to K-Means-Fuzzy
* algorithm as a logic.
*/
double probabilities_of_clusters[][]=KMeans.GetKFuzzyClassifixationProbabilities();
/*
* Let's print the probabilities along with the
* classification prediction.
*/
for(int i=0; i