001 /* 002 * Created on Apr 14, 2005 003 * 004 */ 005 package aima.learning.learners; 006 007 import java.util.ArrayList; 008 import java.util.List; 009 010 import aima.learning.framework.DataSet; 011 import aima.learning.framework.Example; 012 import aima.learning.framework.Learner; 013 import aima.util.Util; 014 015 /** 016 * @author Ravi Mohan 017 * 018 */ 019 public class MajorityLearner implements Learner { 020 021 private DataSet dataset; 022 023 private String result; 024 025 public void train(DataSet ds) { 026 List<String> targets = new ArrayList<String>(); 027 for (Example e : ds.examples) { 028 targets.add(e.targetValue()); 029 } 030 result = Util.mode(targets); 031 } 032 033 public String predict(Example e) { 034 return result; 035 } 036 037 public int[] test(DataSet ds) { 038 int[] results = new int[] { 0, 0 }; 039 040 for (Example e : ds.examples) { 041 if (e.targetValue().equals(result)) { 042 results[0] = results[0] + 1; 043 } else { 044 results[1] = results[1] + 1; 045 } 046 } 047 return results; 048 } 049 050 }