如何生成一个arff文件

arff文件可以用于Weka和Mulan。

原网站:http://weka.wikispaces.com/Creating+an+ARFF+file


代码:

/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 *    CreateInstances.java
 *    Copyright (C) 2009 University of Waikato, Hamilton, New Zealand
 *
 */

package wekaexamples.core;

import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instances;

import java.util.ArrayList;

/**
 * Generates an weka.core.Instances object with different attribute types.
 *
 * @author FracPete (fracpete at waikato dot ac dot nz)
 * @version $Revision$
 */
public class CreateInstances {

  /**
   * Generates the Instances object and outputs it in ARFF format to stdout.
   *
   * @param args	ignored
   * @throws Exception	if generation of instances fails
   */
  public static void main(String[] args) throws Exception {
    ArrayList<Attribute>	atts;
    ArrayList<Attribute>	attsRel;
    ArrayList<String>		attVals;
    ArrayList<String>		attValsRel;
    Instances			data;
    Instances			dataRel;
    double[]			vals;
    double[]			valsRel;
    int				i;

    // 1. set up attributes
    atts = new ArrayList<Attribute>();
    // - numeric
    atts.add(new Attribute("att1"));
    // - nominal
    attVals = new ArrayList<String>();
    for (i = 0; i < 5; i++)
      attVals.add("val" + (i+1));
    atts.add(new Attribute("att2", attVals));
    // - string
    atts.add(new Attribute("att3", (ArrayList<String>) null));
    // - date
    atts.add(new Attribute("att4", "yyyy-MM-dd"));
    // - relational
    attsRel = new ArrayList<Attribute>();
    // -- numeric
    attsRel.add(new Attribute("att5.1"));
    // -- nominal
    attValsRel = new ArrayList<String>();
    for (i = 0; i < 5; i++)
      attValsRel.add("val5." + (i+1));
    attsRel.add(new Attribute("att5.2", attValsRel));
    dataRel = new Instances("att5", attsRel, 0);
    atts.add(new Attribute("att5", dataRel, 0));

    // 2. create Instances object
    data = new Instances("MyRelation", atts, 0);

    // 3. fill with data
    // first instance
    vals = new double[data.numAttributes()];
    // - numeric
    vals[0] = Math.PI;
    // - nominal
    vals[1] = attVals.indexOf("val3");
    // - string
    vals[2] = data.attribute(2).addStringValue("This is a string!");
    // - date
    vals[3] = data.attribute(3).parseDate("2001-11-09");
    // - relational
    dataRel = new Instances(data.attribute(4).relation(), 0);
    // -- first instance
    valsRel = new double[2];
    valsRel[0] = Math.PI + 1;
    valsRel[1] = attValsRel.indexOf("val5.3");
    dataRel.add(new DenseInstance(1.0, valsRel));
    // -- second instance
    valsRel = new double[2];
    valsRel[0] = Math.PI + 2;
    valsRel[1] = attValsRel.indexOf("val5.2");
    dataRel.add(new DenseInstance(1.0, valsRel));
    vals[4] = data.attribute(4).addRelation(dataRel);
    // add
    data.add(new DenseInstance(1.0, vals));

    // second instance
    vals = new double[data.numAttributes()];  // important: needs NEW array!
    // - numeric
    vals[0] = Math.E;
    // - nominal
    vals[1] = attVals.indexOf("val1");
    // - string
    vals[2] = data.attribute(2).addStringValue("And another one!");
    // - date
    vals[3] = data.attribute(3).parseDate("2000-12-01");
    // - relational
    dataRel = new Instances(data.attribute(4).relation(), 0);
    // -- first instance
    valsRel = new double[2];
    valsRel[0] = Math.E + 1;
    valsRel[1] = attValsRel.indexOf("val5.4");
    dataRel.add(new DenseInstance(1.0, valsRel));
    // -- second instance
    valsRel = new double[2];
    valsRel[0] = Math.E + 2;
    valsRel[1] = attValsRel.indexOf("val5.1");
    dataRel.add(new DenseInstance(1.0, valsRel));
    vals[4] = data.attribute(4).addRelation(dataRel);
    // add
    data.add(new DenseInstance(1.0, vals));

    // 4. output data
    System.out.println(data);
  }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生成arff文件的主要步骤如下: 1. 导入arff文件格式的包: ``` import weka.core.Instances; import weka.core.converters.ArffSaver; ``` 2. 创建Instances对象: ``` Instances data = new Instances(new BufferedReader(new FileReader("data.arff"))); ``` 3. 设置特征属性: ``` // 设置属性值 ArrayList<Attribute> attributes = new ArrayList<Attribute>(); Attribute attribute1 = new Attribute("attribute1"); Attribute attribute2 = new Attribute("attribute2"); attributes.add(attribute1); attributes.add(attribute2); // 设置label值 ArrayList<String> labels = new ArrayList<String>(); labels.add("label1"); labels.add("label2"); // 设置类别属性 Attribute classAttribute = new Attribute("class", labels); // 将属性设置到Instances对象中 data.setClass(classAttribute); data.setAttributes(attributes); ``` 4. 生成arff文件: ``` ArffSaver saver = new ArffSaver(); saver.setInstances(data); saver.setFile(new File("data_new.arff")); saver.writeBatch(); ``` 完整代码如下: ``` import weka.core.Attribute; import weka.core.Instances; import weka.core.converters.ArffSaver; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.ArrayList; public class GenerateArff { public static void main(String[] args) throws Exception { // 读取数据文件 double[][] dataValues = {{1, 2}, {3, 4}, {5, 6}, {7, 8}}; Instances data = new Instances(new BufferedReader(new FileReader("data.arff"))); // 设置属性值 ArrayList<Attribute> attributes = new ArrayList<Attribute>(); Attribute attribute1 = new Attribute("attribute1"); Attribute attribute2 = new Attribute("attribute2"); attributes.add(attribute1); attributes.add(attribute2); // 设置label值 ArrayList<String> labels = new ArrayList<String>(); labels.add("label1"); labels.add("label2"); // 设置类别属性 Attribute classAttribute = new Attribute("class", labels); // 将属性设置到Instances对象中 data.setClass(classAttribute); data.setAttributes(attributes); // 生成arff文件 ArffSaver saver = new ArffSaver(); saver.setInstances(data); saver.setFile(new File("data_new.arff")); saver.writeBatch(); } } ``` 运行后,会在当前目录下生成一个名为data_new.arff文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值