gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:40:56 27 4
gpt4 key购买 nike

我正在尝试向我的训练集添加更多实例并执行 10 倍交叉验证。

我的实例采用字符串格式,因此我使用 StringToWordVector 过滤器将它们转换为数字。如果我不添加我想要的额外页面,事情就会顺利进行。但是当我添加命令 trainSet.addAll(data2); 并将 trainSet 传递给过滤器时,我在第一次迭代中得到一个奇怪的 IndexOutOfBoundsException 实例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....

当我尝试使用过滤器时出现以下错误:

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)

可能出了什么问题?

最佳答案

经过一番搜索,我意识到addAll有问题。功能。我能想到的一个原因是 addAll只是添加实例的引用,当我尝试将它们与 filter 一起使用时,这是一个问题。相反,我使用了此处建议的合并功能 https://stackoverflow.com/a/12359788/3923800 ,所以我替换了trainSet.addAll(data2);Instances newTrainSettrainSet = merge(trainSet,data2);一切正常。

关于java - 尝试使用 Weka 将更多实例添加到训练集中时出现 IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38460698/

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