📄 randomsearch.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. *//* * RandomSearch.java * Copyright (C) 1999 University of Waikato, Hamilton, New Zealand * */package weka.attributeSelection;import weka.core.Instances;import weka.core.Option;import weka.core.OptionHandler;import weka.core.Range;import weka.core.TechnicalInformation;import weka.core.TechnicalInformation.Type;import weka.core.TechnicalInformation.Field;import weka.core.TechnicalInformationHandler;import weka.core.Utils;import java.util.BitSet;import java.util.Enumeration;import java.util.Random;import java.util.Vector;/** <!-- globalinfo-start --> * RandomSearch : <br/> * <br/> * Performs a Random search in the space of attribute subsets. If no start set is supplied, Random search starts from a random point and reports the best subset found. If a start set is supplied, Random searches randomly for subsets that are as good or better than the start point with the same or or fewer attributes. Using RandomSearch in conjunction with a start set containing all attributes equates to the LVF algorithm of Liu and Setiono (ICML-96).<br/> * <br/> * For more information see:<br/> * <br/> * H. Liu, R. Setiono: A probabilistic approach to feature selection - A filter solution. In: 13th International Conference on Machine Learning, 319-327, 1996. * <p/> <!-- globalinfo-end --> * <!-- technical-bibtex-start --> * BibTeX: * <pre> * @inproceedings{Liu1996, * author = {H. Liu and R. Setiono}, * booktitle = {13th International Conference on Machine Learning}, * pages = {319-327}, * title = {A probabilistic approach to feature selection - A filter solution}, * year = {1996} * } * </pre> * <p/> <!-- technical-bibtex-end --> * <!-- options-start --> * Valid options are: <p/> * * <pre> -P <start set> * Specify a starting set of attributes. * Eg. 1,3,5-7. * If a start point is supplied, * random search evaluates the start * point and then randomly looks for * subsets that are as good as or better * than the start point with the same * or lower cardinality.</pre> * * <pre> -F <percent> * Percent of search space to consider. * (default = 25%).</pre> * * <pre> -V * Output subsets as the search progresses. * (default = false).</pre> * <!-- options-end --> * * @author Mark Hall (mhall@cs.waikato.ac.nz) * @version $Revision: 1.17 $ */public class RandomSearch extends ASSearch implements StartSetHandler, OptionHandler, TechnicalInformationHandler { /** for serialization */ static final long serialVersionUID = 7479392617377425484L; /** * holds a starting set as an array of attributes. */ private int[] m_starting; /** holds the start set as a range */ private Range m_startRange; /** the best feature set found during the search */ private BitSet m_bestGroup; /** the merit of the best subset found */ private double m_bestMerit; /** * only accept a feature set as being "better" than the best if its * merit is better or equal to the best, and it contains fewer * features than the best (this allows LVF to be implimented). */ private boolean m_onlyConsiderBetterAndSmaller; /** does the data have a class */ private boolean m_hasClass; /** holds the class index */ private int m_classIndex; /** number of attributes in the data */ private int m_numAttribs; /** seed for random number generation */ private int m_seed; /** percentage of the search space to consider */ private double m_searchSize; /** the number of iterations performed */ private int m_iterations; /** random number object */ private Random m_random; /** output new best subsets as the search progresses */ private boolean m_verbose; /** * Returns a string describing this search method * @return a description of the search suitable for * displaying in the explorer/experimenter gui */ public String globalInfo() { return "RandomSearch : \n\nPerforms a Random search in " +"the space of attribute subsets. If no start set is supplied, Random " +"search starts from a random point and reports the best subset found. " +"If a start set is supplied, Random searches randomly for subsets " +"that are as good or better than the start point with the same or " +"or fewer attributes. Using RandomSearch in conjunction with a start " +"set containing all attributes equates to the LVF algorithm of Liu " +"and Setiono (ICML-96).\n\n" + "For more information see:\n\n" + getTechnicalInformation().toString(); } /** * Returns an instance of a TechnicalInformation object, containing * detailed information about the technical background of this class, * e.g., paper reference or book this class is based on. * * @return the technical information about this class */ public TechnicalInformation getTechnicalInformation() { TechnicalInformation result; result = new TechnicalInformation(Type.INPROCEEDINGS); result.setValue(Field.AUTHOR, "H. Liu and R. Setiono"); result.setValue(Field.TITLE, "A probabilistic approach to feature selection - A filter solution"); result.setValue(Field.BOOKTITLE, "13th International Conference on Machine Learning"); result.setValue(Field.YEAR, "1996"); result.setValue(Field.PAGES, "319-327"); return result; } /** * Constructor */ public RandomSearch () { resetOptions(); } /** * Returns an enumeration describing the available options. * @return an enumeration of all the available options. **/ public Enumeration listOptions () { Vector newVector = new Vector(3); newVector.addElement(new Option("\tSpecify a starting set of attributes." + "\n\tEg. 1,3,5-7." +"\n\tIf a start point is supplied," +"\n\trandom search evaluates the start" +"\n\tpoint and then randomly looks for" +"\n\tsubsets that are as good as or better" +"\n\tthan the start point with the same" +"\n\tor lower cardinality." ,"P",1 , "-P <start set>")); newVector.addElement(new Option("\tPercent of search space to consider." +"\n\t(default = 25%)." , "F", 1 , "-F <percent> ")); newVector.addElement(new Option("\tOutput subsets as the search progresses." +"\n\t(default = false)." , "V", 0 , "-V")); return newVector.elements(); } /** * Parses a given list of options. <p/> * <!-- options-start --> * Valid options are: <p/> * * <pre> -P <start set> * Specify a starting set of attributes. * Eg. 1,3,5-7. * If a start point is supplied, * random search evaluates the start * point and then randomly looks for * subsets that are as good as or better * than the start point with the same * or lower cardinality.</pre> * * <pre> -F <percent> * Percent of search space to consider. * (default = 25%).</pre> * * <pre> -V * Output subsets as the search progresses. * (default = false).</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 optionString; resetOptions(); optionString = Utils.getOption('P', options); if (optionString.length() != 0) { setStartSet(optionString); } optionString = Utils.getOption('F',options); if (optionString.length() != 0) { setSearchPercent((new Double(optionString)).doubleValue()); } setVerbose(Utils.getFlag('V',options)); } /** * Gets the current settings of RandomSearch. * @return an array of strings suitable for passing to setOptions() */ public String[] getOptions () { String[] options = new String[5]; int current = 0; if (m_verbose) { options[current++] = "-V"; } if (!(getStartSet().equals(""))) { options[current++] = "-P"; options[current++] = "" + startSetToString(); } options[current++] = "-F"; options[current++] = "" + getSearchPercent(); while (current < options.length) { options[current++] = ""; } return options; } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String startSetTipText() { return "Set the start point for the search. This is specified as a comma " +"seperated list off attribute indexes starting at 1. It can include " +"ranges. Eg. 1,2,5-9,17. If specified, Random searches for subsets " +"of attributes that are as good as or better than the start set with " +"the same or lower cardinality."; } /** * Sets a starting set of attributes for the search. It is the * search method's responsibility to report this start set (if any) * in its toString() method. * @param startSet a string containing a list of attributes (and or ranges), * eg. 1,2,6,10-15. "" indicates no start point. * If a start point is supplied, random search evaluates the * start point and then looks for subsets that are as good as or better * than the start point with the same or lower cardinality. * @throws Exception if start set can't be set. */ public void setStartSet (String startSet) throws Exception { m_startRange.setRanges(startSet); } /** * Returns a list of attributes (and or attribute ranges) as a String * @return a list of attributes (and or attribute ranges) */ public String getStartSet () { return m_startRange.getRanges(); } /** * Returns the tip text for this property
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -