创建一个新的weka实例 [英] Create a new weka Instance

查看:167
本文介绍了创建一个新的weka实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Weka的新手,我正在努力创建新的实例,用之前训练过的 MultilayerPerceptron 进行标记,我不知道如何创建一个实例,以便从训练数据中获取第一个实例,然后通过更改属性值对其进行修改:

I'm new in Weka, I'm triying to create new instances to be labeled with a previous trained MultilayerPerceptron, I did't know very much about how to create an instance so I got the first instance from my training data and then modified it by changing the atributes values:

//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为我给出的所有实例获得相同的类值,我认为问题是我创建实例的方式。

May be that is no the right way to do it, the clasifiers get the same class value to all the instances I give, I think the problem is the way I create the instance.

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

I hope someone could give me an advice, Thanks in advance

推荐答案

如果你已经有了arff结构并希望添加额外的实例,那么你可以通过以下方式实现:

If you already have the arff structure available and want to add extra instances , then you can do so by:

 //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实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆