📄 classorder.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. *//* * ClassOrder.java * Copyright (C) 2002 Xin Xu * */package weka.filters.supervised.attribute;import weka.core.Attribute;import weka.core.Capabilities;import weka.core.FastVector;import weka.core.Instance;import weka.core.Instances;import weka.core.Option;import weka.core.OptionHandler;import weka.core.Utils;import weka.core.Capabilities.Capability;import weka.filters.Filter;import weka.filters.SupervisedFilter;import java.util.Enumeration;import java.util.Random;import java.util.Vector;/** <!-- globalinfo-start --> * Changes the order of the classes so that the class values are no longer of in the order specified in the header. The values will be in the order specified by the user -- it could be either in ascending/descending order by the class frequency or in random order. Note that this filter currently does not change the header, only the class values of the instances, so there is not much point in using it in conjunction with the FilteredClassifier. The value can also be converted back using 'originalValue(double value)' procedure. * <p/> <!-- globalinfo-end --> * <!-- options-start --> * Valid options are: <p/> * * <pre> -R <seed> * Specify the seed of randomization * used to randomize the class * order (default: 1)</pre> * * <pre> -C <order> * Specify the class order to be * sorted, could be 0: ascending * 1: descending and 2: random.(default: 0)</pre> * <!-- options-end --> * * @author Xin Xu (xx5@cs.waikato.ac.nz) * @author Eibe Frank (eibe@cs.waikato.ac.nz) * @version $Revision: 1.7 $ */public class ClassOrder extends Filter implements SupervisedFilter, OptionHandler { /** for serialization */ static final long serialVersionUID = -2116226838887628411L; /** The seed of randomization */ private long m_Seed = 1; /** The random object */ private Random m_Random = null; /** * The 1-1 converting table from the original class values * to the new values */ private int[] m_Converter = null; /** Class attribute of the data */ private Attribute m_ClassAttribute = null; /** The class order to be sorted */ private int m_ClassOrder = 0; /** The class values are sorted in ascending order based on their frequencies */ public static final int FREQ_ASCEND = 0; /** The class values are sorted in descending order based on their frequencies */ public static final int FREQ_DESCEND = 1; /** The class values are sorted in random order*/ public static final int RANDOM =2; /** This class can provide the class distribution in the sorted order * as side effect */ private double[] m_ClassCounts = null; /** * Returns a string describing this filter * * @return a description of the filter suitable for * displaying in the explorer/experimenter gui */ public String globalInfo() { return "Changes the order of the classes so that the class values are " + "no longer of in the order specified in the header. " + "The values will be in the order specified by the user " + "-- it could be either in ascending/descending order by the class " + "frequency or in random order. Note that this filter currently does not " + "change the header, only the class values of the instances, " + "so there is not much point in using it in conjunction with the " + "FilteredClassifier. The value can also be converted back using " + "'originalValue(double value)' procedure."; } /** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options. */ public Enumeration listOptions() { Vector newVector = new Vector(1); newVector.addElement(new Option("\tSpecify the seed of randomization\n" + "\tused to randomize the class\n" + "\torder (default: 1)", "R", 1, "-R <seed>")); newVector.addElement(new Option("\tSpecify the class order to be\n" + "\tsorted, could be 0: ascending\n" + "\t1: descending and 2: random.(default: 0)", "C", 1, "-C <order>")); return newVector.elements(); } /** * Parses a given list of options. <p/> * <!-- options-start --> * Valid options are: <p/> * * <pre> -R <seed> * Specify the seed of randomization * used to randomize the class * order (default: 1)</pre> * * <pre> -C <order> * Specify the class order to be * sorted, could be 0: ascending * 1: descending and 2: random.(default: 0)</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 seedString = Utils.getOption('R', options); if (seedString.length() != 0) m_Seed = Long.parseLong(seedString); else m_Seed = 1; String orderString = Utils.getOption('C', options); if (orderString.length() != 0) m_ClassOrder = Integer.parseInt(orderString); else m_ClassOrder = FREQ_ASCEND; if (getInputFormat() != null) setInputFormat(getInputFormat()); m_Random = null; } /** * Gets the current settings of the filter. * * @return an array of strings suitable for passing to setOptions */ public String [] getOptions() { String [] options = new String [4]; int current = 0; options[current++] = "-R"; options[current++] = "" + m_Seed; options[current++] = "-C"; options[current++] = "" + m_ClassOrder; 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 seedTipText() { return "Specify the seed of randomization of the class order"; } /** * Get the current randomization seed * * @return a seed */ public long getSeed() { return m_Seed; } /** * Set randomization seed * * @param seed the set seed */ public void setSeed(long seed){ m_Seed = seed; m_Random = null; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String classOrderTipText() { return "Specify the class order after the filtering"; } /** * Get the wanted class order * * @return class order */ public int getClassOrder() { return m_ClassOrder; } /** * Set the wanted class order
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -