我正在尝试在我的java代码中使用Weka API运行支持向量机(SVM)分类器。我收到以下错误“Src和Dest在第13行的属性中有所不同:”。请指导我如何解决此问题并成功运行分类器(这是我的时间关键学术项目)。如果可能的话,请举例说明解决方案。
错误:
java.lang.IllegalArgumentException: Src and Dest differ in # of attributes: 22 != 31
at weka.core.RelationalLocator.copyRelationalValues(RelationalLocator.java:88)
at weka.filters.Filter.copyValues(Filter.java:359)
at weka.filters.Filter.push(Filter.java:276)
at weka.filters.unsupervised.attribute.Standardize.convertInstance(Standardize.java:260)
at weka.filters.unsupervised.attribute.Standardize.input(Standardize.java:142)
at weka.filters.Filter.useFilter(Filter.java:661)
at ClassifierJan12Pure.main(ClassifierJan12Pure.java:139)
JAVA代码:
1. filteredData = new Instances(new BufferedReader(new FileReader("training.arff")));
2. filteredData.setClassIndex(0);
3. Classifier classifier=new SMO();
4. classifier.buildClassifier(filteredData);
5. FilteredClassifier filteredClassifier=new FilteredClassifier();
6. filteredClassifier.setClassifier(classifier);
7. Filter filter = new StringToWordVector(1000);
8. filteredClassifier.setFilter(filter);
9. Instances filteredTests= new Instances(new BufferedReader(new FileReader("testing.arff")));
10. Standardize sfilter = new Standardize();
11. sfilter.setInputFormat(filteredData); // initializing the filter once with training set
12. Instances newTrain = Filter.useFilter(filteredData, sfilter); // configures the Filter based on train instances and returns filtered instances
**13. Instances testsF = Filter.useFilter(filteredTests, sfilter); // create new test set ///<<<<<<< Error line.**
14. Evaluation eval = new Evaluation(filteredData);
15. eval.evaluateModel(filteredClassifier, filteredTests);
16. System.out.println(eval.toSummaryString("\nResults\n", false));
training.arff
@relation '_Users_Passionate_Desktop_Training_Text-weka.filters.unsupervised.attribute.StringToWordVector-R1-W1000-prune-rate-1.0-N0-stemmerweka.core.stemmers.NullStemmer-M1-tokenizerweka.core.tokenizers.WordTokenizer -delimiters \" \\r\\n\\t.,;:\\\'\\\"()?!\"'
@attribute @@class@@ {dummy,ham,spam}
@attribute about numeric
@attribute and numeric
@attribute be numeric
@attribute construction numeric
@attribute discussion numeric
@attribute me numeric
@attribute much numeric
@attribute np numeric
@attribute of numeric
@attribute reminds numeric
@attribute s numeric
@attribute said numeric
@attribute second numeric
@attribute that numeric
@attribute the numeric
@attribute to numeric
@attribute very numeric
@attribute agree numeric
@attribute auto numeric
@attribute boxes numeric
@attribute credit numeric
@attribute dear numeric
@attribute display numeric
@attribute have numeric
@attribute ll numeric
@attribute nlpeople numeric
@attribute sure numeric
@attribute we numeric
@attribute with numeric
@attribute you numeric
@data
{0 ham,1 1,2 1,3 1,4 1,7 1,8 1,12 1,13 1,15 1,16 1,17 1}
{0 ham,5 1,6 1,8 1,9 1,10 1,11 1,14 1,15 1}
{0 spam,18 1,19 1,22 1,25 1,26 1,27 1,30 1}
{0 spam,20 1,21 1,23 1,24 1,28 1,29 1}
Testing.arff
@relation '_Users_Passionate_Desktop_TestingDiffThanTesting_Text-weka.filters.unsupervised.attribute.StringToWordVector-R1-W1000-prune-rate-1.0-N0-stemmerweka.core.stemmers.NullStemmer-M1-tokenizerweka.core.tokenizers.WordTokenizer -delimiters \" \\r\\n\\t.,;:\\\'\\\"()?!\"'
@attribute @@class@@ {dummy,ham,spam}
@attribute brazilian numeric
@attribute do numeric
@attribute in numeric
@attribute indigenous numeric
@attribute languages numeric
@attribute linguists numeric
@attribute on numeric
@attribute prepare numeric
@attribute research numeric
@attribute specialization numeric
@attribute to numeric
@attribute and numeric
@attribute be numeric
@attribute developed numeric
@attribute evaluation numeric
@attribute of numeric
@attribute program numeric
@attribute published numeric
@attribute reanalysis numeric
@attribute the numeric
@attribute will numeric
@data
{0 ham,1 1,3 1,4 1,5 1,10 1}
{0 ham,2 1,4 1,6 1,7 1,8 1,9 1,11 1}
{0 spam,12 1,15 1,16 1,18 1,19 1}
{0 spam,5 1,13 1,14 1,17 1,20 1,21 1}
答案 0 :(得分:1)
谢谢James&amp;迈克尔,我能够解决“Src和Dest在属性中的不同:”的问题。
第1步: 运行TextDirectoryLoader以训练和测试文件夹。
java weka.core.converters.TextDirectoryLoader -dir Testing_Text > testing.arff
第2步: 以下命令实际上在training.arff&amp ;;中提供了相同数量的属性。 testing.arff文件(http://weka.wikispaces.com/Batch+filtering)。我错误地单独运行StringToWordVector命令,这在training.arff和testing.arff文件中给了我不同数量的属性。
java weka.filters.unsupervised.attribute.StringToWordVector -b -i training.arff -o train_std.arff -r testing.arff -s test_std.arff
现在我面临另一个问题:
即使我在第3行给出了setClassIndex,但仍显示错误消息“Class not set!”。
错误强>:
weka.core.UnassignedClassException: Class is not set!
weka.core.UnassignedClassException: Class is not set!
at weka.core.Instance.setClassMissing(Instance.java:544)
at weka.classifiers.Evaluation.evaluateModelOnceAndRecordPrediction(Evaluation.java:1439)
at weka.classifiers.Evaluation.evaluateModel(Evaluation.java:1412)
at ClassifierJan12Pure.main(ClassifierJan12Pure.java:94)
Jave Code:
1. filteredData = new Instances(new BufferedReader(new FileReader("/Users/Passionate/Desktop/train_std.arff")));
2. Instances filteredTests= new Instances(new BufferedReader(new FileReader("/Users/Passionate/Desktop/test_std.arff")));
3. filteredData.setClassIndex(filteredData.attribute("@@class@@").index());
4. Classifier classifier=new SMO();
5. classifier.buildClassifier(filteredData);
6. FilteredClassifier filteredClassifier=new FilteredClassifier();
7. filteredClassifier.setClassifier(classifier);
8. Evaluation eval = new Evaluation(filteredData);
9. eval.evaluateModel(filteredClassifier, filteredTests); **// Error line.**
10. System.out.println(eval.toSummaryString("\nResults\n======\n", false));
train_std.arff文件:
@relation '_Users_Passionate_Desktop_Training_Text-weka.filters.unsupervised.attribute.StringToWordVector-R1-W1000-prune-rate-1.0-N0-stemmerweka.core.stemmers.NullStemmer-M1-tokenizerweka.core.tokenizers.WordTokenizer -delimiters \" \\r\\n\\t.,;:\\\'\\\"()?!\"'
@attribute @@class@@ {dummy,ham,spam}
@attribute a numeric
@attribute address numeric
@attribute all numeric
@attribute as numeric
@attribute at numeric
@attribute back numeric
@attribute boxes numeric
@attribute credit numeric
@attribute display numeric
@attribute had numeric
@attribute happy numeric
@attribute have numeric
@attribute hoped numeric
@attribute is numeric
@attribute line numeric
@attribute message numeric
@attribute moderators numeric
@attribute new numeric
@attribute not numeric
@attribute of numeric
@attribute on numeric
@attribute our numeric
@attribute remember numeric
@attribute running numeric
@attribute subscribers numeric
@attribute the numeric
@attribute those numeric
@attribute to numeric
@attribute very numeric
@attribute we numeric
@attribute with numeric
@data
{0 ham,1 1,3 1,11 1,16 1,17 1,22 1,25 1,28 1,29 1}
{0 ham,4 1,6 1,10 1,13 1,14 1,15 1,19 1,21 1,26 1,30 1}
{0 spam,2 1,5 1,18 1,20 1,22 1,23 1,24 1,27 1}
{0 spam,7 1,8 1,9 1,12 1,30 1,31 1}
test_std.arff文件:
@relation '_Users_Passionate_Desktop_Training_Text-weka.filters.unsupervised.attribute.StringToWordVector-R1-W1000-prune-rate-1.0-N0-stemmerweka.core.stemmers.NullStemmer-M1-tokenizerweka.core.tokenizers.WordTokenizer -delimiters \" \\r\\n\\t.,;:\\\'\\\"()?!\"'
@attribute @@class@@ {dummy,ham,spam}
@attribute a numeric
@attribute address numeric
@attribute all numeric
@attribute as numeric
@attribute at numeric
@attribute back numeric
@attribute boxes numeric
@attribute credit numeric
@attribute display numeric
@attribute had numeric
@attribute happy numeric
@attribute have numeric
@attribute hoped numeric
@attribute is numeric
@attribute line numeric
@attribute message numeric
@attribute moderators numeric
@attribute new numeric
@attribute not numeric
@attribute of numeric
@attribute on numeric
@attribute our numeric
@attribute remember numeric
@attribute running numeric
@attribute subscribers numeric
@attribute the numeric
@attribute those numeric
@attribute to numeric
@attribute very numeric
@attribute we numeric
@attribute with numeric
@data
{0 ham}
{0 ham,21 1,28 1}
{0 spam,20 1}
{0 spam,26 1}
答案 1 :(得分:0)
您列出的属性在培训和测试文件之间存在很大差异。
它如何测试使用一组属性构建的分类器,如果它只有一组不同的属性?