问题
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 "?";
}
}
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
回答1:
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
回答2:
This link shows you how Weka suggests to build a new instance.
If you want to stick to your code and see if it is working, you could try creating some instances by hand. You could then classify the instances to see if you get the same result as with the instance created using your method.
To create some Instances by hand you would:
- Create a physical copy of your existing ".arff" training data
- Open the copy with a text editor
- Edit and save the X and Y values as you see fit
If these instances are classified differently then the ones you modify with your code, you can be sure that the instances are not being created correctly.
来源:https://stackoverflow.com/questions/19334494/create-a-new-weka-instance