java - 使用arff文件存储数据

标签 java weka

我正在使用此示例为我的 weka projext enter link description here 创建 .arff 文件.

double[][] data = {{4058.0, 4059.0, 4060.0, 214.0, 1710.0, 2452.0, 2473.0, 2474.0, 2475.0, 2476.0, 2477.0, 2478.0, 2688.0, 2905.0, 2906.0, 2907.0, 2908.0, 2909.0, 2950.0, 2969.0, 2970.0, 3202.0, 3342.0, 3900.0, 4007.0, 4052.0, 4058.0, 4059.0, 4060.0}, 
                       {19.0, 20.0, 21.0, 31.0, 103.0, 136.0, 141.0, 142.0, 143.0, 144.0, 145.0, 146.0, 212.0, 243.0, 244.0, 245.0, 246.0, 247.0, 261.0, 270.0, 271.0, 294.0, 302.0, 340.0, 343.0, 354.0, 356.0, 357.0, 358.0}};

    int numInstances = data[0].length;

    FastVector atts = new FastVector();
    ArrayList<Instance> instances = new ArrayList<Instance>();
    for (int dim = 0; dim < 2; dim++) {
        // Create new attribute / dimension
        Attribute current = new Attribute("Attribute" + dim, dim);
        // Create an instance for each data object


        if (dim == 0) {
            for (int obj = 0; obj < numInstances; obj++) {
                instances.add(new SparseInstance(0));

            }
        }

        // Fill the value of dimension "dim" into each object
        for (int obj = 0; obj < numInstances; obj++) {
            instances.get(obj).setValue(current, data[dim][obj]);
            System.out.println(instances.get(obj));
        }

        // Add attribute to total attributes
        atts.addElement(current);

    }

     // Create new dataset
    Instances newDataset = new Instances("Dataset", atts, instances.size());

    // Fill in data objects
    for (Instance inst : instances) {
        newDataset.add(inst);       
    }

    BufferedWriter writer = new BufferedWriter(new FileWriter("test.arff"));
    writer.write(newDataset.toString());
    writer.flush();
    writer.close();
}

我注意到结果格式将行元素放入 vector 在 .arff 文件的列中。我想将整行放在 .arff 文件的第一行中。我怎样才能这样做?对于我的情况,二维 vector 的最后一列表示行数据的标签。

我的 arff 文件的预期结果:

4058.0, 4059.0, 4060.0, 214.0, 1710.0, 2452.0, 2473.0, 2474.0, 2475.0, 2476.0, 2477.0, 2478.0, 2688.0, 2905.0, 2906.0, 2907.0, 2908.0, 2909.0, 2950.0, 2969.0, 2970.0, 3202.0, 3342.0, 3900.0, 4007.0, 4052.0, 4058.0, 4059.0, 4060.0, 1 // for example the first row
 19.0, 20.0, 21.0, 31.0, 103.0, 136.0, 141.0, 142.0, 143.0, 144.0, 145.0, 146.0, 212.0,  
 243.0, 244.0, 245.0, 246.0, 247.0, 261.0, 270.0, 271.0, 294.0, 302.0, 340.0, 343.0, 
 354.0, 356.0, 357.0, 358.0, 0 // the second row.

最佳答案

示例中的代码将表中的每一列视为一个实例(因此有 29 个实例,每个实例都有两个属性)。听起来您想将每一行视为一个实例(给出两个实例,每个实例有 29 个属性):

double[][] data = {
                    {4058.0, 4059.0, ... }, /* first instance */
                    {19.0, 20.0, ... }      /* second instance */
                  };

int numAtts = data[0].length;
FastVector atts = new FastVector(numAtts);
for (int att = 0; att < numAtts; att++)
{
    atts.addElement(new Attribute("Attribute" + att, att));
}

int numInstances = data.length;
Instances dataset = new Instances("Dataset", atts, numInstances);
for (int inst = 0; inst < numInstances; inst++)
{
    dataset.add(new Instance(1.0, data[inst]));
}

BufferedWriter writer = new BufferedWriter(new FileWriter("test.arff"));
writer.write(dataset.toString());
writer.flush();
writer.close();

我将 SparseInstance 替换为 Instance,因为几乎所有属性值都不为零。请注意,在 Weka 3.7 中,Instance 已成为一个接口(interface),应使用 DenseInstance 来代替。此外,FastVector 已被弃用,取而代之的是 Java 的 ArrayList

关于java - 使用arff文件存储数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21723013/

相关文章:

java - 部署到heroku时,它在pom.xml中看不到我的jar文件或maven依赖项

java - 与使用带有多个分隔符的 split 感到困惑

java - 仅在 Activity 持续时间内唯一的任务队列任务名称

java - 如何获得weka簇质心的值(value)

weka - 在weka中的StringToWordVector过滤器中保留属性的单词

java - n-puzzle DFS 解决方案适用于 2X2,但适用于 3X3 StackOverflowError

java - ObservableList.remove(index, index+1) 导致 UnsupportedOperationException 并仍然从列表中删除

java - "Convert"Weka 的 java 代码命令行

python - 包含字符串和数值的数据集中的特征选择?

java - CSVLoader--错误,不在 CLASSPATH 中?