2016-09-07 43 views
-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的假訊息。我的代碼在哪裏做錯了?我已經在搜索教程和搜索答案,但我找不到一個。

謝謝。

+0

無關:你應該創建一個數組/列表來容納你的**屬性**對象。命名a1,a2,...總是一個很好的跡象表明你做錯了什麼。你看,如果這些屬性已經在列表中,例如你不需要對attributes.addElement進行6次調用。 – GhostCat

回答

0

感謝您的評論GhostCat。我得到朋友的幫助解決了這個問題。更新後的代碼如下:

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 attr2 = new Attribute("bitrate"); 
    Attribute attr3 = new Attribute("resolution", (FastVector) null); 
    //Attribute attr4 = new Attribute("fps", (FastVector) null); 
    Attribute attr4 = new Attribute("fps"); 
    //Attribute attr5 = new Attribute("duration", (FastVector) null); 
    Attribute attr5 = new Attribute("duration"); 
    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("multimedia", attributes, 0); 
    testing.setClassIndex(testing.numAttributes() - 1); 

    double[] values = new double[testing.numAttributes()]; 
    values[0] = testing.attribute(0).addStringValue(type); 
    values[1] = Integer.parseInt(bitrate); 
    values[2] = testing.attribute(2).addStringValue(resolution); 
    values[3] = Integer.parseInt(fps); 
    values[4] = Integer.parseInt(duration); 

    Instance test = new Instance(1, values); 
    testing.add(test); 

    System.out.println("The instance: "+testing); 

    try { 
     Classifier cls = (Classifier) weka.core.SerializationHelper.read(rootPath+"multimedia.model"); 
     //double myValue = cls.classifyInstance(testing.lastInstance()); 
     //double myValue = cls.classifyInstance(testing.firstInstance()); 
     //String prediction = testing.classAttribute().value((int) myValue); 
     System.out.println(cls.classifyInstance(testing.firstInstance())); 
     //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(); 
    } 
} 

結果如下:

Received JSON : {"id":"1","duration":"30","flag":"start","fps":"24","bitrate":"128","resolution":"720p","type":"video"} 

==Service parameter information== 
Service ID : 1 
Type : video 
Bitrate : 128 
Resolution : 720p 
FPS : 24 
Duration : 30 
The instance: @relation multimedia 

@attribute type string 
@attribute bitrate numeric 
@attribute resolution string 
@attribute fps numeric 
@attribute duration numeric 
@attribute class string 

@data 
video,128,720p,24,30,*WEKA*DUMMY*STRING*FOR*STRING*ATTRIBUTES* 
0.0 

看來,我們根據新的數據已經創建實例。但是,它不能按照型號進行分類併產生WEKA DUMMY STRING對於STRING屬性*。你可以或任何人幫助我,爲什麼這樣的結果?謝謝...