Saturday, 14 September 2013

weka java API - uploading CSV error - wrong number of values. Read 21, expected 20, read Token[EOL], line 3

weka java API - uploading CSV error - wrong number of values. Read 21,
expected 20, read Token[EOL], line 3

I am trying to use WEKA API in my java code and trying to upload CSV file
and analyze the data. But I am facing the below IO Exception saying wrong
number of values in my CSV file.... So how can I fix this? What is the
problem with the CSV? Also, if I need to make any changes to the csv file,
I am worried how to generalize this as I want the program to accept all
csv files with data no matter of some fields missing. Solution please !
Error: Exception in thread "main" java.io.IOException: wrong number of
values. Read 21, expected 20, read Token[EOL], line 3 at
weka.core.converters.ConverterUtils.errms(ConverterUtils.java:912) at
weka.core.converters.CSVLoader.getInstance(CSVLoader.java:819) at
weka.core.converters.CSVLoader.getDataSet(CSVLoader.java:642) at
App.uploadCSV(App.java:25)
function uploadCSV() {
CSVLoader loader = new CSVLoader();
loader.setSource(new
File("C:\\Users\\soumya\\Downloads\\GimletDownload2013_03.csv"));
Instances data = loader.getDataSet();
// save ARFF
ArffSaver saver = new ArffSaver();
saver.setInstances(data);
saver.setFile(new
File("C:\\Users\\soumya\\Downloads\\GimletDownload2013_03.arff"));
saver.setDestination(new
File("C:\\Users\\soumya\\Downloads\\GimletDownload2013_03.arff"));
saver.writeBatch();
BufferedReader br=null;
br=new BufferedReader(new
FileReader("C:\\Users\\soumya\\Downloads\\GimletDownload2013_03.arff"));
Instances train=new Instances(br);
train.setClassIndex(train.numAttributes()-1);
br.close();
NaiveBayes nb=new NaiveBayes();
nb.buildClassifier(train);
Evaluation eval=new Evaluation(train);
eval.crossValidateModel(nb, train, 10, new Random(1));
System.out.println(eval.toSummaryString("\nResults\n=====\n",true));
System.out.println(eval.fMeasure(1)+" "+eval.precision(1)+"
"+eval.recall(1));
}

No comments:

Post a Comment