public abstract class SimpleBatchFilter extends SimpleFilter
true.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:
public static void main(String[] args) {
runFilter(new <Filtername>(), args);
}
Example implementation:
import weka.core.*;
import weka.core.Capabilities.*;
import weka.filters.*;
public class SimpleBatch extends SimpleBatchFilter {
public String globalInfo() {
return "A simple batch filter that adds an additional attribute 'bla' at the end containing the index of the processed instance.";
}
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 Instances process(Instances inst) {
Instances result = new Instances(determineOutputFormat(inst), 0);
for (int i = 0; i < inst.numInstances(); i++) {
double[] values = new double[result.numAttributes()];
for (int n = 0; n < inst.numAttributes(); n++)
values[n] = inst.instance(i).value(n);
values[values.length - 1] = i;
result.add(new DenseInstance(1, values));
}
return result;
}
public static void main(String[] args) {
runFilter(new SimpleBatch(), args);
}
}
Options:m_Debug, m_DoNotCheckCapabilities, m_FirstBatchDone, m_InputRelAtts, m_InputStringAtts, m_NewBatch, m_OutputRelAtts, m_OutputStringAtts| Constructor and Description |
|---|
SimpleBatchFilter() |
| Modifier and Type | Method and Description |
|---|---|
boolean |
allowAccessToFullInputFormat()
Returns whether to allow the determineOutputFormat(Instances) method access
to the full dataset rather than just the header.
|
boolean |
batchFinished()
Signify that this batch of input to the filter is finished.
|
protected boolean |
hasImmediateOutputFormat()
returns true if the output format is immediately available after the input
format has been set and not only after all the data has been seen (see
batchFinished())
|
boolean |
input(Instance instance)
Input an instance for filtering.
|
determineOutputFormat, globalInfo, process, reset, setInputFormatbatchFilterFile, bufferInput, copyValues, copyValues, debugTipText, doNotCheckCapabilitiesTipText, filterFile, flushInput, getCapabilities, getCapabilities, getCopyOfInputFormat, getDebug, getDoNotCheckCapabilities, getInputFormat, getOptions, getOutputFormat, getRevision, initInputLocators, initOutputLocators, inputFormatPeek, isFirstBatchDone, isNewBatch, isOutputFormatDefined, listOptions, main, makeCopies, makeCopy, mayRemoveInstanceAfterFirstBatchDone, numPendingOutput, output, outputFormatPeek, outputPeek, postExecution, preExecution, push, push, resetQueue, run, runFilter, setDebug, setDoNotCheckCapabilities, setOptions, setOutputFormat, testInputFormat, toString, useFilter, wekaStaticWrapperprotected boolean hasImmediateOutputFormat()
hasImmediateOutputFormat in class SimpleFilterbatchFinished(),
SimpleFilter.setInputFormat(Instances)public boolean allowAccessToFullInputFormat()
public boolean input(Instance instance) throws java.lang.Exception
input in class Filterinstance - the input instancejava.lang.IllegalStateException - if no input structure has been definedjava.lang.Exception - if something goes wrongbatchFinished()public boolean batchFinished()
throws java.lang.Exception
batchFinished in class Filterjava.lang.IllegalStateException - if no input format has been set.java.lang.Exception - if something goes wrongFilter.m_NewBatch,
Filter.m_FirstBatchDone