/** * Parses a given list of options. * <p/> * * <!-- options-start --> Valid options are: * <p/> * * <pre> * -S <num> * Specify the random number seed (default 42) * </pre> * * <!-- options-end --> * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ @Override public void setOptions(String[] options) throws Exception { String seedString = Utils.getOption('S', options); if (seedString.length() != 0) { setRandomSeed(Integer.parseInt(seedString)); } else { setRandomSeed(42); } if (getInputFormat() != null) { setInputFormat(getInputFormat()); } }
/** * Input an instance for filtering. Filter requires all training instances be * read before producing output. * * @param instance the input instance * @return true if the filtered instance may now be collected with output(). * @throws IllegalStateException if no input structure has been defined */ @Override public boolean input(Instance instance) { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (m_NewBatch) { resetQueue(); m_NewBatch = false; } if (isFirstBatchDone()) { push(instance); return true; } else { bufferInput(instance); return false; } }
/** * Split the dataset into p% train an (100-p)% test set * * @param data Input data * @param p train percentage * @return Array of instances: (0) Train, (1) Test * @throws Exception Filterapplication went wrong */ public static Instances[] splitTrainVal(Instances data, double p) throws Exception { // Randomize data Randomize rand = new Randomize(); rand.setInputFormat(data); rand.setRandomSeed(42); data = Filter.useFilter(data, rand); // Remove testpercentage from data to get the train set RemovePercentage rp = new RemovePercentage(); rp.setInputFormat(data); rp.setPercentage(p); Instances train = Filter.useFilter(data, rp); // Remove trainpercentage from data to get the test set rp = new RemovePercentage(); rp.setInputFormat(data); rp.setPercentage(p); rp.setInvertSelection(true); Instances test = Filter.useFilter(data, rp); return new Instances[]{train, test}; }
/** * Signify that this batch of input to the filter is finished. If the filter * requires all instances prior to filtering, output() may now be called to * retrieve the filtered instances. Any subsequent instances filtered should * be filtered based on setting obtained from the first batch (unless the * setInputFormat has been re-assigned or new options have been set). This * implementation randomizes all the instances received in the batch. * * @return true if there are instances pending output * @throws IllegalStateException if no input format has been set. */ @Override public boolean batchFinished() { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (!isFirstBatchDone()) { getInputFormat().randomize(m_Random); } for (int i = 0; i < getInputFormat().numInstances(); i++) { push(getInputFormat().instance(i), false); // No need to copy because of bufferInput() } flushInput(); m_NewBatch = true; m_FirstBatchDone = true; return (numPendingOutput() != 0); }
/** Creates a default Randomize */ public Filter getFilter() { return new Randomize(); }
@ProgrammaticProperty public void setSeed(int seed) { setRandomSeed(seed); }
/** * Gets the current settings of the filter. * * @return an array of strings suitable for passing to setOptions */ @Override public String[] getOptions() { Vector<String> options = new Vector<String>(); options.add("-S"); options.add("" + getRandomSeed()); return options.toArray(new String[0]); }
/** * Split the dataset into p% train and (100-p)% testImdb set * * @param data Input data * @param p train percentage * @return Array of instances: (0) Train, (1) Test * @throws Exception Filterapplication went wrong */ public static Instances[] splitTrainTest(Instances data, double p) throws Exception { Randomize rand = new Randomize(); rand.setInputFormat(data); rand.setRandomSeed(42); data = Filter.useFilter(data, rand); RemovePercentage rp = new RemovePercentage(); rp.setInputFormat(data); rp.setPercentage(p); rp.setInvertSelection(true); Instances train = Filter.useFilter(data, rp); rp = new RemovePercentage(); rp.setInputFormat(data); rp.setPercentage(p); Instances test = Filter.useFilter(data, rp); return new Instances[] {train, test}; }
/** * Signify that this batch of input to the filter is finished. If the filter * requires all instances prior to filtering, output() may now be called to * retrieve the filtered instances. Any subsequent instances filtered should * be filtered based on setting obtained from the first batch (unless the * setInputFormat has been re-assigned or new options have been set). This * implementation randomizes all the instances received in the batch. * * @return true if there are instances pending output * @throws IllegalStateException if no input format has been set. */ @Override public boolean batchFinished() { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (!isFirstBatchDone()) { getInputFormat().randomize(m_Random); } for (int i = 0; i < getInputFormat().numInstances(); i++) { push(getInputFormat().instance(i), false); // No need to copy because of bufferInput() } flushInput(); m_NewBatch = true; m_FirstBatchDone = true; return (numPendingOutput() != 0); }
/** Creates a default Randomize */ public Filter getFilter() { return new Randomize(); }
@ProgrammaticProperty public void setSeed(int seed) { setRandomSeed(seed); }
/** * Gets the current settings of the filter. * * @return an array of strings suitable for passing to setOptions */ @Override public String[] getOptions() { Vector<String> options = new Vector<String>(); options.add("-S"); options.add("" + getRandomSeed()); return options.toArray(new String[0]); }
/** * Input an instance for filtering. Filter requires all training instances be * read before producing output. * * @param instance the input instance * @return true if the filtered instance may now be collected with output(). * @throws IllegalStateException if no input structure has been defined */ @Override public boolean input(Instance instance) { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (m_NewBatch) { resetQueue(); m_NewBatch = false; } if (isFirstBatchDone()) { push(instance); return true; } else { bufferInput(instance); return false; } }
/** * Parses a given list of options. * <p/> * * <!-- options-start --> Valid options are: * <p/> * * <pre> * -S <num> * Specify the random number seed (default 42) * </pre> * * <!-- options-end --> * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ @Override public void setOptions(String[] options) throws Exception { String seedString = Utils.getOption('S', options); if (seedString.length() != 0) { setRandomSeed(Integer.parseInt(seedString)); } else { setRandomSeed(42); } if (getInputFormat() != null) { setInputFormat(getInputFormat()); } }
@ProgrammaticProperty public int getSeed() { return getRandomSeed(); }
@ProgrammaticProperty public int getSeed() { return getRandomSeed(); }