Majority voting algorithm in Weka.classifiers.meta.vote

What is the majority vote algorithm used in Weka. I tried to figure out his code, but could not understand it.

+5
source share
1 answer

In Weka, you can select several classifiers to be used in Weka.classifiers.meta.vote. If you choose Majority Votinghow combinationRule(which works only with classes nominal), then each of these classifiers will predict the nominal class label for the test sample. The label that was most predicted will then be selected as the result of the classifier vote.

For instance. You select the following classifications to be used: trees.J48, bayes.NaiveBayesand functions.LibSVMfor weather forecasting, which can be labeled as bad, normalor good. Given the new test sample, these are their predictions:

J48        - bad
NaiveBayes - good
LibSVM     - good

Results in the following votes for each possible label:

bad    - 1
normal - 0
good   - 2

So, Weka votewill select the classifier goodas a label for the test sample, since it has the largest number of votes among all three classifiers.

- Edit -

The function distributionForInstanceMajorityVotingin the source code of the Weka class voteshows you how most votes are executed. I have added the function below. Here is a description of what he is doing:

, . votes. , . , . , , . , .

protected double[] distributionForInstanceMajorityVoting(Instance instance) throws Exception {

  double[] probs = new double[instance.classAttribute().numValues()];
  double[] votes = new double[probs.length];

  for (int i = 0; i < m_Classifiers.length; i++) {
    probs = getClassifier(i).distributionForInstance(instance);
    int maxIndex = 0;
    for(int j = 0; j<probs.length; j++) {
      if(probs[j] > probs[maxIndex])
        maxIndex = j;
    }

    // Consider the cases when multiple classes happen to have the same probability
    for (int j=0; j<probs.length; j++) {
      if (probs[j] == probs[maxIndex])
        votes[j]++;
    }
  }

  for (int i = 0; i < m_preBuiltClassifiers.size(); i++) {
    probs = m_preBuiltClassifiers.get(i).distributionForInstance(instance);
    int maxIndex = 0;

    for(int j = 0; j<probs.length; j++) {
      if(probs[j] > probs[maxIndex])
        maxIndex = j;
    }

    // Consider the cases when multiple classes happen to have the same probability
    for (int j=0; j<probs.length; j++) {
      if (probs[j] == probs[maxIndex])
        votes[j]++;
    }
  }

  int tmpMajorityIndex = 0;
  for (int k = 1; k < votes.length; k++) {
    if (votes[k] > votes[tmpMajorityIndex])
      tmpMajorityIndex = k;
  }

  // Consider the cases when multiple classes receive the same amount of votes
  Vector<Integer> majorityIndexes = new Vector<Integer>();
  for (int k = 0; k < votes.length; k++) {
    if (votes[k] == votes[tmpMajorityIndex])
      majorityIndexes.add(k);
   }

  // Resolve the ties according to a uniform random distribution
  int majorityIndex = majorityIndexes.get(m_Random.nextInt(majorityIndexes.size()));

  //set probs to 0
  probs = new double[probs.length];

  probs[majorityIndex] = 1; //the class that have been voted the most receives 1

  return probs;
}
+13

All Articles