📄 randomrbf.java
字号:
* @return the default number of classes */ protected int defaultNumClasses() { return 2; } /** * Sets the number of classes the dataset should have. * @param numClasses the new number of classes */ public void setNumClasses(int numClasses) { m_NumClasses = numClasses; } /** * Gets the number of classes the dataset should have. * @return the number of classes the dataset should have */ public int getNumClasses() { return m_NumClasses; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String numClassesTipText() { return "The number of classes to generate."; } /** * returns the default number of centroids * * @return the default number of centroids */ protected int defaultNumCentroids() { return 50; } /** * Gets the number of centroids. * * @return the number of centroids. */ public int getNumCentroids() { return m_NumCentroids; } /** * Sets the number of centroids to use. * * @param value the number of centroids to use. */ public void setNumCentroids(int value) { if (value > 0) m_NumCentroids = value; else System.out.println("At least 1 centroid is necessary (provided: " + value + ")!"); } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String numCentroidsTipText() { return "The number of centroids to use."; } /** * Return if single mode is set for the given data generator * mode depends on option setting and or generator type. * * @return single mode flag * @throws Exception if mode is not set yet */ public boolean getSingleModeFlag() throws Exception { return true; } /** * returns a random index based on the given proportions * * @param proportionArray the proportions * @param random the random number generator to use * @return the random index */ protected int chooseRandomIndexBasedOnProportions( double[] proportionArray, Random random) { double probSum; double val; int index; double sum; probSum = Utils.sum(proportionArray); val = random.nextDouble() * probSum; index = 0; sum = 0.0; while ((sum <= val) && (index < proportionArray.length)) sum += proportionArray[index++]; return index - 1; } /** * Initializes the format for the dataset produced. * Must be called before the generateExample or generateExamples * methods are used. * Re-initializes the random number generator with the given seed. * * @return the format for the dataset * @throws Exception if the generating of the format failed * @see #getSeed() */ public Instances defineDataFormat() throws Exception { int i; int j; FastVector atts; FastVector clsValues; Random rand; m_Random = new Random(getSeed()); rand = getRandom(); // number of examples is the same as given per option setNumExamplesAct(getNumExamples()); // initialize centroids m_centroids = new double[getNumCentroids()][getNumAttributes()]; m_centroidClasses = new int[getNumCentroids()]; m_centroidWeights = new double[getNumCentroids()]; m_centroidStdDevs = new double[getNumCentroids()]; for (i = 0; i < getNumCentroids(); i++) { for (j = 0; j < getNumAttributes(); j++) m_centroids[i][j] = rand.nextDouble(); m_centroidClasses[i] = rand.nextInt(getNumClasses()); m_centroidWeights[i] = rand.nextDouble(); m_centroidStdDevs[i] = rand.nextDouble(); } // initialize dataset format atts = new FastVector(); for (i = 0; i < getNumAttributes(); i++) atts.addElement(new Attribute("a" + i)); clsValues = new FastVector(); for (i = 0; i < getNumClasses(); i++) clsValues.addElement("c" + i); atts.addElement(new Attribute("class", clsValues)); m_DatasetFormat = new Instances(getRelationNameToUse(), atts, 0); return m_DatasetFormat; } /** * Generates one example of the dataset. * * @return the generated example * @throws Exception if the format of the dataset is not yet defined * @throws Exception if the generator only works with generateExamples * which means in non single mode */ public Instance generateExample() throws Exception { Instance result; int centroid; double[] atts; double magnitude; double desiredMag; double scale; int i; double label; Random rand; result = null; rand = getRandom(); if (m_DatasetFormat == null) throw new Exception("Dataset format not defined."); // generate class label based on class probs centroid = chooseRandomIndexBasedOnProportions(m_centroidWeights, rand); label = m_centroidClasses[centroid]; // generate attributes atts = new double[getNumAttributes() + 1]; for (i = 0; i < getNumAttributes(); i++) atts[i] = (rand.nextDouble() * 2.0) - 1.0; atts[atts.length - 1] = label; magnitude = 0.0; for (i = 0; i < getNumAttributes(); i++) magnitude += atts[i] * atts[i]; magnitude = Math.sqrt(magnitude); desiredMag = rand.nextGaussian() * m_centroidStdDevs[centroid]; scale = desiredMag / magnitude; for (i = 0; i < getNumAttributes(); i++) { atts[i] *= scale; atts[i] += m_centroids[centroid][i]; result = new Instance(1.0, atts); } // dataset reference result.setDataset(m_DatasetFormat); return result; } /** * Generates all examples of the dataset. Re-initializes the random number * generator with the given seed, before generating instances. * * @return the generated dataset * @throws Exception if the format of the dataset is not yet defined * @throws Exception if the generator only works with generateExample, * which means in single mode * @see #getSeed() */ public Instances generateExamples() throws Exception { Instances result; int i; result = new Instances(m_DatasetFormat, 0); m_Random = new Random(getSeed()); for (i = 0; i < getNumExamplesAct(); i++) result.add(generateExample()); return result; } /** * 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 */ public String generateStart () { StringBuffer result; int i; result = new StringBuffer(); result.append("%\n"); result.append("% centroids:\n"); for (i = 0; i < getNumCentroids(); i++) result.append( "% " + i + ".: " + Utils.arrayToString(m_centroids[i]) + "\n"); result.append("%\n"); result.append( "% centroidClasses: " + Utils.arrayToString(m_centroidClasses) + "\n"); result.append("%\n"); result.append( "% centroidWeights: " + Utils.arrayToString(m_centroidWeights) + "\n"); result.append("%\n"); result.append( "% centroidStdDevs: " + Utils.arrayToString(m_centroidStdDevs) + "\n"); result.append("%\n"); return result.toString(); } /** * Generates a comment string that documentats the data generator. * By default this string is added at the end of theproduces output * as ARFF file type. * * @return string contains info about the generated rules * @throws Exception if the generating of the documentaion fails */ public String generateFinished() throws Exception { return ""; } /** * Main method for executing this class. * * @param args should contain arguments for the data producer: */ public static void main(String[] args) { runDataGenerator(new RandomRBF(), args); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -