【问题标题】:Create a new weka Instance创建一个新的 weka 实例
【发布时间】:2013-10-20 11:58:47
【问题描述】:

我是 Weka 的新手,我正在尝试创建新的实例来标记以前训练的 MultilayerPerceptron,我不太了解如何创建实例,所以我从我的训练数据,然后通过更改属性值对其进行修改:

//Opening the model
public boolean abrirModelo(String ruta) {
    try {

        clasificador = (MultilayerPerceptron) weka.core.SerializationHelper.read(ruta); 

        return true;
    } catch (IOException e) {
        System.out.println("Fallo la lectura del archivo");
        return false;
    } catch (ClassNotFoundException a) {
        System.out.println("Fallo el casting");
        return false;
    }catch(Exception e){
        System.out.println("Error con el castingo");
        return false;
    }
}

//getting the first instance to be modified
public boolean inicializarInstancias(String directorio){
   archivo = new ArffLoader();
    try {
        archivo.setFile(new File(directorio));
        structure = archivo.getStructure();
        structure.setClassIndex(structure.numAttributes() - 1);
        actual = archivo.getNextInstance(structure); //instance to be used
    } catch (IOException ex) {
        System.out.println("Algo salio mal al cargar la estructura de lsa instancias");
    }
    return true;
}

//creating an instance from my local data using the previous instantiated actual instance, it is a List of Points with x and y
public Instance convertir(LineaDeArchivo line) {
    int size = line.getDatos().size();
    for (int i = 0; i < size; i+=2) {
        actual.setValue(i, line.getDatos().get(i).x);
        actual.setValue(i + 1, line.getDatos().get(i).y);
    }   
    return actual;
}
//getting the class 
public String getClase(Instance e){
    try{
        double clase;
        clase = clasificador.classifyInstance(e);
        return structure.classAttribute().value((int) clase);
    }catch(Exception a){
        System.out.println("Algo salio mal con la clasificacion");
        return "?";
    }

}

可能这不是正确的方法,分类器对我给出的所有实例都获得相同的类值,我认为问题在于我创建实例的方式。

希望有人能给我建议,在此先感谢

【问题讨论】:

    标签: java weka


    【解决方案1】:

    如果你已经有了 arff 结构并且想要添加额外的实例,那么你可以这样做:

     //assuming we already have arff loaded in a variable called dataset
     Instance newInstance  = new Instance();
     for(int i = 0 ; i < dataset.numAttributes() ; i++)
     {
    
         newInstance.setValue(i , value);
         //i is the index of attribute
         //value is the value that you want to set
     }
     //add the new instance to the main dataset at the last position
     dataset.add(newInstance);
     //repeat as necessary
    

    【讨论】:

    • 我不知道一年过去了,但现在 Instance() 构造函数受到保护,这意味着你不能使用它。如需更多参考,请查看 API:weka.sourceforge.net/doc.stable(您仍需自行导航至实例文档)
    • 应该可以改用new SparseInstance()new DenseInstance()
    【解决方案2】:

    这个link 向您展示了 Weka 如何建议构建一个新实例

    如果您想坚持自己的代码并查看它是否正常工作,您可以尝试手动创建一些实例。然后,您可以对实例进行分类,以查看您是否获得与使用您的方法创建的实例相同的结果。

    要手动创建一些实例,您可以:

    1. 创建现有“.arff”训练数据的物理副本
    2. 使用文本编辑器打开副本
    3. 根据需要编辑并保存 X 和 Y 值

    如果这些实例的分类与您使用代码修改的实例不同,您可以确定这些实例没有正确创建。

    【讨论】:

      猜你喜欢
      • 2015-07-16
      • 2011-09-07
      • 2012-08-20
      • 1970-01-01
      • 2012-11-08
      • 2012-06-22
      • 2014-05-19
      • 1970-01-01
      • 2012-05-31
      相关资源
      最近更新 更多