2

I am trying to use the filtered classifier on data of the following format:

real,real,real,...,nominal

where I have 138 real values and a single nominal string representing the class. I am using J48 as the base classifier and the supervised discretization filter as follows:

Filter discretize = new weka.filters.supervised.attribute.Discretize();
FilteredClassifier fc = new FilteredClassifier(); 
discretize.setInputFormat(m_data);

J48 ft = new J48();
ft.setOptions(wekaOptions);
fc.setFilter(discretize);
fc.setClassifier(ft);

fc.buildClassifier(m_data);

Where m_data is my (labelled) training data with m_data.setClassIndex(m_data.numAttributes()-1) set. No problems (that I can see) here. I serialize this model and load it later. I then use it to classify unseen/new data like so:

Instance unlabeledInstance = new DenseInstance(1.0,features);
unlabeledInstance.setDataset(m_instances); 
m_classifier.classifyInstance(unlabeledInstance);

where features is a double[] that follows the format of the input data, but with no nominal class value. Here m_instances is just loaded from the training data file with m_instances.setClassIndex(m_instances.numAttributes()-1) set.

However, when running m_classifier.classifyInstance(unlabeledInstance), I receive an arrayOutOfBoundsException error. Can anyone shed any light on this?

The trace is:

07-13 15:15:35.383: W/System.err(30659): java.lang.ArrayIndexOutOfBoundsException: length=138; index=138
07-13 15:15:35.383: W/System.err(30659):    at weka.core.DenseInstance.value(DenseInstance.java:309)
07-13 15:15:35.383: W/System.err(30659):    at weka.filters.unsupervised.attribute.Discretize.convertInstance(Discretize.java:1047)
07-13 15:15:35.383: W/System.err(30659):    at weka.filters.unsupervised.attribute.Discretize.input(Discretize.java:389)
07-13 15:15:35.383: W/System.err(30659):    at weka.classifiers.meta.FilteredClassifier.distributionForInstance(FilteredClassifier.java:425)
07-13 15:15:35.383: W/System.err(30659):    at weka.classifiers.AbstractClassifier.classifyInstance(AbstractClassifier.java:72)

It looks like its expecting the class value to be set.

4

2 回答 2

0

我认为您需要从中删除类值unlabeledInstancem_instances.setClassIndex(m_instances.numAttributes()-1)因此也不需要)。当 Weka 对测试实例进行分类时,它只需要特征而不需要类,因此如果您将类留在实例中,那么 Weka 不会期望数组中的额外条目并为您提供ArrayIndexOutOfBoundsException.

于 2012-07-17T09:38:57.240 回答
0

我这样解决:删除discretize.setInputFormat(m_data);. 当您使用过滤分类器时,weka 将调用 SetUp 方法为您执行此操作。我使用的是 naivebayesUpdate NaiveBayes

于 2017-09-28T14:20:52.473 回答