创建一个新的weka实例

我是Weka的新手,我正在努力创建新的实例以使用之前训练过的MultilayerPerceptron进行标记,我不太了解如何创建实例,所以我从训练数据中获得了第一个实例然后y通过更改atributes值来修改它:

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

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

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

如果您已经拥有可用的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 

此链接向您展示了Weka如何建议构建新实例

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

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

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

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