/** Creates a RandomProjection with the number of attributes */ protected Filter getFilter(int numAtts) { RandomProjection f = new RandomProjection(); f.setNumberOfAttributes(numAtts); return f; }
/** * 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>(); if (getReplaceMissingValues()) { options.add("-M"); } if (getPercent() <= 0) { options.add("-N"); options.add("" + getNumberOfAttributes()); } else { options.add("-P"); options.add("" + getPercent()); } options.add("-R"); options.add("" + getSeed()); SelectedTag t = getDistribution(); options.add("-D"); options.add("" + t.getSelectedTag().getReadable()); return options.toArray(new String[0]); }
/** * Input an instance for filtering. * * @param instance the input instance * @return true if the filtered instance may now be collected with output(). * @throws IllegalStateException if no input format has been set */ @Override public boolean input(Instance instance) throws Exception { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (m_NewBatch) { resetQueue(); m_NewBatch = false; } if ((m_OutputFormatDefined) && ((!m_useReplaceMissing) || isFirstBatchDone())) { if (m_replaceMissing != null) { m_replaceMissing.input(instance); instance = m_replaceMissing.output(); } m_ntob.input(instance); instance = m_ntob.output(); push(convertInstance(instance), false); return true; } bufferInput(instance); return false; }
/** * Main method for testing this class. * * @param argv should contain arguments to the filter: use -h for help */ public static void main(String[] argv) { runFilter(new RandomProjection(), argv); } }
/** * Signify that this batch of input to the filter is finished. * * @return true if there are instances pending output * @throws NullPointerException if no input structure has been defined, * @throws Exception if there was a problem finishing the batch. */ @Override public boolean batchFinished() throws Exception { if (getInputFormat() == null) { throw new NullPointerException("No input instance format defined"); } Instances insts = getInputFormat(); if (m_useReplaceMissing) { insts = Filter.useFilter(insts, m_replaceMissing); } insts = Filter.useFilter(insts, m_ntob); if (!m_OutputFormatDefined) { setOutputFormat(); } for (Instance instance : insts) { push(convertInstance(instance), false); // No need to copy } flushInput(); m_NewBatch = true; m_FirstBatchDone = true; return (numPendingOutput() != 0); }
setPercent(Double.parseDouble(mString)); } else { setPercent(0); mString = Utils.getOption('N', options); if (mString.length() != 0) { setNumberOfAttributes(Integer.parseInt(mString)); } else { setNumberOfAttributes(10); setSeed(Integer.parseInt(mString)); if (mString.length() != 0) { if (mString.equalsIgnoreCase("sparse1")) { setDistribution(new SelectedTag(SPARSE1, TAGS_DSTRS_TYPE)); } else if (mString.equalsIgnoreCase("sparse2")) { setDistribution(new SelectedTag(SPARSE2, TAGS_DSTRS_TYPE)); } else if (mString.equalsIgnoreCase("gaussian")) { setDistribution(new SelectedTag(GAUSSIAN, TAGS_DSTRS_TYPE)); setReplaceMissingValues(true); } else { setReplaceMissingValues(false);
/** Creates a default RandomProjection */ public Filter getFilter() { return getFilter(new RandomProjection().getNumberOfAttributes()); }
m_k = (int) ((getInputFormat().numAttributes() - 1) * m_percent); attributes.add(new Attribute("K" + (i + 1))); if (getInputFormat().classIndex() > -1) { attributes.add((Attribute) getInputFormat().classAttribute().copy()); if (getInputFormat().classIndex() > -1) { newFormat.setClassIndex(attributes.size() - 1); for (int i = 0; i < m_rmatrix.length; i++) { for (int j = 0; j < m_rmatrix[i].length; j++) { m_rmatrix[i][j] = rndmNum(useDstrWithZero); setOutputFormat(newFormat);
/** * Default constructor. */ public RandomizableFilteredClassifier() { m_Classifier = new weka.classifiers.lazy.IBk(); m_Filter = new weka.filters.unsupervised.attribute.RandomProjection(); }
/** * performs some checks on the given result * * @param result the instances to compare against original dataset */ protected void checkResult(Instances result) { assertEquals( ((RandomProjection) m_Filter).getNumberOfAttributes() + 1, result.numAttributes()); assertEquals(m_Instances.numInstances(), result.numInstances()); }
/** * tests the given distribution type * * @param type the distribution type to use * @see RandomProjection#TAGS_DSTRS_TYPE */ protected void checkDistributionType(int type) { m_Filter = getFilter(); ((RandomProjection) m_Filter).setDistribution( new SelectedTag(type, RandomProjection.TAGS_DSTRS_TYPE)); Instances result = useFilter(); checkResult(result); }
setPercent(Double.parseDouble(mString)); } else { setPercent(0); mString = Utils.getOption('N', options); if (mString.length() != 0) { setNumberOfAttributes(Integer.parseInt(mString)); } else { setNumberOfAttributes(10); setSeed(Integer.parseInt(mString)); if (mString.length() != 0) { if (mString.equalsIgnoreCase("sparse1")) { setDistribution(new SelectedTag(SPARSE1, TAGS_DSTRS_TYPE)); } else if (mString.equalsIgnoreCase("sparse2")) { setDistribution(new SelectedTag(SPARSE2, TAGS_DSTRS_TYPE)); } else if (mString.equalsIgnoreCase("gaussian")) { setDistribution(new SelectedTag(GAUSSIAN, TAGS_DSTRS_TYPE)); setReplaceMissingValues(true); } else { setReplaceMissingValues(false);
/** * Signify that this batch of input to the filter is finished. * * @return true if there are instances pending output * @throws NullPointerException if no input structure has been defined, * @throws Exception if there was a problem finishing the batch. */ @Override public boolean batchFinished() throws Exception { if (getInputFormat() == null) { throw new NullPointerException("No input instance format defined"); } Instances insts = getInputFormat(); if (m_useReplaceMissing) { insts = Filter.useFilter(insts, m_replaceMissing); } insts = Filter.useFilter(insts, m_ntob); if (!m_OutputFormatDefined) { setOutputFormat(); } for (Instance instance : insts) { push(convertInstance(instance), false); // No need to copy } flushInput(); m_NewBatch = true; m_FirstBatchDone = true; return (numPendingOutput() != 0); }
/** Creates a default RandomProjection */ public Filter getFilter() { return getFilter(new RandomProjection().getNumberOfAttributes()); }
/** * Main method for testing this class. * * @param argv should contain arguments to the filter: use -h for help */ public static void main(String[] argv) { runFilter(new RandomProjection(), argv); } }
m_k = (int) ((getInputFormat().numAttributes() - 1) * m_percent); attributes.add(new Attribute("K" + (i + 1))); if (getInputFormat().classIndex() > -1) { attributes.add((Attribute) getInputFormat().classAttribute().copy()); if (getInputFormat().classIndex() > -1) { newFormat.setClassIndex(attributes.size() - 1); for (int i = 0; i < m_rmatrix.length; i++) { for (int j = 0; j < m_rmatrix[i].length; j++) { m_rmatrix[i][j] = rndmNum(useDstrWithZero); setOutputFormat(newFormat);
/** * Default constructor. */ public RandomizableFilteredClassifier() { m_Classifier = new weka.classifiers.lazy.IBk(); m_Filter = new weka.filters.unsupervised.attribute.RandomProjection(); }
/** * performs some checks on the given result * * @param result the instances to compare against original dataset */ protected void checkResult(Instances result) { assertEquals( ((RandomProjection) m_Filter).getNumberOfAttributes() + 1, result.numAttributes()); assertEquals(m_Instances.numInstances(), result.numInstances()); }
/** * tests the given distribution type * * @param type the distribution type to use * @see RandomProjection#TAGS_DSTRS_TYPE */ protected void checkDistributionType(int type) { m_Filter = getFilter(); ((RandomProjection) m_Filter).setDistribution( new SelectedTag(type, RandomProjection.TAGS_DSTRS_TYPE)); Instances result = useFilter(); checkResult(result); }
/** * 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>(); if (getReplaceMissingValues()) { options.add("-M"); } if (getPercent() <= 0) { options.add("-N"); options.add("" + getNumberOfAttributes()); } else { options.add("-P"); options.add("" + getPercent()); } options.add("-R"); options.add("" + getSeed()); SelectedTag t = getDistribution(); options.add("-D"); options.add("" + t.getSelectedTag().getReadable()); return options.toArray(new String[0]); }