Using a arff file for storing data

我怕爱的太早我们不能终老 提交于 2019-12-06 12:45:13

The code in the example treats each column in the table as an instance (so there are 29 instances, each with two attributes). It sounds like you want to treat each row as an instance (giving two instances, each with 29 attributes):

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();

I replaced SparseInstance with Instance, since almost all of the attribute values are non-zero. Note that in Weka 3.7 Instance has become an interface and DenseInstance should be used instead. Also, FastVector has been deprecated in favour of Java's ArrayList.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!