我必须保存一个由输入数据和集群分配组成的新文件
我现在可以将输入数据保存到新文件中
我可以打印clusterNum:分配,我找不到如何保存集群分配
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import weka.clusterers.ClusterEvaluation;
import weka.clusterers.SimpleKMeans;
import weka.core.Instances;
import weka.core.converters.CSVSaver;
import weka.core.converters.ConverterUtils.DataSource;
import weka.filters.unsupervised.attribute.Remove;
/**
*
* @author x2010s
*/
public class automatizacion {
public static BufferedReader readDataFile(String filename) {
BufferedReader inputReader = null;
try {
inputReader = new BufferedReader(new FileReader(filename));
} catch (FileNotFoundException ex) {
System.err.println("File not found: " + filename);
}
return inputReader;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception{
SimpleKMeans kmeans = new SimpleKMeans();
kmeans.setSeed(10);
//important parameter to set: preserver order, number of cluster.
kmeans.setPreserveInstancesOrder(true);
kmeans.setNumClusters(2);
DataSource source = new DataSource("C:/Users/x2010s/Documents/NetBeansProjects/transmisibles.csv");
Instances data = source.getDataSet();
Remove removeFilter = new Remove();
removeFilter.setAttributeIndicesArray("Age");
removeFilter.setInvertSelection(true);
removeFilter.setInputFormat(data);
kmeans.buildClusterer(data);
ClusterEvaluation eval = new ClusterEvaluation();
eval.setClusterer(kmeans);
eval.evaluateClusterer(data);
// 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 \n", i, clusterNum);
i++;
}
System.out.println("\n Cluster Evaluation: "+eval.clusterResultsToString());
CSVSaver saver = new CSVSaver();
saver.setInstances(data);
saver.setFile(new File("C:/Users/x2010s/Documents/NetBeansProjects/transmisiblesp.csv"));
saver.writeBatch();
// TODO code application logic here
}
我还没有找到一种方法来将群集分配也保存在同一文件中