开发者

how to implement k-means for simple grouping in java

I would like to know simple k-means algorithm in java. I want to use k-means only for grouping one d开发者_如何学编程imensional 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


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.


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.)


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++;
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜