2
我嘗試在我的java代碼中做文本分類樸素貝葉斯weka libarary,但我認爲分類的結果是不正確的,我不知道是什麼問題。我使用arff文件輸入。在java中使用樸素貝葉斯(weka)的簡單文本分類
這是我的訓練數據:
@relation hamspam
@attribute text string
@attribute class {spam,ham}
@data
'good',ham
'good',ham
'very good',ham
'bad',spam
'very bad',spam
'very bad, very bad',spam
'good good bad',ham
這是我testing_data:
@relation test
@attribute text string
@attribute class {spam,ham}
@data
'good bad very bad',?
'good bad very bad',?
'good',?
'good very good',?
'bad',?
'very good',?
'very very good',?
,這是我的代碼:
public static void NaiveBayes(String training_file, String testing_file) throws FileNotFoundException, IOException, Exception{
//filter
StringToWordVector filter = new StringToWordVector();
Classifier naive = new NaiveBayes();
//training data
Instances train = new Instances(new BufferedReader(new FileReader(training_file)));
int lastIndex = train.numAttributes() - 1;
train.setClassIndex(lastIndex);
filter.setInputFormat(train);
train = Filter.useFilter(train, filter);
//testing data
Instances test = new Instances(new BufferedReader(new FileReader(testing_file)));
test.setClassIndex(lastIndex);
filter.setInputFormat(test);
Instances test2 = Filter.useFilter(test, filter);
naive.buildClassifier(train);
for(int i=0; i<test2.numInstances(); i++) {
System.out.println(test.instance(i));
double index = naive.classifyInstance(test2.instance(i));
String className = train.attribute(0).value((int)index);
System.out.println(className);
}
}
結果表明,該數據應已被歸類爲分類爲類火腿的類垃圾郵件,以及應被分類爲類的數據驢子被分類爲垃圾郵件。有什麼問題嗎?,請幫助我..