Java 创建一个新的 weka 实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19334494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a new weka Instance
提问by Emmanuel Vázquez
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:
我是 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 "?";
}
}
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
我希望有人可以给我建议,在此先感谢
回答by Walter
This linkshows you how Weka suggests to build a new instance.
此链接向您展示 Weka 如何建议构建新实例。
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
- 创建现有“.arff”训练数据的物理副本
- 使用文本编辑器打开副本
- 根据需要编辑并保存 X 和 Y 值
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.
如果这些实例的分类与您使用代码修改的实例不同,您可以确定这些实例没有被正确创建。
回答by abhinna11
If you already have the arff structure available and want to add extra instances , then you can do so by:
如果您已经有可用的 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