【发布时间】:2012-07-22 12:12:15
【问题描述】:
Weka 中使用的多数投票算法是什么。我试图弄清楚它的代码,但无法理解。
【问题讨论】:
标签: weka
Weka 中使用的多数投票算法是什么。我试图弄清楚它的代码,但无法理解。
【问题讨论】:
标签: weka
在 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。每个分类器对实例进行分类,概率最高的标签获得投票。如果多个标签具有相同的概率,则所有这些标签都会获得投票。一旦所有分类器都投票,投票最多的标签将被选为测试实例的 the 标签。如果多个标签的票数相同,则将随机选择其中一个标签。
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;
}
【讨论】:
meta.vote 和您的非 Weka 分类器,然后调用它们中的每一个来预测标签。从那里您可以自己实施多数投票或使用其他一些指标来组合各个结果.