2016-09-07 11 views
-3

私はWEKA GUIを使用して生成し、WEKA JAVAコードで使用する予定のJ48モデルを用意しています。私はそのモデルを使用して、その場でデータを予測したいと思っています。次のように私のコード:My 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* 

私の値は、インスタンスに含まれており、ウェカダミーメッセージを結果のなかったようです。私のコードはどこが間違っていますか?私は既にチュートリアルを検索し、その答えをグーグルではあるが、見つけられない。

ありがとうございます。

+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 STRINGSTRING属性*あなたや誰かが助けてくれるのですが、どうしてこのような結果になりましたか?ありがとうございました...ありがとうございました...