Sic*_*cco 13
在Weka中,您可以选择要使用的多个分类器Weka.classifiers.meta.vote.如果选择Majority Votingas combinationRule(仅适用于nominal类),则每个分类器将预测测试样本的标称类标签.然后将选择预测最多的标签作为vote分类器的输出.
例如.您可以选择使用以下分类:trees.J48,bayes.NaiveBayes并functions.LibSVM预测天气,这可以被标记bad,normal或good.给出一个新的测试样本,这些是他们的预测:
J48 - bad
NaiveBayes - good
LibSVM - good
Run Code Online (Sandbox Code Playgroud)
在下面的结果票数为每个可能的标签:
bad - 1
normal - 0
good - 2
Run Code Online (Sandbox Code Playgroud)
因此,Weka的vote分类器将选择good作为测试样本的标签,因为它在所有三个分类器中拥有最多的选票.
- 编辑 -
该功能distributionForInstanceMajorityVoting在源代码 Weka中的的Vote类显示了多数表决是如何实现的.我添加了下面的功能.这是对它的作用的描述:
代码的工作原理与我上面解释的相同.测试实例的所有标称类都被加载到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;
}
Run Code Online (Sandbox Code Playgroud)