朴素贝叶斯法

 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类的区别在于,后者初始化时训练数据可为空,然后动态添加训练数据,而前者都不可以。

 

参考资料:

  1. 统计学习方法 李航 著
  2. https://en.wikipedia.org/wiki/Naive_Bayes_classifier
  3. zh.wikipedia.org/wiki/贝叶斯定理
  4. http://www.ruanyifeng.com/blog/2013/12/naive_bayes_classifier.html
  5. http://weka.sourceforge.net/doc.stable/

posted on 2016-03-13 10:46  lima  阅读(301)  评论(0编辑  收藏  举报

导航