合并两个具有不同但重叠模式和不同实例数量的稀疏Weka数据集

我需要迭代地使用SparseInstance对象扩展一个Weka ARFF文件。每次添加一个新的SparseInstance时,头部可能会改变,因为新的实例可能会添加额外的属性。我以为mergeInstances方法可以解决我的问题,但它并不能。它要求两个数据集没有共享的属性。

如果这还不够清楚,请看以下示例:

Dataset1a b c1 2 34 5 6Dataset2c d7 8合并结果:a b c d1 2 3 ?4 5 6 ?? ? 7 8

目前我能想到的唯一解决方案是手动解析arff文件,并使用字符串处理进行合并。有人知道更好的解决方案吗?


回答:

好的。我自己找到了解决方案。解决方案的核心部分是Instances#insertAttributeAt方法,如果第二个参数是model.numAttributes(),它会将新属性插入到最后。这里是针对数值属性的示例代码。也很容易适应其他类型的属性:

    Map<String,String> currentInstanceFeatures = currentInstance.getFeatures();    Instances model = null;    try {        if (targetFile.exists()) {            FileReader in = new FileReader(targetFile);            try {                BufferedReader reader = new BufferedReader(in);                ArffReader arff = new ArffReader(reader);                model = arff.getData();            } finally {                IOUtils.closeQuietly(in);            }        } else {            FastVector schema = new FastVector();            model = new Instances("model", schema, 1);        }        Instance newInstance = new SparseInstance(0);        newInstance.setDataset(model);        for(Map.Entry<String,String> feature:currentInstanceFeatures.entrySet()) {            Attribute attribute = model.attribute(feature.getKey());                if (attribute == null) {                    attribute = new Attribute(feature.getKey());                    model.insertAttributeAt(attribute, model.numAttributes());                    attribute = model.attribute(feature.getKey());                }            newInstance.setValue(attribute, feature.getValue());        }        model.add(newInstance);        model.compactify();        ArffSaver saver = new ArffSaver();        saver.setInstances(model);        saver.setFile(targetFile);        LOGGER.debug("Saving dataset to: " + targetFile.getAbsoluteFile());        saver.writeBatch();    } catch (IOException e) {        throw new IllegalArgumentException(e);    }

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注