gpt4 book ai didi

java - 为什么 weka 实例不将标称属性设置为第一个值(索引 : 0)

转载 作者:行者123 更新时间:2023-12-01 11:29:02 25 4
gpt4 key购买 nike

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

代码如下:

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);
}

返回的目标类实际上是一个类索引。我有两个类:由以下代码片段创建的“花”和“树”:

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

问题来了 - 当我将记录目标类设置为 '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”目标类。

正如您所看到的,这种“零切割”也在目标类属性上执行。

关于java - 为什么 weka 实例不将标称属性设置为第一个值(索引 : 0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30573385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com