朴素贝叶斯法
1 // load data 2 ArffLoader loader = new ArffLoader(); 3 loader.setFile(new File(args[0])); 4 Instances structure = loader.getStructure(); 5 structure.setClassIndex(structure.numAttributes() - 1); 6 7 // train NaiveBayes 8 NaiveBayesUpdateable nb = new NaiveBayesUpdateable(); 9 NaiveBayes nb2 = new NaiveBayes(); 10 nb.buildClassifier(structure); 11 Instance current; 12 while ((current = loader.getNextInstance(structure)) != null) 13 nb.updateClassifier(current); 14 15 // output generated model 16 System.out.println(nb);
其中,NaiveBayes类和NaiveBayesUpdateable类的区别在于,后者初始化时训练数据可为空,然后动态添加训练数据,而前者都不可以。
参考资料: