为什么 weka Instance 没有将名义属性设置为第一个值(索引:0)

Why weka Instance doesn't set nominal attribute to first value (index: 0)

我在为实例对象设置目标 class 时遇到问题。 想象一下这样的情况:我有两个回归结果(包含斜率和截距) 鉴于此,我将前四个属性设置为一些双精度值,最后一个属性(目标属性)由索引而不是值设置。

代码如下所示:

for (RegressionArffRow row : input) {
        Instance record = new SparseInstance(attrInfo.size());
        int attrIdx = 0;
        for (RegressionResult regResult : row.getRegressionResults()) {
            record.setValue(attrIdx++, regResult.getSlope());
            record.setValue(attrIdx++, regResult.getIntercept());
        }

        record.setValue(attrIdx, row.getDestinationClass());            
        instances.add(record);
    }

返回的目的地class实际上是一个class索引。我有两个 classes: "flower" 和 "tree" 由以下代码段创建:

FastVector destValues = new FastVector();
destValues.addElement("tree");
destValues.addElement("flower");
Attribute destClassAttribute = new Attribute("destClass", destValues);

问题来了 - 当我将记录目标 class 设置为 '1' 时,我的实例设置为 "flower"。但是当我将记录设置为 '0' 时,最后一个属性根本没有设置。

很快看起来像这样:

record.setValue(attrIdx, 0);

在调试器中给出这样的结果:

{0 0.07017,1 -1.338295,2 -0.252162,3 1.377695}

还有这个:

record.setValue(attrIdx, 1);

给出以下内容:

{0 0.07017,1 -1.338295,2 -0.252162,3 1.377695, 4 "flower"}

好的,这里的问题是我在这里使用SparseInstance,这是切割等于0的值。我直觉地认为它只涉及数字属性 - 并且只删除它们的值 - 对名义属性没有影响。但是我在 documentation:

中错过了这个片段

this also includes nominal attributes -- the first nominal value (i.e. that which has index 0) will not require explicit storage, so rearrange your nominal attribute value orderings if necessary

在我的示例中,未插入 "tree" 值,因为它的索引等于 0

如果你想持有你的每一个名义价值,请确保你会克服它(如果你还想使用SparseInstance

你可能想这样实现:

FastVector destValues = new FastVector();
destValues.addElement("dummy");
destValues.addElement("tree");
destValues.addElement("flower");
Attribute destClassAttribute = new Attribute("destClass", destValues);

然后你将永远不会使用 "dummy" 目的地 class。

如您所见,此 "zero-cutting" 也在目标 class 属性上执行。