datagenerator.java
来自「Java 编写的多种数据挖掘算法 包括聚类、分类、预处理等」· Java 代码 · 共 767 行 · 第 1/2 页
JAVA
767 行
* * @return the default number of actual examples */ protected int defaultNumExamplesAct() { return 0; } /** * Sets the number of examples the dataset should have. * @param numExamplesAct the new number of examples */ protected void setNumExamplesAct(int numExamplesAct) { m_NumExamplesAct = numExamplesAct; } /** * Gets the number of examples the dataset should have. * @return the number of examples the dataset should have */ protected int getNumExamplesAct() { return m_NumExamplesAct; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ protected String numExamplesActTipText() { return "The actual number of examples to generate."; } /** * Sets the print writer. * @param newOutput the new print writer */ public void setOutput(PrintWriter newOutput) { m_Output = newOutput; m_DefaultOutput = null; } /** * Gets the print writer. * @return print writer object */ public PrintWriter getOutput() { return m_Output; } /** * Gets the string writer, which is used for outputting to stdout. * A workaround for the problem of closing stdout when closing the * associated Printwriter. * @return print string writer object */ public StringWriter defaultOutput() { return m_DefaultOutput; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String outputTipText() { return "The output writer to use for printing the generated data."; } /** * Sets the format of the dataset that is to be generated. * @param newFormat the new dataset format of the dataset */ public void setDatasetFormat(Instances newFormat) { m_DatasetFormat = new Instances(newFormat, 0); } /** * Gets the format of the dataset that is to be generated. * @return the dataset format of the dataset */ public Instances getDatasetFormat() { if (m_DatasetFormat != null) return new Instances(m_DatasetFormat, 0); else return null; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String formatTipText() { return "The data format to use."; } /** * returns the default seed * * @return the default seed */ protected int defaultSeed() { return 1; } /** * Gets the random number seed. * * @return the random number seed. */ public int getSeed() { return m_Seed; } /** * Sets the random number seed. * * @param newSeed the new random number seed. */ public void setSeed(int newSeed) { m_Seed = newSeed; m_Random = new Random(newSeed); } /** * 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 "The seed value for the random number generator."; } /** * Gets the random generator. * * @return the random generator */ public Random getRandom() { if (m_Random == null) m_Random = new Random (getSeed()); return m_Random; } /** * Sets the random generator. * * @param newRandom is the random generator. */ public void setRandom(Random newRandom) { m_Random = newRandom; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String randomTipText() { return "The random number generator to use."; } /** * Returns a string representing the dataset in the instance queue. * @return the string representing the output data format */ protected String toStringFormat() { if (m_DatasetFormat == null) return ""; return m_DatasetFormat.toString(); } /** * removes all entries from the options blacklist */ protected static void clearBlacklist() { m_OptionBlacklist.clear(); } /** * adds the given option, e.g., for "-V" use "V", to the blacklist of options * that are not to be output via the makeOptionString method * @param option the option to exclude from listing * @see #makeOptionString(DataGenerator) */ protected static void addToBlacklist(String option) { m_OptionBlacklist.add(option); } /** * checks, whether the given option is in the blacklist of options not to * be output by makeOptionString * @param option the option to check * @return true if the option is on the blacklist * @see #makeOptionString(DataGenerator) */ protected static boolean isOnBlacklist(String option) { return m_OptionBlacklist.contains(option); } /** * removes all the options from the options array that are blacklisted * * @param options the options to remove from the blacklist * @return the processed options array */ protected String[] removeBlacklist(String[] options) { Enumeration enm; Hashtable pool; Option option; // retrieve options that are on blacklist enm = listOptions(); pool = new Hashtable(); while (enm.hasMoreElements()) { option = (Option) enm.nextElement(); if (isOnBlacklist(option.name())) pool.put(option.name(), option); } // remove options enm = pool.keys(); while (enm.hasMoreElements()) { option = (Option) pool.get(enm.nextElement()); try { if (option.numArguments() == 0) Utils.getFlag(option.name(), options); else Utils.getOption(option.name(), options); } catch (Exception e) { e.printStackTrace(); } } return options; } /** * returns all the options in a string * * @param generator the DataGenerator to return all the options for * @return the assembled option string */ protected static String makeOptionString(DataGenerator generator) { StringBuffer result; Enumeration enm; Option option; result = new StringBuffer(); result.append("\nData Generator options:\n\n"); enm = generator.listOptions(); while (enm.hasMoreElements()) { option = (Option) enm.nextElement(); // skip option if on blacklist if (isOnBlacklist(option.name())) continue; result.append(option.synopsis() + "\n" + option.description() + "\n"); } return result.toString(); } /** * Calls the data generator. * * @param generator one of the data generators * @param options options of the data generator * @throws Exception if there was an error in the option list */ public static void makeData(DataGenerator generator, String[] options) throws Exception { boolean printhelp; Vector unknown; int i; // help? printhelp = (Utils.getFlag('h', options)); // read options if (!printhelp) { try { options = generator.removeBlacklist(options); generator.setOptions(options); // check for left-over options, but don't raise exception unknown = new Vector(); for (i = 0; i < options.length; i++) { if (options[i].length() != 0) unknown.add(options[i]); } if (unknown.size() > 0) { System.out.print("Unknown options:"); for (i = 0; i < unknown.size(); i++) System.out.print(" " + unknown.get(i)); System.out.println(); } } catch (Exception e) { e.printStackTrace(); printhelp = true; } } if (printhelp) { System.out.println(makeOptionString(generator)); return; } // define dataset format // computes actual number of examples to be produced generator.setDatasetFormat(generator.defineDataFormat()); // get print writer PrintWriter output = generator.getOutput(); // output of options output.println("%"); output.println("% Commandline"); output.println("%"); output.println("% " + generator.getClass().getName() + " " + Utils.joinOptions(generator.getOptions())); output.println("%"); // comment at beginning of ARFF File String commentAtStart = generator.generateStart(); if (commentAtStart.length() > 0) { output.println("%"); output.println("% Prologue"); output.println("%"); output.println(commentAtStart.trim()); output.println("%"); } // ask data generator which mode boolean singleMode = generator.getSingleModeFlag(); // start data producer if (singleMode) { // output of dataset header output.println(generator.toStringFormat()); for (i = 0; i < generator.getNumExamplesAct(); i++) { // over all examples to be produced Instance inst = generator.generateExample(); output.println(inst); } } else { // generator produces all instances at once Instances dataset = generator.generateExamples(); // output of dataset output.println(dataset); } // comment at end of ARFF File String commentAtEnd = generator.generateFinished(); if (commentAtEnd.length() > 0) { output.println("%"); output.println("% Epilogue"); output.println("%"); output.println(commentAtEnd.trim()); output.println("%"); } output.flush(); output.close(); // print result to stdout? if (generator.defaultOutput() != null) System.out.println(generator.defaultOutput().toString()); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?