📄 clustergenerator.java
字号:
/* * Generator.java * Copyright (C) 2000 Gabi Schmidberger * * 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. */package weka.datagenerators;import java.lang.Exception;import java.io.FileOutputStream;import java.io.PrintWriter;import java.io.Serializable;import java.util.Enumeration;import weka.core.Instance;import weka.core.Instances;import weka.core.Option;import weka.core.OptionHandler;import weka.core.Utils;import weka.core.Attribute;import weka.core.FastVector;/** * Abstract class for cluster data generators. * * ------------------------------------------------------------------- <p> * * General options are: <p> * * -r string <br> * Name of the relation of the generated dataset. <br> * (default = name built using name of used generator and options) <p> * * -a num <br> * Number of attributes. (default = 2) <p> * * -k num <br> * Number of clusters. (default = 4) <p> * * -c <br> * Class Flag. If set, cluster is listed in extra class attribute.<p> * * -o filename<br> * writes the generated dataset to the given file using ARFF-Format. * (default = stdout). * * ------------------------------------------------------------------- <p> * * Example usage as the main of a datagenerator called RandomGenerator: * <code> <pre> * public static void main(String [] args) { * try { * DataGenerator.makeData(new RandomGenerator(), argv); * } catch (Exception e) { * System.err.println(e.getMessage()); * } * } * </pre> </code> * <p> * * ------------------------------------------------------------------ <p> * * * @author Gabi Schmidberger (gabi@cs.waikato.ac.nz) * @version $Revision: 1.1.1.1 $ */public abstract class ClusterGenerator implements Serializable { /** @serial Debugging mode */ private boolean m_Debug = false; /** @serial The format for the generated dataset */ private Instances m_Format = null; /** @serial Relation name the dataset should have */ private String m_RelationName = ""; /** @serial Number of attribute the dataset should have */ protected int m_NumAttributes = 2; /** @serial Number of Clusters the dataset should have */ protected int m_NumClusters = 4; /** @serial class flag */ private boolean m_ClassFlag = false; /** @serial Number of instances that should be produced into the dataset * this number is by default m_NumExamples, * but can be reset by the generator */ private int m_NumExamplesAct = 0; /** @serial PrintWriter */ private PrintWriter m_Output = null; /** * Initializes the format for the dataset produced. * Must be called before the generateExample or generateExamples * methods are used. * * @return the format for the dataset * @exception Exception if the generating of the format failed */ abstract Instances defineDataFormat() throws Exception; /** * Generates one example of the dataset. * * @return the generated example * @exception Exception if the format of the dataset is not yet defined * @exception Exception if the generator only works with generateExamples * which means in non single mode */ abstract Instance generateExample() throws Exception; /** * Generates all examples of the dataset. * * @return the generated dataset * @exception Exception if the format of the dataset is not yet defined * @exception Exception if the generator only works with generateExample, * which means in single mode */ abstract Instances generateExamples() throws Exception; /** * Generates a comment string that documentates the data generator. * By default this string is added at the beginning of the produced output * as ARFF file type, next after the options. * * @return string contains info about the generated rules * @exception Exception if the generating of the documentation fails */ abstract String generateStart () throws Exception; /** * Generates a comment string that documentates the data generator. * By default this string is added at the end of the produced output * as ARFF file type. * * @return string contains info about the generated rules * @exception Exception if the generating of the documentation fails */ abstract String generateFinished () throws Exception; /** * Return if single mode is set for the given data generator * mode depends on option setting and or generator type. * * @return single mode flag * @exception Exception if mode is not set yet */ abstract boolean getSingleModeFlag () throws Exception; /** * Sets the class flag, if class flag is set, * the cluster is listed as class atrribute in an extra attribute. * @param classFlag the new class flag */ public void setClassFlag(boolean classFlag) { m_ClassFlag = classFlag; } /** * Gets the class flag. * @return the class flag */ public boolean getClassFlag() { boolean b = m_ClassFlag; return m_ClassFlag; } /** * Sets the debug flag. * @param debug the new debug flag */ public void setDebug(boolean debug) { m_Debug = debug; } /** * Gets the debug flag. * @return the debug flag */ public boolean getDebug() { return m_Debug; } /** * Sets the relation name the dataset should have. * @param relationName the new relation name */ public void setRelationName(String relationName) { if (relationName.length() == 0) { // build relationname StringBuffer name = new StringBuffer(this.getClass().getName()); String [] options = getGenericOptions(); for (int i = 0; i < options.length; i++) { name = name.append(options[i].trim()); } if (this instanceof OptionHandler) { options = ((OptionHandler)this).getOptions(); for (int i = 0; i < options.length; i++) { name = name.append(options[i].trim()); } } m_RelationName = name.toString(); } else m_RelationName = relationName; } /** * Gets the relation name the dataset should have. * @return the relation name the dataset should have */ public String getRelationName() { return m_RelationName; } /** * Sets the number of clusters the dataset should have. * @param numClusters the new number of clusters */ public void setNumClusters(int numClusters) { m_NumClusters = numClusters; } /** * Gets the number of clusters the dataset should have. * @return the number of clusters the dataset should have */ public int getNumClusters() { return m_NumClusters; } /** * Sets the number of attributes the dataset should have. * @param numAttributes the new number of attributes */ public void setNumAttributes(int numAttributes) { m_NumAttributes = numAttributes; } /** * Gets the number of attributes that should be produced. * @return the number of attributes that should be produced */ public int getNumAttributes() { return m_NumAttributes; } /** * Sets the number of examples the dataset should have. * @param numExamplesAct the new number of examples */ public void setNumExamplesAct(int numExamplesAct) { m_NumExamplesAct = numExamplesAct; } /** * Gets the number of examples the dataset should have.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -