문제

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.

도움이 되었습니까?

해결책

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]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top