📄 bayesnet.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. *//* * BayesNet.java * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand * */package weka.datagenerators.classifiers.classification;import weka.classifiers.bayes.net.BayesNetGenerator;import weka.core.Instance;import weka.core.Instances;import weka.core.Option;import weka.core.Utils;import weka.datagenerators.ClassificationGenerator;import java.util.Enumeration;import java.util.Vector;/** <!-- globalinfo-start --> * Generates random instances based on a Bayes network. * <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 arcs to use. (default 20)</pre> * * <pre> -C <num> * The cardinality of the attributes and the class. (default 2)</pre> * <!-- options-end --> * * @author FracPete (fracpete at waikato dot ac dot nz) * @version $Revision: 1.3 $ * @see BayesNetGenerator */public class BayesNet extends ClassificationGenerator { /** for serialization */ static final long serialVersionUID = -796118162379901512L; /** the bayesian net generator, that produces the actual data */ protected BayesNetGenerator m_Generator; /** * initializes the generator */ public BayesNet() { super(); setNumAttributes(defaultNumAttributes()); setNumArcs(defaultNumArcs()); setCardinality(defaultCardinality()); } /** * 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 "Generates random instances based on a Bayes network."; } /** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options */ public Enumeration listOptions() { Vector result = enumToVector(super.listOptions()); result.add(new Option( "\tThe number of arcs to use. (default " + defaultNumArcs() + ")", "A", 1, "-A <num>")); result.add(new Option( "\tThe cardinality of the attributes and the class. (default " + defaultCardinality() + ")", "C", 1, "-C <num>")); 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 arcs to use. (default 20)</pre> * * <pre> -C <num> * The cardinality of the attributes and the class. (default 2)</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; Vector list; super.setOptions(options); list = new Vector(); list.add("-N"); list.add("" + getNumAttributes()); list.add("-M"); list.add("" + getNumExamples()); list.add("-S"); list.add("" + getSeed()); list.add("-A"); tmpStr = Utils.getOption('A', options); if (tmpStr.length() != 0) list.add(tmpStr); else list.add("" + defaultNumArcs()); list.add("-C"); tmpStr = Utils.getOption('C', options); if (tmpStr.length() != 0) list.add(tmpStr); else list.add("" + defaultCardinality()); setGeneratorOptions(list); } /** * Gets the current settings of the datagenerator. * * @return an array of strings suitable for passing to setOptions */ public String[] getOptions() { Vector result; String[] options; int i; result = new Vector(); options = removeBlacklist(super.getOptions()); for (i = 0; i < options.length; i++) result.add(options[i]); // determine options from generator options = getGenerator().getOptions(); try { result.add("-A"); result.add(Utils.getOption('A', options)); } catch (Exception e) { e.printStackTrace(); } try { result.add("-C"); result.add(Utils.getOption('C', options)); } catch (Exception e) { e.printStackTrace(); } return (String[]) result.toArray(new String[result.size()]); } /** * sets the given options of the BayesNetGenerator * * @param generator the generator to set the options for * @param options the options to set */ protected void setGeneratorOptions( BayesNetGenerator generator, Vector options) { try { generator.setOptions( (String[]) options.toArray(new String[options.size()])); } catch (Exception e) { e.printStackTrace(); } } /** * returns the actual datagenerator * * @return the actual datagenerator */ protected BayesNetGenerator getGenerator() { if (m_Generator == null) m_Generator = new BayesNetGenerator(); return m_Generator; } /** * sets the given options of the BayesNetGenerator * * @param options the options to set */ protected void setGeneratorOptions(Vector options) { setGeneratorOptions(getGenerator(), options); } /** * sets a specific option/value of the generator (option must be w/o * then '-') * @param generator the generator to set the option for * @param option the option to set * @param value the new value for the option */ protected void setGeneratorOption( BayesNetGenerator generator, String option, String value ) { String[] options; Vector list; int i; try { // get options and remove specific option options = generator.getOptions(); Utils.getOption(option, options); // add option and set the new options list = new Vector(); for (i = 0; i < options.length; i++) { if (options[i].length() != 0) list.add(options[i]); } list.add("-" + option); list.add(value); setGeneratorOptions(generator, list); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -