I had an input file having 45311 instance. After applying my programing task, when I write it back in a new file, it actually writes 43371 instances only. It runs successfully, but where are my remaining instances?
package kmean;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import weka.clusterers.SimpleKMeans;
import weka.core.Instances;
public class Kmean {
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 IOException, Exception {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter("perturbed1.csv"));
}
catch (IOException e) {
}
SimpleKMeans kmeans = new SimpleKMeans();
kmeans.setSeed(10);
//important parameter to set: preserver order, number of cluster.
kmeans.setPreserveInstancesOrder(true);
kmeans.setNumClusters(5);
BufferedReader datafile = readDataFile("elecNormNew.arff");
// BufferedReader datafile = readDataFile("perturbed.csv");
Instances data = new Instances(datafile);
kmeans.buildClusterer(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();
StringBuilder sb = new StringBuilder();
int i=0;
for(int clusterNum : assignments) {
// System.out.printf("Instance %d -> Cluster %d \n", i, clusterNum);
sb.append(i);
sb.append(";");
sb.append(clusterNum);
sb.append("\n");
//System.out.printf("\n");
i++;
}
System.out.println(sb.toString());
writer.write(sb.toString()+"\n");
// TODO code application logic here
}
}
You should call writer.close()
at the end after writing all data.