📄 vote.java
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Vote.java
* Copyright (C) 2000 Alexander K. Seewald
*
*/
package weka.classifiers.meta;
import weka.classifiers.Evaluation;
import weka.classifiers.MultipleClassifiersCombiner;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.UnsupportedClassTypeException;
/**
* Class for combining classifiers using unweighted average of
* probability estimates (classification) or numeric predictions
* (regression).
*
* Valid options from the command line are:<p>
*
* -B classifierstring <br>
* Classifierstring should contain the full class name of a scheme
* included for selection followed by options to the classifier
* (required, option should be used once for each classifier).<p>
*
* @author Alexander K. Seewald (alex@seewald.at)
* @author Eibe Frank (eibe@cs.waikato.ac.nz)
* @version $Revision$
*/
public class Vote extends MultipleClassifiersCombiner {
/**
* Returns a string describing classifier
* @return a description suitable for
* displaying in the explorer/experimenter gui
*/
public String globalInfo() {
return "Class for combining classifiers using unweighted average of "
+ "probability estimates (classification) or numeric predictions "
+ "(regression).";
}
/**
* Buildclassifier selects a classifier from the set of classifiers
* by minimising error on the training data.
*
* @param data the training data to be used for generating the
* boosted classifier.
* @exception Exception if the classifier could not be built successfully
*/
public void buildClassifier(Instances data) throws Exception {
// Check for non-nominal classes
if (!data.classAttribute().isNominal()) {
throw new UnsupportedClassTypeException("Vote: Nominal class, please.");
}
Instances newData = new Instances(data);
newData.deleteWithMissingClass();
for (int i = 0; i < m_Classifiers.length; i++) {
getClassifier(i).buildClassifier(data);
}
}
/**
* Classifies a given instance using the selected classifier.
*
* @param instance the instance to be classified
* @exception Exception if instance could not be classified
* successfully
*/
public double[] distributionForInstance(Instance instance) throws Exception {
double[] probs = getClassifier(0).distributionForInstance(instance);
for (int i = 1; i < m_Classifiers.length; i++) {
double[] dist = getClassifier(i).distributionForInstance(instance);
for (int j = 0; j < dist.length; j++) {
probs[j] += dist[j];
}
}
for (int j = 0; j < probs.length; j++) {
probs[j] /= (double)m_Classifiers.length;
}
return probs;
}
/**
* Output a representation of this classifier
*/
public String toString() {
if (m_Classifiers == null) {
return "Vote: No model built yet.";
}
String result = "Vote combines";
result += " the probability distributions of these base learners:\n";
for (int i = 0; i < m_Classifiers.length; i++) {
result += '\t' + getClassifierSpec(i) + '\n';
}
return result;
}
/**
* Main method for testing this class.
*
* @param argv should contain the following arguments:
* -t training file [-T test file] [-c class index]
*/
public static void main(String [] argv) {
try {
System.out.println(Evaluation.evaluateModel(new Vote(), argv));
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -