【问题标题】:how to convert an arff file to a matrix如何将arff文件转换为矩阵
【发布时间】:2019-08-21 19:30:23
【问题描述】:

我有 arff 文件需要转换为矩阵。我已经将文件转换为数组,但无法将其转换为矩阵。有人可以帮忙吗? 下面我有我用来将数据转换为数组的代码

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

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

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

【问题讨论】:

  • 标准 Java 中没有 Matrix 类型。所以你想要什么?更具体。

标签: java data-mining arff


【解决方案1】:

假设您想要double[][],请执行以下操作:

收集ArrayList&lt;double[]&gt;中的所有double[]

最后,使用list.toarray(new double[list.size()][])或类似方法将动态长度的列表转换为固定长度的数组。

或者您直接分配输出数组,因为Instances 已经知道属性和实例的数量。

请注意,double[][] 严格来说并不是一个矩阵。它可能是参差不齐的,即行可能有不同的长度。此外,上面的代码会产生一个转置矩阵,可能不是你所期望的。您可能希望迭代实例并改用instance.toDoubleArray()

无论如何,请查看attributeToDoubleArray 的源代码,了解 Weka 内部在做什么,为您提供一些如何继续的想法。

【讨论】:

    猜你喜欢
    • 2013-10-03
    • 2014-05-31
    • 2015-06-10
    • 1970-01-01
    • 2014-06-11
    • 2014-02-10
    • 1970-01-01
    • 2020-08-24
    • 2012-02-29
    相关资源
    最近更新 更多