Java 创建一个新的weka实例

Java 创建一个新的weka实例,java,weka,Java,Weka,我是Weka的新手,我正在尝试创建新实例,用以前训练过的多层感知器标记,我不太知道如何创建实例,所以我从训练数据中获取了第一个实例,然后通过更改atributes值对其进行了修改: //Opening the model public boolean abrirModelo(String ruta) { try { clasificador = (MultilayerPerceptron) weka.core.SerializationHelper.read(ruta)

我是Weka的新手,我正在尝试创建新实例,用以前训练过的
多层感知器
标记,我不太知道如何创建实例,所以我从训练数据中获取了第一个实例,然后通过更改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 "?";
    }

}
//打开模型
公共布尔abrirModelo(字符串ruta){
试一试{
clasificador=(MultilayerPerceptron)weka.core.SerializationHelper.read(ruta);
返回true;
}捕获(IOE异常){
System.out.println(“Fallo la讲师del archivo”);
返回false;
}捕获(ClassNotFoundException a){
System.out.println(“法洛铸造”);
返回false;
}捕获(例外e){
System.out.println(“错误con el castingo”);
返回false;
}
}
//获取要修改的第一个实例
公共布尔值INIALIZARINSTANCIAS(字符串指令){
archivo=新的ArffLoader();
试一试{
setFile(新文件(directorio));
structure=archivo.getStructure();
structure.setClassIndex(structure.numAttributes()-1);
actual=archivo.getNextInstance(结构);//要使用的实例
}捕获(IOEX异常){
系统输出打印(Algo salio mal al cargar la ESTR STRUCTURA de lsa instancias);
}
返回true;
}
//使用先前实例化的实际实例从本地数据创建实例,它是一个带有x和y的点列表
公共实例转换器(LineaderChivo线){
int size=line.getDatos().size();
对于(int i=0;i
可能这不是正确的方法,分类工具对我给出的所有实例都获得相同的类值,我认为问题在于我创建实例的方式

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

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

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

要手动创建某些实例,您需要:

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

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

    如果您已经有了可用的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
    
    //假设我们已经在名为dataset的变量中加载了arff
    Instance newInstance=新实例();
    对于(int i=0;i
    我不知道一年过去了,但现在Instance()构造函数受到保护,这意味着您无法使用它。有关更多参考信息,请查看API:(您仍然需要自己导航到实例文档)应该可以使用
    new SparseInstance()
    new DenseInstance()