Weka中使用的多数票算法是什么?我试着弄明白它的代码,但我不能理解它。
发布于 2012-07-24 17:25:50
在Weka中,您可以选择在Weka.classifiers.meta.vote中使用多个分类器。如果您选择Majority Voting作为combinationRule (它只适用于nominal类),那么这些分类器中的每一个都将为测试样本预测一个标称类标签。然后,将选择预测最多的标签作为vote分类器的输出。
例如。您可以选择要使用的以下分类器:trees.J48、bayes.NaiveBayes和functions.LibSVM来预测天气,这些分类器可以被标记为bad、normal或good。给定一个新的测试样本,以下是他们的预测:
J48 - bad
NaiveBayes - good
LibSVM - good每个可能标签的投票结果如下:
bad - 1
normal - 0
good - 2因此,Weka的vote分类器将选择good作为测试样本的标签,因为它在所有三个分类器中拥有最多的选票。
--编辑--
Weka的Vote类的source code中的distributionForInstanceMajorityVoting函数向您展示了多数投票是如何实现的。我添加了下面的函数。下面是对它的功能的描述:
代码的工作原理和我上面解释的差不多。测试实例的所有名义类都被加载到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;
}https://stackoverflow.com/questions/11626417
复制相似问题