简体   繁体   English

尝试使用Weka将更多实例添加到训练集中时出现IndexOutOfBoundsException

[英]IndexOutOfBoundsException when trying to add more instances to training set using Weka

I am trying to add more Instances to my training set and perform 10-fold cross validation. 我正在尝试将更多实例添加到我的训练集中,并执行10倍交叉验证。

My instances are in String format so i use the StringToWordVector filter to transform them to numbers. 我的实例为String格式,因此我使用StringToWordVector过滤器将其转换为数字。 Things work well if i do not add the extra pages i want. 如果我不添加我想要的多余页面,事情将会很好。 But when i add the command trainSet.addAll(data2); 但是当我添加命令trainSet.addAll(data2); and pass trainSet to the filter i get a strange IndexOutOfBoundsException in the first iteration at Instances fTrainSet = Filter.useFilter(trainSet, filter); 并将trainSet传递给过滤器,在Instances fTrainSet = Filter.useFilter(trainSet, filter);的第一次迭代中,我得到了一个奇怪的IndexOutOfBoundsException Instances fTrainSet = Filter.useFilter(trainSet, filter);

Instances data = getDataFromFile("pathtofile.arff");//main dataset 1821 instances
Instances data2 = getDataFromFile("anotherpath.arff");//709 instances i want to add 
int folds = 10;
for(int i=0;i<folds;i++){
    Instances trainSet = data.trainCV(folds, i);//training set
    System.out.println(trainSet.numInstances());//Prints 1638
    Instances testSet =  data.testCV(folds, i);//testing set

    //add more instances
    trainSet.addAll(data2);        
    System.out.println(trainSet.numInstances());//Prints 2347

    //filter
    StringToWordVector filter = new StringToWordVector();
    filter.setInputFormat(trainSet);        
    filter.setWordsToKeep(10000);
    filter.setTFTransform(true);
    filter.setLowerCaseTokens(true);
    filter.setOutputWordCounts(true);
    Stemmer stemmer = new IteratedLovinsStemmer();
    filter.setStemmer(stemmer);
    WordsFromFile stopwords = new WordsFromFile();
    stopwords.setStopwords(new File(".data/stopwords2.txt"));
    filter.setStopwordsHandler(stopwords);

    Instances fTrainSet = Filter.useFilter(trainSet, filter);//error!!!
    Instances fTestSet = Filter.useFilter(testSet, filter);
    ....
    //classification and evaluation....

I get the following error when i am trying to use the filter: 尝试使用过滤器时出现以下错误:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2161, Size: 1749
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at weka.core.Attribute.addStringValue(Attribute.java:924)
    at weka.core.StringLocator.copyStringValues(StringLocator.java:150)
    at weka.core.StringLocator.copyStringValues(StringLocator.java:91)
    at weka.filters.Filter.copyValues(Filter.java:399)
    at weka.filters.Filter.bufferInput(Filter.java:342)
    at weka.filters.unsupervised.attribute.StringToWordVector.input(StringToWordVector.java:655)
    at weka.filters.Filter.useFilter(Filter.java:692)
    at CrossValidationExample.main(CrossValidationExample.java:108)

What could be wrong? 有什么事吗

After some searching i realize that there is something wrong with the addAll function. 经过一番搜索,我意识到addAll函数有问题。 One reason i can think of is that addAll just adds references of instances and that is an issue when i try to use them with the filter . 我能想到的一个原因是addAll仅添加实例的引用,当我尝试将其与filter一起使用时,这是一个问题。 Instead, i used the merge function proposed here https://stackoverflow.com/a/12359788/3923800 ,so i replaced trainSet.addAll(data2); 相反,我使用了这里建议的合并功能https://stackoverflow.com/a/12359788/3923800 ,所以我替换了trainSet.addAll(data2); with Instances newTrainSettrainSet = merge(trainSet,data2); Instances newTrainSettrainSet = merge(trainSet,data2); and everything works fine. 一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM