Weka.classifiers.meta.vote 中的多数投票算法 [英] Majority vote algorithm in Weka.classifiers.meta.vote

查看:31
本文介绍了Weka.classifiers.meta.vote 中的多数投票算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Weka 中使用的多数投票算法是什么.我试图弄清楚它的代码,但无法理解.

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

推荐答案

在 Weka 中,您可以在 Weka.classifiers.meta.vote 中选择要使用的多个分类器.如果您选择 Majority Voting 作为 combinationRule(仅适用于 nominal 类),那么这些分类器中的每一个都将预测一个名义类标签测试样品.然后将选择预测最多的标签作为 vote 分类器的输出.

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

例如.您选择以下要使用的分类器:trees.J48bayes.NaiveBayesfunctions.LibSVM 来预测天气,可以标记badnormalgood.给定一个新的测试样本,以下是他们的预测:

For example. You select the following classifiers to be used: trees.J48, bayes.NaiveBayes and functions.LibSVM to predict the weather, which can be labelled bad, normal or good. Given a new test sample, these are their predictions:

J48        - bad
NaiveBayes - good
LibSVM     - good

以下每个可能标签的投票结果:

The results in the following votes for each possible label:

bad    - 1
normal - 0
good   - 2

所以 Weka 的 vote 分类器会选择 good 作为测试样本的标签,因为它在所有三个分类器中得票最多.

So Weka's vote classifier will select good as label for the test sample, because it has the most votes amongst all three classifiers.

--编辑--

源代码 向您展示了多数投票是如何实现的.我在下面添加了功能.下面是它的作用的描述:

The function distributionForInstanceMajorityVoting in the source code of Weka's Vote class shows you how the majority voting is implemented. I added the function below. Here is a description of what it does:

代码的工作原理和我上面解释的差不多.测试实例的所有名义类都加载到 votes 中.每个分类器对实例进行分类,概率最高的标签获得投票.如果多个标签具有相同的概率,则所有这些标签都会收到投票.一旦所有分类器都投了票,投票最多的标签将被选为测试实例的标签.如果多个标签的票数相同,则会随机选择其中一个标签.

The code works pretty much as I explained above. All nominal classes of the test instance are loaded into votes. Each classifier classifies the instance and the label with the highest probability gets a vote. If multiple labels have the same probability then all these labels receive a vote. Once all classifiers have cast there vote, the label with the most votes is selected as the label for the test instance. If multiple labels have the same amount of votes, then one of these labels will randomly be selected.

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;
}

这篇关于Weka.classifiers.meta.vote 中的多数投票算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆