SimpleStreamFilter (original) (raw)
This filter is a superclass for simple stream filters.
General notes:
- After the first call of batchFinished() the field m_FirstBatchDone is set to
true
.
Example:
The following code snippet uses the filter SomeFilter
on a dataset that is loaded from filename
.
import weka.core.; import weka.filters.; import java.io.*; ... SomeFilter filter = new SomeFilter(); // set necessary options for the filter Instances data = new Instances( new BufferedReader( new FileReader(filename))); Instances filteredData = Filter.useFilter(data, filter);
Implementation:
Only the following abstract methods need to be implemented:
- globalInfo()
- determineOutputFormat(Instances)
- process(Instance)
And the getCapabilities() method must return what kind of attributes and classes the filter can handle.
If more options are necessary, then the following methods need to be overriden:
- listOptions()
- setOptions(String[])
- getOptions()
To make the filter available from commandline one must add the following main method for correct execution ( must be replaced with the actual filter classname):
public static void main(String[] args) { runFilter(new (), args); }
Example implementation:
import weka.core.; import weka.core.Capabilities.; import weka.filters.*;
import java.util.Random;
public class SimpleStream extends SimpleStreamFilter {
public String globalInfo() { return "A simple stream filter that adds an attribute 'bla' at the end containing a random number."; }
public Capabilities getCapabilities() { Capabilities result = super.getCapabilities(); result.enableAllAttributes(); result.enableAllClasses(); result.enable(Capability.NO_CLASS); // filter doesn't need class to be set return result; }
protected Instances determineOutputFormat(Instances inputFormat) { Instances result = new Instances(inputFormat, 0); result.insertAttributeAt(new Attribute("bla"), result.numAttributes()); return result; }
protected Instance process(Instance inst) { double[] values = new double[inst.numAttributes() + 1]; for (int n = 0; n < inst.numAttributes(); n++) values[n] = inst.value(n); values[values.length - 1] = new Random().nextInt(); Instance result = new Instance(1, values); return result; }
public static void main(String[] args) { runFilter(new SimpleStream(), args); } }
Options:
Valid filter-specific options are:
-D
Turns on output of debugging information.