创建一个新的weka实例

2024-06-10

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

}

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

希望有人能给我建议,先谢谢了


如果您已经有了可用的 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

创建一个新的weka实例 的相关文章

随机推荐