Weka J48分类不遵循树

时间:2017-07-05 04:15:14

标签: java machine-learning weka decision-tree j48

我原来的树要大得多,但是因为我已经坚持这个问题很长一段时间了,所以我决定尝试简化我的树。我结束了这样的事情:

arvore

正如您所看到的,我只有一个名为" LarguraBandaRede"有3个可能的名义价值" Congestionado"," Livre"和" Merda"。

之后我从weka导出了j48.model,用于我的java代码。

使用这段代码我导入模型以用作分类器:

ObjectInputStream objectInputStream = new ObjectInputStream(in);
classifier = (J48) objectInputStream.readObject();

之后我开始创建属性的arraylist和Instances File

for (int i = 0; i <features.length; i++) {
        String feature = features[i];
        Attribute attribute;
        if (feature.equals("TamanhoDados(Kb)")) {
            attribute = new Attribute(feature);
        } else {
            String[] strings = null;
            if(i==0) strings = populateAttributes(7);
            if(i==1) strings = populateAttributes(10);
            ArrayList<String> attValues = new ArrayList<String>(Arrays.asList(strings));
            attribute = new Attribute(feature,attValues);
        }
        atts.add(attribute);
    }

其中populateAttributes为每个属性提供了可能的值,在本例中为&#34; Livre,Merda,Congestionado;&#34;对于LarguraBandaRede和&#34; Sim,Nao&#34;对于Resultado,我的类属性。

Instances instances = new Instances("header",atts,atts.size());
instances.setClassIndex(instances.numAttributes()-1);

创建我的实例后,是时候创建我的实例文件了,也就是我尝试分类的实例

Instance instanceLivre = new DenseInstance(features.length);
Instance instanceMediano = new DenseInstance(features.length);
Instance instanceCongestionado = new DenseInstance(features.length);
instanceLivre.setDataset(instances);
instanceMediano.setDataset(instances);
instanceCongestionado.setDataset(instances);

然后我将每个实例设置为&#34; LarguraBandaRede&#34;的3个可能值。 &#39; instanceLivre&#39;与&#34; Livre&#34;,&#39; instanceMediano&#39;与&#34; Merda&#34;和&#39; instanceCongestionado&#39;与&#34; Congestionado&#34;。

之后我只使用classifyInstance方法

对这3个实例进行分类
System.out.println(instance.toString());
double resp = classifier.classifyInstance(instance);
System.out.println("valor: "+resp);

这是我的结果:

result

正如您所看到的,Merda的实例为&#34; LarguraBandaRede&#34;被归类为和Congestionado一样的班级,班级和Nao&#39;。但这没有任何意义,因为上面的树清楚地显示了当&#34; LarguraBandaRede&#34;是&#34; Merda&#34;或&#34; Livre&#34;这个班级应该是一样的。

这就是我的问题。这是怎么发生的以及如何解决它?

提前致谢。

修改

我不知道这个:

weka's index

模型的工作方式有所不同。但是,在为名义属性提供可能的值时,我们必须遵循此顺序。

1 个答案:

答案 0 :(得分:1)

您是否检查了weka名义属性索引是否与 populateAttributes 方法相同?