3

我是 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 "?";
    }

}

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

我希望有人能给我一个建议,提前谢谢

4

2 回答 2

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
于 2014-03-03T05:30:21.627 回答
-1

链接向您展示 Weka 如何建议构建一个新实例

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

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

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

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

于 2013-10-13T11:58:37.053 回答