📄 checkattributeselection.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. *//* * CheckAttributeSelection.java * Copyright (C) 2006 University of Waikato, Hamilton, New Zealand * */package weka.attributeSelection;import weka.core.Attribute;import weka.core.CheckScheme;import weka.core.FastVector;import weka.core.Instances;import weka.core.MultiInstanceCapabilitiesHandler;import weka.core.Option;import weka.core.OptionHandler;import weka.core.SerializationHelper;import weka.core.SerializedObject;import weka.core.TestInstances;import weka.core.Utils;import weka.core.WeightedInstancesHandler;import java.util.Enumeration;import java.util.Random;import java.util.Vector;/** * Class for examining the capabilities and finding problems with * attribute selection schemes. If you implement an attribute selection using * the WEKA.libraries, you should run the checks on it to ensure robustness * and correct operation. Passing all the tests of this object does not mean * bugs in the attribute selection don't exist, but this will help find some * common ones. <p/> * * Typical usage: <p/> * <code>java weka.attributeSelection.CheckAttributeSelection -W ASscheme_name * -- ASscheme_options </code><p/> * * CheckAttributeSelection reports on the following: * <ul> * <li> Scheme abilities * <ul> * <li> Possible command line options to the scheme </li> * <li> Whether the scheme can predict nominal, numeric, string, * date or relational class attributes. </li> * <li> Whether the scheme can handle numeric predictor attributes </li> * <li> Whether the scheme can handle nominal predictor attributes </li> * <li> Whether the scheme can handle string predictor attributes </li> * <li> Whether the scheme can handle date predictor attributes </li> * <li> Whether the scheme can handle relational predictor attributes </li> * <li> Whether the scheme can handle multi-instance data </li> * <li> Whether the scheme can handle missing predictor values </li> * <li> Whether the scheme can handle missing class values </li> * <li> Whether a nominal scheme only handles 2 class problems </li> * <li> Whether the scheme can handle instance weights </li> * </ul> * </li> * <li> Correct functioning * <ul> * <li> Correct initialisation during search (i.e. no result * changes when search is performed repeatedly) </li> * <li> Whether the scheme alters the data pased to it * (number of instances, instance order, instance weights, etc) </li> * </ul> * </li> * <li> Degenerate cases * <ul> * <li> building scheme with zero instances </li> * <li> all but one predictor attribute values missing </li> * <li> all predictor attribute values missing </li> * <li> all but one class values missing </li> * <li> all class values missing </li> * </ul> * </li> * </ul> * Running CheckAttributeSelection with the debug option set will output the * training dataset for any failed tests.<p/> * * The <code>weka.attributeSelection.AbstractAttributeSelectionTest</code> * uses this class to test all the schemes. Any changes here, have to be * checked in that abstract test class, too. <p/> * <!-- options-start --> * Valid options are: <p/> * * <pre> -D * Turn on debugging output.</pre> * * <pre> -S * Silent mode - prints nothing to stdout.</pre> * * <pre> -N <num> * The number of instances in the datasets (default 20).</pre> * * <pre> -nominal <num> * The number of nominal attributes (default 2).</pre> * * <pre> -nominal-values <num> * The number of values for nominal attributes (default 1).</pre> * * <pre> -numeric <num> * The number of numeric attributes (default 1).</pre> * * <pre> -string <num> * The number of string attributes (default 1).</pre> * * <pre> -date <num> * The number of date attributes (default 1).</pre> * * <pre> -relational <num> * The number of relational attributes (default 1).</pre> * * <pre> -num-instances-relational <num> * The number of instances in relational/bag attributes (default 10).</pre> * * <pre> -words <comma-separated-list> * The words to use in string attributes.</pre> * * <pre> -word-separators <chars> * The word separators to use in string attributes.</pre> * * <pre> -eval name [options] * Full name and options of the evaluator analyzed. * eg: weka.attributeSelection.CfsSubsetEval</pre> * * <pre> -search name [options] * Full name and options of the search method analyzed. * eg: weka.attributeSelection.Ranker</pre> * * <pre> -test <eval|search> * The scheme to test, either the evaluator or the search method. * (Default: eval)</pre> * * <pre> * Options specific to evaluator weka.attributeSelection.CfsSubsetEval: * </pre> * * <pre> -M * Treat missing values as a seperate * value.</pre> * * <pre> -L * Don't include locally predictive attributes.</pre> * * <pre> * Options specific to search method weka.attributeSelection.Ranker: * </pre> * * <pre> -P <start set> * Specify a starting set of attributes. * Eg. 1,3,5-7. * Any starting attributes specified are * ignored during the ranking.</pre> * * <pre> -T <threshold> * Specify a theshold by which attributes may be discarded from the ranking.</pre> * * <pre> -N <num to select> * Specify number of attributes to select</pre> * <!-- options-end --> * * @author Len Trigg (trigg@cs.waikato.ac.nz) * @author FracPete (fracpete at waikato dot ac dot nz) * @version $Revision: 1.5 $ * @see TestInstances */public class CheckAttributeSelection extends CheckScheme { /* * Note about test methods: * - methods return array of booleans * - first index: success or not * - second index: acceptable or not (e.g., Exception is OK) * * FracPete (fracpete at waikato dot ac dot nz) */ /*** The evaluator to be examined */ protected ASEvaluation m_Evaluator = new CfsSubsetEval(); /*** The search method to be used */ protected ASSearch m_Search = new Ranker(); /** whether to test the evaluator (default) or the search method */ protected boolean m_TestEvaluator = true; /** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options. */ public Enumeration listOptions() { Vector result = new Vector(); Enumeration en = super.listOptions(); while (en.hasMoreElements()) result.addElement(en.nextElement()); result.addElement(new Option( "\tFull name and options of the evaluator analyzed.\n" +"\teg: weka.attributeSelection.CfsSubsetEval", "eval", 1, "-eval name [options]")); result.addElement(new Option( "\tFull name and options of the search method analyzed.\n" +"\teg: weka.attributeSelection.Ranker", "search", 1, "-search name [options]")); result.addElement(new Option( "\tThe scheme to test, either the evaluator or the search method.\n" +"\t(Default: eval)", "test", 1, "-test <eval|search>")); if ((m_Evaluator != null) && (m_Evaluator instanceof OptionHandler)) { result.addElement(new Option("", "", 0, "\nOptions specific to evaluator " + m_Evaluator.getClass().getName() + ":")); Enumeration enm = ((OptionHandler) m_Evaluator).listOptions(); while (enm.hasMoreElements()) result.addElement(enm.nextElement()); } if ((m_Search != null) && (m_Search instanceof OptionHandler)) { result.addElement(new Option("", "", 0, "\nOptions specific to search method " + m_Search.getClass().getName() + ":")); Enumeration enm = ((OptionHandler) m_Search).listOptions(); while (enm.hasMoreElements()) result.addElement(enm.nextElement()); } return result.elements(); } /** * Parses a given list of options. <p/> * <!-- options-start --> * Valid options are: <p/> * * <pre> -D * Turn on debugging output.</pre> * * <pre> -S * Silent mode - prints nothing to stdout.</pre> * * <pre> -N <num> * The number of instances in the datasets (default 20).</pre> * * <pre> -nominal <num> * The number of nominal attributes (default 2).</pre> * * <pre> -nominal-values <num> * The number of values for nominal attributes (default 1).</pre> * * <pre> -numeric <num> * The number of numeric attributes (default 1).</pre> * * <pre> -string <num> * The number of string attributes (default 1).</pre> * * <pre> -date <num> * The number of date attributes (default 1).</pre> * * <pre> -relational <num> * The number of relational attributes (default 1).</pre> * * <pre> -num-instances-relational <num> * The number of instances in relational/bag attributes (default 10).</pre> * * <pre> -words <comma-separated-list> * The words to use in string attributes.</pre> * * <pre> -word-separators <chars> * The word separators to use in string attributes.</pre> * * <pre> -eval name [options] * Full name and options of the evaluator analyzed. * eg: weka.attributeSelection.CfsSubsetEval</pre> * * <pre> -search name [options] * Full name and options of the search method analyzed. * eg: weka.attributeSelection.Ranker</pre> * * <pre> -test <eval|search> * The scheme to test, either the evaluator or the search method. * (Default: eval)</pre> * * <pre> * Options specific to evaluator weka.attributeSelection.CfsSubsetEval: * </pre> * * <pre> -M * Treat missing values as a seperate * value.</pre> * * <pre> -L * Don't include locally predictive attributes.</pre> * * <pre> * Options specific to search method weka.attributeSelection.Ranker: * </pre> * * <pre> -P <start set> * Specify a starting set of attributes. * Eg. 1,3,5-7. * Any starting attributes specified are * ignored during the ranking.</pre> * * <pre> -T <threshold> * Specify a theshold by which attributes may be discarded from the ranking.</pre> * * <pre> -N <num to select> * Specify number of attributes to select</pre> * <!-- options-end --> * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ public void setOptions(String[] options) throws Exception { String tmpStr; String[] tmpOptions; super.setOptions(options); tmpStr = Utils.getOption("eval", options); tmpOptions = Utils.splitOptions(tmpStr); if (tmpOptions.length != 0) { tmpStr = tmpOptions[0]; tmpOptions[0] = ""; setEvaluator( (ASEvaluation) forName( "weka.attributeSelection", ASEvaluation.class, tmpStr, tmpOptions)); } tmpStr = Utils.getOption("search", options); tmpOptions = Utils.splitOptions(tmpStr); if (tmpOptions.length != 0) { tmpStr = tmpOptions[0]; tmpOptions[0] = ""; setSearch( (ASSearch) forName( "weka.attributeSelection", ASSearch.class, tmpStr, tmpOptions)); } tmpStr = Utils.getOption("test", options); setTestEvaluator(!tmpStr.equalsIgnoreCase("search")); } /** * Gets the current settings of the CheckAttributeSelection. * * @return an array of strings suitable for passing to setOptions */ public String[] getOptions() { Vector result; String[] options; int i; result = new Vector(); options = super.getOptions(); for (i = 0; i < options.length; i++) result.add(options[i]); result.add("-eval"); if (getEvaluator() instanceof OptionHandler) result.add( getEvaluator().getClass().getName() + " " + Utils.joinOptions(((OptionHandler) getEvaluator()).getOptions())); else result.add( getEvaluator().getClass().getName()); result.add("-search"); if (getSearch() instanceof OptionHandler) result.add( getSearch().getClass().getName() + " " + Utils.joinOptions(((OptionHandler) getSearch()).getOptions())); else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -