【问题标题】:Weka predict classifcation nodeWeka预测分类节点
【发布时间】:2013-10-17 04:29:36
【问题描述】:

我已经创建了一棵大小约为 7000 的巨大 j48 树,其中包含许多树枝和树叶。我也得到了测试图像的分类结果。我想知道哪个节点正在为每个结果进行分类。换句话说,weka 有没有办法查看做出决定的叶节点的 id 或其他东西。

【问题讨论】:

    标签: java machine-learning weka


    【解决方案1】:

    据我所知,您无法通过 Weka GUI 执行此操作。但是,如果您使用 Weka API,还是有希望的。我不是 Java 专家,因此以下步骤可能不符合最佳实践,但它确实适用于我编写的小示例。

    1. 在 Weka GUI 中构建 j48 树并在“更多选项”选项卡中选择“输出源代码”

    2. 将源代码复制到 Java 代码中的新类

    3. classifyInstance 方法中增加返回变量以包含叶编号

    4. 修改类,使其不再扩展“分类器”(这需要消除您刚刚创建的类中的一些其他方法)

    下面是一个使用决策树桩分类器对 Weka 实例进行分类的类。输出将包括一个叶子编号。这是按照上述步骤从 Weka 示例数据集中包含的分类天气数据构建的。对于您拥有的巨大决策树,可能需要进行一些字符串替换以有效地增加您的返回变量。

    import weka.core.Instance;
    
    public class WekaWrapper {
    
      /**
       * Classifies the given instance.
       *
       * @param i the instance to classify
       * @return the classification result
       */
      public double[] classifyInstance(Instance i) throws Exception {
        Object[] s = new Object[i.numAttributes()];
    
        for (int j = 0; j < s.length; j++) {
          if (!i.isMissing(j)) {
            if (i.attribute(j).isNominal())
              s[j] = new String(i.stringValue(j));
            else if (i.attribute(j).isNumeric())
              s[j] = new Double(i.value(j));
          }
        }
    
        // set class value to missing
        s[i.classIndex()] = null;
    
        return WekaClassifier.classify(s);
      }
    }
    class WekaClassifier {
      public static double[] classify(Object[] i) {
        /* outlook */
          double[][] here=new double[3][2];
          here[0][0]=0; //leaf value
          here[0][1]=1; //leaf ID
          here[1][0]=0; //leaf value
          here[1][1]=2; //leaf ID
          here[2][0]=0; //leaf value
          here[2][1]=3; //leaf ID
        if (i[0] == null) { return here[0]; } else if (((String)i[0]).equals("overcast")) { return here[1]; } else { return here[2]; }
      }
    }
    

    【讨论】:

    • 非常感谢!不知道 weka 中有可用的输出源代码选项。虽然我希望通过方法调用 weka api 来获得相同的结果,但很高兴知道这至少是可能的。
    猜你喜欢
    • 1970-01-01
    • 2013-09-25
    • 2015-04-13
    • 2016-02-25
    • 2013-12-14
    • 2017-04-30
    • 2017-01-09
    • 1970-01-01
    • 2016-05-22
    相关资源
    最近更新 更多