有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

eclipse My Weka Java代码结果*Weka*虚拟*STRING*用于*STRING*属性*

我有一个通过WEKA GUI生成的J48模型,并计划在我的WEKA JAVA代码中使用它。我想用这个模型来实时预测我的数据。我的代码如下:

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伪消息中。我的代码哪里出错了?我已经搜索了教程并用谷歌搜索了答案,但我找不到答案

谢谢


共 (1) 个答案

  1. # 1 楼答案

    谢谢你的评论,幽灵猫。我得到了朋友的帮助来解决这个问题。更新代码如下:

    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
    

    似乎我们已经基于新数据创建了实例。然而,它不能按模型进行分类,并导致STRING属性的WEKADUMMYSTRING。你或任何人能帮我吗,为什么会有这样的结果?谢谢你