Reputation: 51
I would like to know simple k-means algorithm in java. I want to use k-means only for grouping one dimensional array not multi. For example, before grouping the array consists of 2,4,7,5,12,34,18,25 if we want four group then we got group 1: 2,4,5 group 2: 7,12 group 3: 18,25 group 4: 34
Upvotes: 3
Views: 5955
Reputation: 3520
You can check my software : SPMF data mining software.
It offers an efficient implementation of KMeans in just 3 files so it should be easy to understand.
The software also offers many other algorithms. But you don't need them.
But another thing is that there is also a graphical user interface for launching KMeans and the other algorithms.
Upvotes: 0
Reputation: 21
You can implement k-Means as:
SimpleKMeans kmeans = new SimpleKMeans();
kmeans.setSeed(10);
// This is the important parameter to set
kmeans.setPreserveInstancesOrder(true);
kmeans.setNumClusters(numberOfClusters);
kmeans.buildClusterer(instances);
// This array returns the cluster number (starting with 0) for each instance
// The array has as many elements as the number of instances
int[] assignments = kmeans.getAssignments();
int i=0;
for(int clusterNum : assignments) {
System.out.printf("Instance %d -> Cluster %d", i, clusterNum);
i++;
}
Upvotes: 1
Reputation: 719641
The standard (heuristic) algorithm for K-means clustering is presented on the Wikipedia page, together with links to variations and some existing implementations.
(This is programming forum, so it is reasonable to assume that you are capable of writing Java code yourself ... if you cannot find an existing implementation that it is suitable.)
Upvotes: 1
Reputation: 10183
You can take a look at the Weka implementation or simply use the Weka API if all you need are the clusters and not the implementation.
Upvotes: 1