Concept behind arff and how to read weka arff in java? -
why use arff? , please give sample code read arff file , make use of in java.
i found following snippet of code in weka site:
bufferedreader reader = new bufferedreader(new filereader("/some/where/file.arff")); arffreader arff = new arffreader(reader); instances data = arff.getdata(); data.setclassindex(data.numattributes() - 1); what after that? can explain what's going on above? how can access data file? , weka site mentions 2 different usages namely, batch , incremental. what's difference between two?
well, use arff because it's simple file format, csv file header describing data , it's usual way save/read data using weka.
the sample code read arff file 1 provided, if want make use of instances loaded should work data. print them: system.out.println(data); check lot of examples on how work data (classification, clustering, etc) here.
the code using loads arff file in standard bufferedreader, creates arffreader instance (arff) reads data reader, after use getdata method return data in instances object (called data). finally, set attribute class (the last 1 in arff file).
if want iterate instances object , retrieve each instance:
for (int = 0; <= data.numinstances - 1; i++) { instance instance = data.getinstance(i); system.out.println(instance.stringvalue(0)); //get attribute 0 string } you talking batch , incremental read arff file. batch mode reads arff file , incremental mode gives chance read each instance (line) of arff file , add manually.
code incremental mode:
bufferedreader reader = new bufferedreader(new filereader("/some/where/file.arff")); arffreader arff = new arffreader(reader, 1000); instances data = arff.getstructure(); data.setclassindex(data.numattributes() - 1); instance inst; while ((inst = arff.readinstance(data)) != null) { data.add(inst); }
Comments
Post a Comment