【问题标题】:Create and classify new instance Weka创建和分类新实例 Weka
【发布时间】:2015-07-16 09:45:21
【问题描述】:

我使用 Weka API 和 Java 创建了一个决策树 (J48)。首先,我使用 arff 文件训练我的决策树。

public static void Tree(String Path) throws Exception {//Path path for the arff file
    J48 tree = new J48();         // new instance of tree
    DataSource source = new DataSource(Path);
    Instances data = source.getDataSet();
    // setting class attribute if the data format does not provide this information
    // For example, the XRFF format saves the class attribute information as well
    if (data.classIndex() == -1) {
        data.setClassIndex(data.numAttributes() - 1);
    }
    tree.buildClassifier(data);
    System.out.println(tree.toString());
}

使用的 arff 文件包括 780 个实例。每个实例有 6 个属性 {PT1, w1, d1, PT2, w2, d2} 都是数字和一个类 {yes, no}。 我的代码正在运行,我可以使用

查看生成的决策树
System.out.println(tree.toString());

现在,我想创建一个新实例(不使用另一个 arff 文件)并对这个新实例进行分类。假设这个新实例的值是 {50, 5, 800, 74, 3, 760}。然后决策树必须返回相应的类(“是”或“否”)。

【问题讨论】:

    标签: java weka


    【解决方案1】:

    我找到了解决问题的方法,希望对您有用。

        //Declaring attributes
        Attribute PT1 = new Attribute("PT1");
        Attribute w1 = new Attribute("w1");
        Attribute d1 = new Attribute("d1");
        Attribute PT2 = new Attribute("PT2");
        Attribute w2 = new Attribute("w2");
        Attribute d2 = new Attribute("d2");
    
        // Declare the class attribute along with its values contains two nominal values yes and no using FastVector. "ScheduledFirst" is the name of the class attribute        
        FastVector fvClassVal = new FastVector(2);
        fvClassVal.addElement("yes");
        fvClassVal.addElement("no");
        Attribute Class = new Attribute("ScheduledFirst", fvClassVal);
    
        // Declare the feature vector
        FastVector fvWekaAttributes = new FastVector(7);
        // Add attributes 
        fvWekaAttributes.addElement(PT1);
        fvWekaAttributes.addElement(w1);
        fvWekaAttributes.addElement(d1);
        fvWekaAttributes.addElement(PT2);
        fvWekaAttributes.addElement(w2);
        fvWekaAttributes.addElement(d2);
        fvWekaAttributes.addElement(Class);
        // Declare Instances which is required since I want to use classification/Prediction
        Instances dataset = new Instances("whatever", fvWekaAttributes, 0);
    
        //Creating a double array and defining values
        double[] attValues = new double[dataset.numAttributes()];
        attValues[0] = 50;
        attValues[1] = 5;
        attValues[2] = 800;
        attValues[3] = 74;
        attValues[4] = 3;
        attValues[5] = 760;
    
        //Create the new instance i1
        Instance i1 = new Instance(1.0, attValues);
        //Add the instance to the dataset (Instances) (first element 0)        
        dataset.add(i1);
        //Define class attribute position
        dataset.setClassIndex(dataset.numAttributes()-1);
    
        //Will print 0 if it's a "yes", and 1 if it's a "no"
        System.out.println(tree.classifyInstance(dataset.instance(0)));
        //Here I call dataset.instance(0) since there is only one instance added in the dataset, if you do add another one you can use dataset.instance(0), etc.
    

    【讨论】:

      猜你喜欢
      • 2011-09-07
      • 2013-05-13
      • 2013-10-20
      • 2012-06-30
      • 2012-03-25
      • 2015-02-01
      • 2012-11-08
      • 2014-05-19
      • 1970-01-01
      相关资源
      最近更新 更多