-3
我有一個通過WEKA GUI生成並計劃在我的WEKA JAVA代碼中使用它的J48模型。我想使用該模型來即時預測我的數據。我的如下代碼:我的Weka Java代碼結果* WEKA * DUMMY * STRING * FOR * STRING *屬性*
public static void dt(String type, String bitrate, String resolution, String fps, String duration){
String rootPath="/home/weka/Documents/";
Attribute attr1 = new Attribute("type", (FastVector) null);
Attribute attr2 = new Attribute("bitrate", (FastVector) null);
Attribute attr3 = new Attribute("resolution", (FastVector) null);
Attribute attr4 = new Attribute("fps", (FastVector) null);
Attribute attr5 = new Attribute("duration", (FastVector) null);
Attribute attr6 = new Attribute("class", (FastVector) null);
FastVector attributes = new FastVector();
attributes.addElement(attr1);
attributes.addElement(attr2);
attributes.addElement(attr3);
attributes.addElement(attr4);
attributes.addElement(attr5);
attributes.addElement(attr6);
Instances testing = new Instances("Test-dataset", attributes, 0);
testing.setClassIndex(testing.numAttributes() - 1);
double[] values = new double[testing.numAttributes()];
values[0] = testing.attribute(0).addStringValue(type);
values[1] = testing.attribute(1).addStringValue(bitrate);
values[2] = testing.attribute(2).addStringValue(resolution);
values[3] = testing.attribute(3).addStringValue(fps);
values[4] = testing.attribute(4).addStringValue(duration);
Instance inst = new Instance(1.0, values);
inst.setValue(testing.attribute(0), values[0]);
inst.setValue(testing.attribute(1), values[1]);
inst.setValue(testing.attribute(2), values[2]);
inst.setValue(testing.attribute(3), values[3]);
inst.setValue(testing.attribute(4), values[4]);
System.out.println("The instance: "+inst);
testing.add(inst);
try {
Classifier cls = (Classifier) weka.core.SerializationHelper.read(rootPath+"multimedia.model");
double myValue = cls.classifyInstance(testing.lastInstance());
String prediction = testing.classAttribute().value((int) myValue);
System.out.println("The predicted value of the data = "+prediction);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我的代碼的結果是:
==Service parameter information==
Service ID : 1
Type : audio
Bitrate : 96
Resolution : 0
FPS : 0
Duration : 20
The instance: 1,1,1,1,1,0
The predicted value of the data = *WEKA*DUMMY*STRING*FOR*STRING*ATTRIBUTES*
看來,我的價值不包含在該實例並導致Weka的假訊息。我的代碼在哪裏做錯了?我已經在搜索教程和搜索答案,但我找不到一個。
謝謝。
無關:你應該創建一個數組/列表來容納你的**屬性**對象。命名a1,a2,...總是一個很好的跡象表明你做錯了什麼。你看,如果這些屬性已經在列表中,例如你不需要對attributes.addElement進行6次調用。 – GhostCat