Weka:SimpleKMeans実装Class to clusters evaluation検証
今日wekaを利用してクラスタリングを実現する際、javaを使用してClass to clusters evaluationを実現する方法について質問がありました.次はコアコードです.
package WekaProcess;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import weka.clusterers.ClusterEvaluation;
import weka.clusterers.EM;
import weka.clusterers.SimpleKMeans;
import weka.core.DistanceFunction;
import weka.core.EuclideanDistance;
import weka.core.Instances;
import weka.core.converters.ArffLoader;
import weka.filters.Filter;
public class StrongCluster {
public static void Kmeans(String ArffFile)
{
Instances ins = null;
Instances tempIns = null;
SimpleKMeans KM = null;
DistanceFunction disFun = null;
try{
/*
* 1.
*/
Instances data = new Instances(new BufferedReader(new
FileReader(ArffFile)));
data.setClassIndex(data.numAttributes() - 1);
//l ,
weka .filters.unsupervised.attribute.Remove filter = new weka.filters.unsupervised.attribute.Remove();
filter.setAttributeIndices("" + (data.classIndex() + 1));
filter.setInputFormat(data);
Instances dataClusterer = Filter.useFilter(data,filter);
// l clusterer, EM
/// EM clusterer = new EM();
// set further options for EM if necessary...
KM = new SimpleKMeans();
//
KM.setNumClusters(2);
KM.setSeed(10);
/*
* 3.
*/
//KM.buildClusterer(ins);
KM.buildClusterer(dataClusterer);
// l clusterer
ClusterEvaluation evals = new ClusterEvaluation();
evals.setClusterer(KM);
evals.evaluateClusterer(data);
// l
System.out.println(evals.clusterResultsToString());
}catch(Exception e){
e.printStackTrace();
}
}
public static void DoCluster(String CSVfile)
{
System.out.println("Strong Cluster IS STARTED!!");
//String CSVfile="D://fre21.csv";
String InputArff=InputCSV.CSV2Arff(CSVfile);
Kmeans(InputArff);
}
public static void main(String[] args) {
System.out.println("Strong Cluster IS STARTED!!");
String CSVfile="D://fre21.csv";
String InputArff=InputCSV.CSV2Arff(CSVfile);
Kmeans(InputArff);
//Kmeans();
}
}