The Majority Vote classifier is a simple ensemble classifier. Given a list of base classifiers, it will sum the most likely votes from each base classifier and return a result based on the majority votes. It does not take into account the confidence of the votes. @author Edward Raff
| 12 | * @author Edward Raff |
| 13 | */ |
| 14 | public class MajorityVote implements Classifier |
| 15 | { |
| 16 | |
| 17 | private static final long serialVersionUID = 7945429768861275845L; |
| 18 | private Classifier[] voters; |
| 19 | |
| 20 | /** |
| 21 | * Creates a new Majority Vote classifier using the given voters. If already trained, the |
| 22 | * Majority Vote classifier can be used immediately. The MajorityVote does not make |
| 23 | * copies of the given classifiers. <br> |
| 24 | * <tt>null</tt> values in the array will have no vote. |
| 25 | * |
| 26 | * @param voters the array of voters to use |
| 27 | */ |
| 28 | public MajorityVote(Classifier... voters) |
| 29 | { |
| 30 | this.voters = voters; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Creates a new Majority Vote classifier using the given voters. If already trained, the |
| 35 | * Majority Vote classifier can be used immediately. The MajorityVote does not make |
| 36 | * copies of the given classifiers. <br> |
| 37 | * <tt>null</tt> values in the array will have no vote. |
| 38 | * |
| 39 | * @param voters the list of voters to use |
| 40 | */ |
| 41 | public MajorityVote(List<Classifier> voters) |
| 42 | { |
| 43 | this.voters = voters.toArray(new Classifier[0]); |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public CategoricalResults classify(DataPoint data) |
| 48 | { |
| 49 | CategoricalResults toReturn = null; |
| 50 | |
| 51 | for (Classifier classifier : voters) |
| 52 | if (classifier != null) |
| 53 | if (toReturn == null) |
| 54 | { |
| 55 | toReturn = classifier.classify(data); |
| 56 | //Instead of allocating a new catResult, reuse the given one. Set the non likely to zero, and most to 1. |
| 57 | for (int i = 0; i < toReturn.size(); i++) |
| 58 | if (i != toReturn.mostLikely()) |
| 59 | toReturn.setProb(i, 0); |
| 60 | else |
| 61 | toReturn.setProb(i, 1.0); |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | CategoricalResults vote = classifier.classify(data); |
| 66 | for (int i = 0; i < toReturn.size(); i++) |
| 67 | toReturn.incProb(vote.mostLikely(), 1.0); |
| 68 | } |
| 69 | |
| 70 | toReturn.normalize(); |
| 71 | return toReturn; |
nothing calls this directly
no outgoing calls
no test coverage detected