从java对象获取WEKA实例

时间:2016-07-12 09:19:41

标签: java weka

我的java代码中有一个Set<User>对象。这是User类的来源:

public class User {
  private String id;
  private String bDate;
  private String name;
  private String sName;
  private String description;
  private String city;

  ... getters/setters
}

您能举例说明一下,如何在WEKA的Clusterer中使用它?

1 个答案:

答案 0 :(得分:1)

首先,您必须从此处将opencsv包含在项目中:http://opencsv.sourceforge.net/,以便以csv格式打印数据。

我使用一个编写器来编写csv数据,另一个编写arff格式。

        writer = new CSVWriter(new OutputStreamWriter(new FileOutputStream(pathtoyourfile+".arff",true), "utf-8"));
        writer2 =  new PrintWriter(pathtoyourfile+".arff", "UTF-8");
        writer2.println("@Relation Users");
        writer2.println("");
        writer2.println("@attribute id string");
        writer2.println("@attribute bDate string");
        writer2.println("@attribute name string");
        writer2.println("@attribute sName string");
        writer2.println("@attribute description string");
        writer2.println("@attribute city string");          
        writer2.println("");
        writer2.println("@data");
        writer2.close();

        String[] entries =  new String[6];
        //lets say you have a list with users
        List<User>  users = .....

        for(int i=0;i<users.size();i++){
             entries[0]= users.get(i).getId();
             entries[1]= users.get(i).getbDate();
             .....
             entries[5]= users.get(i).getCity();
             writer.writeNext(entries);
        }
        writer.close();