📄 rdg1.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. *//* * RDG1.java * Copyright (C) 2000 Gabi Schmidberger. * */package weka.datagenerators.classifiers.classification;import weka.core.Attribute;import weka.core.FastVector;import weka.core.Instance;import weka.core.Instances;import weka.core.Option;import weka.core.Utils;import weka.datagenerators.ClassificationGenerator;import weka.datagenerators.Test;import java.io.Serializable;import java.util.Enumeration;import java.util.Random;import java.util.Vector;/** <!-- globalinfo-start --> * A data generator that produces data randomly by producing a decision list.<br/> * The decision list consists of rules.<br/> * Instances are generated randomly one by one. If decision list fails to classify the current instance, a new rule according to this current instance is generated and added to the decision list.<br/> * <br/> * The option -V switches on voting, which means that at the end of the generation all instances are reclassified to the class value that is supported by the most rules.<br/> * <br/> * This data generator can generate 'boolean' attributes (= nominal with the values {true, false}) and numeric attributes. The rules can be 'A' or 'NOT A' for boolean values and 'B < random_value' or 'B >= random_value' for numeric values. * <p/> <!-- globalinfo-end --> * <!-- options-start --> * Valid options are: <p/> * * <pre> -h * Prints this help.</pre> * * <pre> -o <file> * The name of the output file, otherwise the generated data is * printed to stdout.</pre> * * <pre> -r <name> * The name of the relation.</pre> * * <pre> -d * Whether to print debug informations.</pre> * * <pre> -S * The seed for random function (default 1)</pre> * * <pre> -n <num> * The number of examples to generate (default 100)</pre> * * <pre> -a <num> * The number of attributes (default 10).</pre> * * <pre> -c <num> * The number of classes (default 2)</pre> * * <pre> -R <num> * maximum size for rules (default 10) </pre> * * <pre> -M <num> * minimum size for rules (default 1) </pre> * * <pre> -I <num> * number of irrelevant attributes (default 0)</pre> * * <pre> -N * number of numeric attributes (default 0)</pre> * * <pre> -V * switch on voting (default is no voting)</pre> * <!-- options-end --> * * Following an example of a generated dataset: <br/> * <pre> * % * % weka.datagenerators.RDG1 -r expl -a 2 -c 3 -n 4 -N 1 -I 0 -M 2 -R 10 -S 2 * % * relation expl * * attribute a0 {false,true} * attribute a1 numeric * attribute class {c0,c1,c2} * * data * * true,0.496823,c0 * false,0.743158,c1 * false,0.408285,c1 * false,0.993687,c2 * % * % Number of attributes chosen as irrelevant = 0 * % * % DECISIONLIST (number of rules = 3): * % RULE 0: c0 := a1 < 0.986, a0 * % RULE 1: c1 := a1 < 0.95, not(a0) * % RULE 2: c2 := not(a0), a1 >= 0.562 * </pre> * * @author Gabi Schmidberger (gabi@cs.waikato.ac.nz) * @version $Revision: 1.3 $ */public class RDG1 extends ClassificationGenerator { /** for serialization */ static final long serialVersionUID = 7751005204635320414L; /** * class to represent decisionlist */ private class RuleList implements Serializable { /** for serialization */ static final long serialVersionUID = 2830125413361938177L; /** rule list */ private FastVector m_RuleList = null; /** class */ double m_ClassValue = 0.0; /** * returns the class value * * @return the class value */ public double getClassValue() { return m_ClassValue; } /** * sets the class value * * @param newClassValue the new classvalue */ public void setClassValue(double newClassValue) { m_ClassValue = newClassValue; } /** * adds the given test to the list * * @param newTest the test to add */ private void addTest (Test newTest) { if (m_RuleList == null) m_RuleList = new FastVector(); m_RuleList.addElement(newTest); } /** * classifies the given example * * @param example the instance to classify * @return the classification * @throws Exception if classification fails */ private double classifyInstance (Instance example) throws Exception { boolean passedAllTests = true; for (Enumeration e = m_RuleList.elements(); passedAllTests && e.hasMoreElements(); ) { Test test = (Test) e.nextElement(); passedAllTests = test.passesTest(example); } if (passedAllTests) return m_ClassValue; else return -1.0; } /** * returns a string representation of the rule list * * @return the rule list as string */ public String toString () { StringBuffer str = new StringBuffer(); str = str.append(" c" + (int) m_ClassValue + " := "); Enumeration e = m_RuleList.elements(); if (e.hasMoreElements()) { Test test = (Test) e.nextElement(); str = str.append(test.toPrologString()); } while (e.hasMoreElements()) { Test test = (Test) e.nextElement(); str = str.append(", " + test.toPrologString()); } return str.toString(); } } /*end class RuleList ******/ /** Number of attribute the dataset should have */ protected int m_NumAttributes; /** Number of Classes the dataset should have */ protected int m_NumClasses; /** maximum rule size*/ private int m_MaxRuleSize; /** minimum rule size*/ private int m_MinRuleSize; /** number of irrelevant attributes.*/ private int m_NumIrrelevant; /** number of numeric attribute*/ private int m_NumNumeric; /** flag that stores if voting is wished*/ private boolean m_VoteFlag = false; /** decision list */ private FastVector m_DecisionList = null; /** array defines which attributes are irrelevant, with: * true = attribute is irrelevant; false = attribute is not irrelevant*/ boolean[] m_AttList_Irr; /** * initializes the generator with default values */ public RDG1() { super(); setNumAttributes(defaultNumAttributes()); setNumClasses(defaultNumClasses()); setMaxRuleSize(defaultMaxRuleSize()); setMinRuleSize(defaultMinRuleSize()); setNumIrrelevant(defaultNumIrrelevant()); setNumNumeric(defaultNumNumeric()); } /** * Returns a string describing this data generator. * * @return a description of the data generator suitable for * displaying in the explorer/experimenter gui */ public String globalInfo() { return "A data generator that produces data randomly by producing a decision list.\n" + "The decision list consists of rules.\n" + "Instances are generated randomly one by one. If decision list fails " + "to classify the current instance, a new rule according to this current " + "instance is generated and added to the decision list.\n\n" + "The option -V switches on voting, which means that at the end " + "of the generation all instances are " + "reclassified to the class value that is supported by the most rules.\n\n" + "This data generator can generate 'boolean' attributes (= nominal with " + "the values {true, false}) and numeric attributes. The rules can be " + "'A' or 'NOT A' for boolean values and 'B < random_value' or " + "'B >= random_value' for numeric values."; } /** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options */ public Enumeration listOptions() { Vector result = enumToVector(super.listOptions()); result.addElement(new Option( "\tThe number of attributes (default " + defaultNumAttributes() + ").", "a", 1, "-a <num>")); result.addElement(new Option( "\tThe number of classes (default " + defaultNumClasses() + ")", "c", 1, "-c <num>")); result.addElement(new Option( "\tmaximum size for rules (default " + defaultMaxRuleSize() + ") ", "R", 1, "-R <num>")); result.addElement(new Option( "\tminimum size for rules (default " + defaultMinRuleSize() + ") ", "M", 1, "-M <num>")); result.addElement(new Option( "\tnumber of irrelevant attributes (default " + defaultNumIrrelevant() + ")", "I", 1, "-I <num>")); result.addElement(new Option( "\tnumber of numeric attributes (default " + defaultNumNumeric() + ")", "N", 1, "-N")); result.addElement(new Option( "\tswitch on voting (default is no voting)", "V", 1, "-V")); return result.elements(); } /** * Parses a list of options for this object. <p/> * <!-- options-start --> * Valid options are: <p/> * * <pre> -h * Prints this help.</pre> * * <pre> -o <file> * The name of the output file, otherwise the generated data is * printed to stdout.</pre> * * <pre> -r <name> * The name of the relation.</pre> * * <pre> -d * Whether to print debug informations.</pre> * * <pre> -S * The seed for random function (default 1)</pre> * * <pre> -n <num> * The number of examples to generate (default 100)</pre> * * <pre> -a <num> * The number of attributes (default 10).</pre> * * <pre> -c <num> * The number of classes (default 2)</pre> * * <pre> -R <num> * maximum size for rules (default 10) </pre> * * <pre> -M <num> * minimum size for rules (default 1) </pre> * * <pre> -I <num> * number of irrelevant attributes (default 0)</pre> * * <pre> -N * number of numeric attributes (default 0)</pre> * * <pre> -V * switch on voting (default is no voting)</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; super.setOptions(options); tmpStr = Utils.getOption('a', options); if (tmpStr.length() != 0) setNumAttributes(Integer.parseInt(tmpStr)); else setNumAttributes(defaultNumAttributes()); tmpStr = Utils.getOption('c', options); if (tmpStr.length() != 0) setNumClasses(Integer.parseInt(tmpStr)); else setNumClasses(defaultNumClasses()); tmpStr = Utils.getOption('R', options); if (tmpStr.length() != 0) setMaxRuleSize(Integer.parseInt(tmpStr)); else setMaxRuleSize(defaultMaxRuleSize()); tmpStr = Utils.getOption('M', options); if (tmpStr.length() != 0) setMinRuleSize(Integer.parseInt(tmpStr)); else setMinRuleSize(defaultMinRuleSize());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -