📄 classifiersplitevaluator.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.
*/
/*
* ClassifierSplitEvaluator.java
* Copyright (C) 1999 Len Trigg
*
*/
package weka.experiment;
import java.io.*;
import java.util.*;
import weka.core.*;
import weka.classifiers.*;
import weka.classifiers.rules.ZeroR;
/**
* A SplitEvaluator that produces results for a classification scheme
* on a nominal class attribute.
*
* -W classname <br>
* Specify the full class name of the classifier to evaluate. <p>
*
* -C class index <br>
* The index of the class for which IR statistics are to
* be output. (default 1) <p>
*
* -I attr index <br>
* The index of an attribute to output in the tresults. This
* attribute should identify an instance in order to know
* which instances are tested in a fold (default 1).
*
* -P
* Add the prediction and target columns to the result file for each fold.
*
* @author Len Trigg (trigg@cs.waikato.ac.nz)
* @version $Revision: 1.1 $
*/
public class ClassifierSplitEvaluator implements SplitEvaluator,
OptionHandler, AdditionalMeasureProducer {
/** The template classifier */
protected Classifier m_Template = new ZeroR();
/** The classifier used for evaluation */
protected Classifier m_Classifier;
/** The names of any additional measures to look for in SplitEvaluators */
protected String [] m_AdditionalMeasures = null;
/** Array of booleans corresponding to the measures in m_AdditionalMeasures
indicating which of the AdditionalMeasures the current classifier
can produce */
protected boolean [] m_doesProduce = null;
/** The number of additional measures that need to be filled in
after taking into account column constraints imposed by the final
destination for results */
protected int m_numberAdditionalMeasures = 0;
/** Holds the statistics for the most recent application of the classifier */
protected String m_result = null;
/** The classifier options (if any) */
protected String m_ClassifierOptions = "";
/** The classifier version */
protected String m_ClassifierVersion = "";
/** The length of a key */
private static final int KEY_SIZE = 3;
/** The length of a result */
private static final int RESULT_SIZE = 25;
/** The number of IR statistics */
private static final int NUM_IR_STATISTICS = 11;
/** Class index for information retrieval statistics (default 0) */
private int m_IRclass = 0;
/** Flag for prediction and target columns output.*/
private boolean m_predTargetColumn = false;
/** Attribute index of instance identifier (default -1) */
private int m_attID = -1;
/**
* No args constructor.
*/
public ClassifierSplitEvaluator() {
updateOptions();
}
/**
* Returns a string describing this split evaluator
* @return a description of the split evaluator suitable for
* displaying in the explorer/experimenter gui
*/
public String globalInfo() {
return " A SplitEvaluator that produces results for a classification "
+"scheme on a nominal class attribute.";
}
/**
* Returns an enumeration describing the available options..
*
* @return an enumeration of all the available options.
*/
public Enumeration listOptions() {
Vector newVector = new Vector(4);
newVector.addElement(new Option(
"\tThe full class name of the classifier.\n"
+"\teg: weka.classifiers.bayes.NaiveBayes",
"W", 1,
"-W <class name>"));
newVector.addElement(new Option(
"\tThe index of the class for which IR statistics\n" +
"\tare to be output. (default 1)",
"C", 1,
"-C <index>"));
newVector.addElement(new Option(
"\tThe index of an attribute to output in the\n" +
"\tresults. This attribute should identify an\n" +
"\tinstance in order to know which instances are\n" +
"\tin the test set of a cross validation. if 0\n" +
"\tno output (default 0).",
"I", 1,
"-I <index>"));
newVector.addElement(new Option(
"\tAdd target and prediction columns to the result\n" +
"\tfor each fold.",
"P", 0,
"-P"));
if ((m_Template != null) &&
(m_Template instanceof OptionHandler)) {
newVector.addElement(new Option(
"",
"", 0, "\nOptions specific to classifier "
+ m_Template.getClass().getName() + ":"));
Enumeration enu = ((OptionHandler)m_Template).listOptions();
while (enu.hasMoreElements()) {
newVector.addElement(enu.nextElement());
}
}
return newVector.elements();
}
/**
* Parses a given list of options. Valid options are:<p>
*
* -W classname <br>
* Specify the full class name of the classifier to evaluate. <p>
*
* -C class index <br>
* The index of the class for which IR statistics are to
* be output. (default 1) <p>
*
* -I attr index <br>
* The index of an attribute to output in the tresults. This
* attribute should identify an instance in order to know
* which instances are tested in a fold. if zero, no output (default 0).
*
* -P
* The flag that indicate if the prediction and targets have to be output
* in the result files for each fold.
*
* All option after -- will be passed to the classifier.
*
* @param options the list of options as an array of strings
* @exception Exception if an option is not supported
*/
public void setOptions(String[] options) throws Exception {
String cName = Utils.getOption('W', options);
if (cName.length() == 0) {
throw new Exception("A classifier must be specified with"
+ " the -W option.");
}
// Do it first without options, so if an exception is thrown during
// the option setting, listOptions will contain options for the actual
// Classifier.
setClassifier(Classifier.forName(cName, null));
if (getClassifier() instanceof OptionHandler) {
((OptionHandler) getClassifier())
.setOptions(Utils.partitionOptions(options));
updateOptions();
}
String indexName = Utils.getOption('C', options);
if (indexName.length() != 0) {
m_IRclass = (new Integer(indexName)).intValue() - 1;
} else {
m_IRclass = 0;
}
String attID = Utils.getOption('I', options);
if (attID.length() != 0) {
m_attID = (new Integer(attID)).intValue() - 1;
} else {
m_attID = -1;
}
m_predTargetColumn = Utils.getFlag('P', options);
}
/**
* Gets the current settings of the Classifier.
*
* @return an array of strings suitable for passing to setOptions
*/
public String [] getOptions() {
String [] classifierOptions = new String [0];
if ((m_Template != null) &&
(m_Template instanceof OptionHandler)) {
classifierOptions = ((OptionHandler)m_Template).getOptions();
}
String [] options = new String [classifierOptions.length + 8];
int current = 0;
if (getClassifier() != null) {
options[current++] = "-W";
options[current++] = getClassifier().getClass().getName();
}
options[current++] = "-I";
options[current++] = "" + (m_attID + 1);
if (getPredTargetColumn()) options[current++] = "-P";
options[current++] = "-C";
options[current++] = "" + (m_IRclass + 1);
options[current++] = "--";
System.arraycopy(classifierOptions, 0, options, current,
classifierOptions.length);
current += classifierOptions.length;
while (current < options.length) {
options[current++] = "";
}
return options;
}
/**
* Set a list of method names for additional measures to look for
* in Classifiers. This could contain many measures (of which only a
* subset may be produceable by the current Classifier) if an experiment
* is the type that iterates over a set of properties.
* @param additionalMeasures a list of method names
*/
public void setAdditionalMeasures(String [] additionalMeasures) {
// System.err.println("ClassifierSplitEvaluator: setting additional measures");
m_AdditionalMeasures = additionalMeasures;
// determine which (if any) of the additional measures this classifier
// can produce
if (m_AdditionalMeasures != null && m_AdditionalMeasures.length > 0) {
m_doesProduce = new boolean [m_AdditionalMeasures.length];
if (m_Template instanceof AdditionalMeasureProducer) {
Enumeration en = ((AdditionalMeasureProducer)m_Template).
enumerateMeasures();
while (en.hasMoreElements()) {
String mname = (String)en.nextElement();
for (int j=0;j<m_AdditionalMeasures.length;j++) {
if (mname.compareToIgnoreCase(m_AdditionalMeasures[j]) == 0) {
m_doesProduce[j] = true;
}
}
}
}
} else {
m_doesProduce = null;
}
}
/**
* Returns an enumeration of any additional measure names that might be
* in the classifier
* @return an enumeration of the measure names
*/
public Enumeration enumerateMeasures() {
Vector newVector = new Vector();
if (m_Template instanceof AdditionalMeasureProducer) {
Enumeration en = ((AdditionalMeasureProducer)m_Template).
enumerateMeasures();
while (en.hasMoreElements()) {
String mname = (String)en.nextElement();
newVector.addElement(mname);
}
}
return newVector.elements();
}
/**
* Returns the value of the named measure
* @param measureName the name of the measure to query for its value
* @return the value of the named measure
* @exception IllegalArgumentException if the named measure is not supported
*/
public double getMeasure(String additionalMeasureName) {
if (m_Template instanceof AdditionalMeasureProducer) {
if (m_Classifier == null) {
throw new IllegalArgumentException("ClassifierSplitEvaluator: " +
"Can't return result for measure, " +
"classifier has not been built yet.");
}
return ((AdditionalMeasureProducer)m_Classifier).
getMeasure(additionalMeasureName);
} else {
throw new IllegalArgumentException("ClassifierSplitEvaluator: "
+"Can't return value for : "+additionalMeasureName
+". "+m_Template.getClass().getName()+" "
+"is not an AdditionalMeasureProducer");
}
}
/**
* Gets the data types of each of the key columns produced for a single run.
* The number of key fields must be constant
* for a given SplitEvaluator.
*
* @return an array containing objects of the type of each key column. The
* objects should be Strings, or Doubles.
*/
public Object [] getKeyTypes() {
Object [] keyTypes = new Object[KEY_SIZE];
keyTypes[0] = "";
keyTypes[1] = "";
keyTypes[2] = "";
return keyTypes;
}
/**
* Gets the names of each of the key columns produced for a single run.
* The number of key fields must be constant
* for a given SplitEvaluator.
*
* @return an array containing the name of each key column
*/
public String [] getKeyNames() {
String [] keyNames = new String[KEY_SIZE];
keyNames[0] = "Scheme";
keyNames[1] = "Scheme_options";
keyNames[2] = "Scheme_version_ID";
return keyNames;
}
/**
* Gets the key describing the current SplitEvaluator. For example
* This may contain the name of the classifier used for classifier
* predictive evaluation. The number of key fields must be constant
* for a given SplitEvaluator.
*
* @return an array of objects containing the key.
*/
public Object [] getKey(){
Object [] key = new Object[KEY_SIZE];
key[0] = m_Template.getClass().getName();
key[1] = m_ClassifierOptions;
key[2] = m_ClassifierVersion;
return key;
}
/**
* Gets the data types of each of the result columns produced for a
* single run. The number of result fields must be constant
* for a given SplitEvaluator.
*
* @return an array containing objects of the type of each result column.
* The objects should be Strings, or Doubles.
*/
public Object [] getResultTypes() {
int addm = (m_AdditionalMeasures != null)
? m_AdditionalMeasures.length
: 0;
int overall_length = RESULT_SIZE+addm;
overall_length += NUM_IR_STATISTICS;
if (getAttributeID() >= 0) overall_length += 1;
if (getPredTargetColumn()) overall_length += 2;
Object [] resultTypes = new Object[overall_length];
Double doub = new Double(0);
int current = 0;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
// IR stats
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
resultTypes[current++] = doub;
// Timing stats
resultTypes[current++] = doub;
resultTypes[current++] = doub;
// ID/Targets/Predictions
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -