📄 scalebymean.java
字号:
package chen.macroweka.filters.unsupervised.attribute;
import weka.filters.UnsupervisedFilter;
import weka.filters.Filter;
import weka.core.*;
public class ScaleByMean extends Filter implements UnsupervisedFilter
{
/**
* Sets the format of the input instances.
*
* @param instanceInfo an Instances object containing the input instance
* structure (any instances contained in the object are ignored - only the
* structure is required).
* @return true if the outputFormat may be collected immediately
* @throws Exception if the format couldn't be set successfully
*/
public boolean setInputFormat(Instances instanceInfo) throws Exception
{
super.setInputFormat( instanceInfo );
setOutputFormat( instanceInfo );
return true;
}
/**
* 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 inputFormat has been
* re-assigned or new options have been set). This default
* implementation assumes all instance processing occurs during
* inputFormat() and input().
*
* @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.
*/
public boolean batchFinished() throws Exception
{
if (getInputFormat() == null) {
throw new IllegalStateException("No input instance format defined");
}
for (int j=0; j<getInputFormat().numInstances(); ++j) {
Instance instance = getInputFormat().instance( j );
double [] values = new double [getInputFormat().numAttributes()];
values = instance.toDoubleArray();
for (int i=0; i<getInputFormat().numAttributes(); ++i) {
AttributeStats as = getInputFormat().attributeStats( i );
if (as.numericStats != null) {
values[i] = values[i] - as.numericStats.mean;
}
}
Instance inst = new Instance( instance.weight(), values );
inst.setDataset( instance.dataset() );
push(inst);
}
m_NewBatch = true;
return (numPendingOutput() != 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) {
try {
if (Utils.getFlag('b', argv)) {
Filter.batchFilterFile(new ScaleByMean(), argv);
} else {
Filter.filterFile(new ScaleByMean(), argv);
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -