Domanda

I need to print an ARFF file generated after applying a filter method using Weka to an uploaded file in my Java application.

Is there any method in Weka or any way to print the ARFF file as a two dimensional array? I need to print the parameter name and the values.

È stato utile?

Soluzione

First you need to load the file using an ArffReader. Here's the standard way to do so from the Weka javadocs:

BufferedReader reader = new BufferedReader(new FileReader("file.arff"));
ArffReader arff = new ArffReader(reader);
Instances data = arff.getData();
data.setClassIndex(data.numAttributes() - 1);

Then you can use the Instances object obtained above to iterate through each attribute and its associated values, printing as you go:

for (int i = 0; i < data.numAttributes(); i++)
{
    // Print the current attribute.
    System.out.print(data.attribute(i).name() + ": ");

    // Print the values associated with the current attribute.
    double[] values = data.attributeToDoubleArray(i);
    System.out.println(Arrays.toString(values));
}

This will result in output like the following:

attribute1: [value1, value2, value3]
attribute2: [value1, value2, value3]
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top