【发布时间】:2014-04-01 23:55:42
【问题描述】:
我需要打印在我的 Java 应用程序中将使用 Weka 的过滤方法应用到上传的文件后生成的 ARFF 文件。
Weka 中是否有任何方法或任何方式将 ARFF 文件打印为二维数组? 我需要打印参数名称和值。
【问题讨论】:
我需要打印在我的 Java 应用程序中将使用 Weka 的过滤方法应用到上传的文件后生成的 ARFF 文件。
Weka 中是否有任何方法或任何方式将 ARFF 文件打印为二维数组? 我需要打印参数名称和值。
【问题讨论】:
首先,您需要使用ArffReader 加载文件。这是 Weka javadocs 中的标准方法:
BufferedReader reader = new BufferedReader(new FileReader("file.arff"));
ArffReader arff = new ArffReader(reader);
Instances data = arff.getData();
data.setClassIndex(data.numAttributes() - 1);
然后你可以使用上面得到的Instances对象来遍历每个属性及其关联的值,边走边打印:
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));
}
这将导致如下输出:
attribute1: [value1, value2, value3]
attribute2: [value1, value2, value3]
【讨论】: