Weka:在属性选择期间监督离散问题和错误“训练实例不够”

时间:2012-10-21 19:40:36

标签: java attributes selection weka labels

过去一个月左右我一直在学习Weka API(我是学生)。我正在做的是编写一个程序,它将过滤一组特定的数据并最终为它构建一个贝叶斯网络,一周前我完成了我的离散化类和属性选择类。就在几天前,我意识到我需要将我的离散化功能改为监督并最终使用默认的Fayyad& Irani方法,在我这样做之后,我开始在我的属性选择类中得到这个错误:

Exception in thread "main" weka.core.WekaException: 
weka.attributeSelection.CfsSubsetEval: Not enough training instances with class labels (required: 1, provided: 0)!
at weka.core.Capabilities.test(Capabilities.java:1138)
at weka.core.Capabilities.test(Capabilities.java:1023)
at weka.core.Capabilities.testWithFail(Capabilities.java:1302)
at weka.attributeSelection.CfsSubsetEval.buildEvaluator(CfsSubsetEval.java:331)
at weka.attributeSelection.AttributeSelection.SelectAttributes(AttributeSelection.java:597)
at weka.filters.supervised.attribute.AttributeSelection.batchFinished(AttributeSelection.java:456)
at weka.filters.Filter.useFilter(Filter.java:663)
at AttributeSelectionFilter.selectionFilter(AttributeSelectionFilter.java:29)
at Runner.main(Runner.java:70)

我在改变之前的属性选择工作得很好,所以我认为我可能在我的离散班上做错了。我对这个问题的另一部分与此有关,因为我也注意到我的离散化课程似乎并不真正使数据离散化;它只是将所有数字数据放入一个范围内,而不是像Fayyad& amp;伊朗应该。

这是我离散的课程:

import weka.core.Instances;
import weka.filters.Filter;
import weka.filters.supervised.attribute.Discretize;
import weka.filters.unsupervised.attribute.NumericToNominal;

public class DiscretizeFilter
{
    private Instances data;
    private boolean sensitiveOption;
    private Filter filter = new Discretize();

    public DiscretizeFilter(Instances data, boolean sensitiveOption)
    {
        this.data = data;
        this.sensitiveOption = sensitiveOption;
    }

    public Instances discreteFilter() throws Exception
    {
        NumericToNominal nm = new NumericToNominal();
        nm.setInputFormat(data);
        Filter.useFilter(data, nm);
        Instances nominalData = nm.getOutputFormat();

        if(sensitiveOption)//if the user wants extra sensitivity
        {
            String options[] = new String[1];
            options[0] = options[0];
            options[2] = "-E";
            ((Discretize) filter).setOptions(options);
        }
        filter.setInputFormat(nominalData);
        Filter.useFilter(nominalData,filter);
        return filter.getOutputFormat();
    }
}

这是我的属性选择类:

import weka.attributeSelection.BestFirst;
import weka.attributeSelection.CfsSubsetEval;
import weka.core.Instances;
import weka.filters.supervised.attribute.AttributeSelection;

public class AttributeSelectionFilter 
{
    public Instances selectionFilter(Instances data) throws Exception
    {
        AttributeSelection filter = new AttributeSelection();

        for(int i = 0; i < data.numInstances(); i++)
        {
            filter.input(data.instance(i));
        }
        CfsSubsetEval eval = new CfsSubsetEval();
        BestFirst search = new BestFirst();
        filter.setSearch(search);
        filter.setEvaluator(eval);

        filter.setInputFormat(data);
        AttributeSelection.useFilter(data, filter);

        return filter.getOutputFormat();
    }

    public int attributeCounter(Instances data)
    {
        return data.numAttributes();
    }
}

任何帮助都将非常感谢!!!

2 个答案:

答案 0 :(得分:0)

内部Weka将属性值存储为双精度值。似乎抛出了一个异常,因为数据集中的每个实例(data)都“缺少一个类”,即由于某种原因给出了内部类属性值NaN(“非数字”)。我建议仔细检查是否正确创建/设置了data's类属性。

答案 1 :(得分:0)

我想通了,我错误地误解了Discretize类中方法“outputFormat()”的描述。我改为从useFilter()获取过滤后的实例,这解决了我的问题!我只是给属性选择过滤器提供了错误的数据类型。