/** Creates an example StringToNominal */ public Filter getFilter() { StringToNominal f = new StringToNominal(); f.setAttributeRange("1"); return f; }
/** * Signifies 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. * * @return true if there are instances pending output. * @throws IllegalStateException if no input structure has been defined. */ @Override public boolean batchFinished() { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (!isOutputFormatDefined()) { setOutputFormat(); // Convert pending input instances for (int i = 0; i < getInputFormat().numInstances(); i++) { push((Instance) getInputFormat().instance(i).copy(), false); // No need to copy } } flushInput(); m_NewBatch = true; return (numPendingOutput() != 0); }
/** * 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("-R"); options.add("" + (getAttributeRange())); if (this.m_AttIndices.getInvert()) { options.add("-V"); } return options.toArray(new String[0]); }
/** * 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 StringToNominal(), argv); } }
public boolean input(Instance instance) { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); resetQueue(); m_NewBatch = false; if (isOutputFormatDefined()) { Instance newInstance = (Instance) instance.copy(); if (newInstance.attribute(i).isString() && !newInstance.isMissing(i) && m_AttIndices.isInRange(i)) { Attribute outAtt = outputFormatPeek().attribute(i); String inVal = newInstance.stringValue(i); int outIndex = outAtt.indexOfValue(inVal); push(newInstance, false); // No need to copy return true; bufferInput(instance); return false;
setAttributeRange(attIndices); } else { setAttributeRange("last"); if (getInputFormat() != null) { setInputFormat(getInputFormat());
/** * tests the filter in conjunction with the FilteredClassifier */ public void testFilteredClassifier() { try { Instances data = getFilteredClassifierData(); for (int i = 0; i < data.numAttributes(); i++) { if (data.classIndex() == i) continue; if (data.attribute(i).isString()) { ((StringToNominal) m_FilteredClassifier.getFilter()).setAttributeRange( "" + (i + 1)); break; } } } catch (Exception e) { fail("Problem setting up test for FilteredClassifier: " + e.toString()); } super.testFilteredClassifier(); }
String[] options = new String[2]; options[0] = "-R"; // "range" options[1] = "1"; // first attribute weka.filters.unsupervised.attribute.StringToNominal ff=new weka.filters.unsupervised.attribute.StringToNominal(); // new instance of filter ff.setOptions(options); // set options ff.setInputFormat(data1); // inform filter about dataset **AFTER** setting options Instances data2 = Filter.useFilter(data1, ff);
newAtts = new ArrayList<Attribute>(getInputFormat().numAttributes()); for (int j = 0; j < getInputFormat().numAttributes(); j++) { Attribute att = getInputFormat().attribute(j); if (!m_AttIndices.isInRange(j) || !att.isString()) { newData = new Instances(getInputFormat().relationName(), newAtts, 0); newData.setClassIndex(getInputFormat().classIndex()); setOutputFormat(newData);
public boolean input(Instance instance) { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); resetQueue(); m_NewBatch = false; if (isOutputFormatDefined()) { Instance newInstance = (Instance) instance.copy(); if (newInstance.attribute(i).isString() && !newInstance.isMissing(i) && m_AttIndices.isInRange(i)) { Attribute outAtt = outputFormatPeek().attribute(i); String inVal = newInstance.stringValue(i); int outIndex = outAtt.indexOfValue(inVal); push(newInstance, false); // No need to copy return true; bufferInput(instance); return false;
setAttributeRange(attIndices); } else { setAttributeRange("last"); if (getInputFormat() != null) { setInputFormat(getInputFormat());
/** * 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 StringToNominal(), argv); } }
/** * tests the filter in conjunction with the FilteredClassifier */ public void testFilteredClassifier() { try { Instances data = getFilteredClassifierData(); for (int i = 0; i < data.numAttributes(); i++) { if (data.classIndex() == i) continue; if (data.attribute(i).isString()) { ((StringToNominal) m_FilteredClassifier.getFilter()).setAttributeRange( "" + (i + 1)); break; } } } catch (Exception e) { fail("Problem setting up test for FilteredClassifier: " + e.toString()); } super.testFilteredClassifier(); }
newAtts = new ArrayList<Attribute>(getInputFormat().numAttributes()); for (int j = 0; j < getInputFormat().numAttributes(); j++) { Attribute att = getInputFormat().attribute(j); if (!m_AttIndices.isInRange(j) || !att.isString()) { newData = new Instances(getInputFormat().relationName(), newAtts, 0); newData.setClassIndex(getInputFormat().classIndex()); setOutputFormat(newData);
/** * Signifies 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. * * @return true if there are instances pending output. * @throws IllegalStateException if no input structure has been defined. */ @Override public boolean batchFinished() { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (!isOutputFormatDefined()) { setOutputFormat(); // Convert pending input instances for (int i = 0; i < getInputFormat().numInstances(); i++) { push((Instance) getInputFormat().instance(i).copy(), false); // No need to copy } } flushInput(); m_NewBatch = true; return (numPendingOutput() != 0); }
/** Creates an example StringToNominal */ public Filter getFilter() { StringToNominal f = new StringToNominal(); f.setAttributeRange("1"); return f; }
public void testMissing() { ((StringToNominal)m_Filter).setAttributeRange("4"); Instances result = useFilter(); // Number of attributes and instances shouldn't change assertEquals(m_Instances.numAttributes(), result.numAttributes()); assertEquals(m_Instances.numInstances(), result.numInstances()); assertEquals("Attribute type should now be NOMINAL", Attribute.NOMINAL, result.attribute(3).type()); assertEquals(8, result.attribute(3).numValues()); for (int i = 0; i < result.numInstances(); i++) { assertTrue("Missing values should be preserved", m_Instances.instance(i).isMissing(3) == result.instance(i).isMissing(3)); } }
/** * 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("-R"); options.add("" + (getAttributeRange())); if (this.m_AttIndices.getInvert()) { options.add("-V"); } return options.toArray(new String[0]); }
public void testMissing() { ((StringToNominal)m_Filter).setAttributeRange("4"); Instances result = useFilter(); // Number of attributes and instances shouldn't change assertEquals(m_Instances.numAttributes(), result.numAttributes()); assertEquals(m_Instances.numInstances(), result.numInstances()); assertEquals("Attribute type should now be NOMINAL", Attribute.NOMINAL, result.attribute(3).type()); assertEquals(8, result.attribute(3).numValues()); for (int i = 0; i < result.numInstances(); i++) { assertTrue("Missing values should be preserved", m_Instances.instance(i).isMissing(3) == result.instance(i).isMissing(3)); } }
public void testRange() { ((StringToNominal)m_Filter).setAttributeRange("first-last"); Instances result = useFilter();