/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
/*
* NumericTransform.java
* Copyright (C) 1999-2012 University of Waikato, Hamilton, New Zealand
*
*/
package weka.filters.unsupervised.attribute;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Vector;
import weka.core.*;
import weka.core.Capabilities.Capability;
import weka.filters.Filter;
import weka.filters.StreamableFilter;
import weka.filters.UnsupervisedFilter;
/**
* Transforms numeric attributes using a given
* transformation method.
*
*
*
* Valid options are:
*
*
*
* -R <index1,index2-index4,...>
* Specify list of columns to transform. First and last are
* valid indexes (default none). Non-numeric columns are
* skipped.
*
*
*
* -V
* Invert matching sense.
*
*
*
* -C <string>
* Sets the class containing transformation method.
* (default java.lang.Math)
*
*
*
* -M <string>
* Sets the method. (default abs)
*
*
*
*
* @author Eibe Frank (eibe@cs.waikato.ac.nz)
* @version $Revision: 14508 $
*/
public class NumericTransform extends Filter implements UnsupervisedFilter,
StreamableFilter, OptionHandler, WeightedInstancesHandler, WeightedAttributesHandler {
/** for serialization */
static final long serialVersionUID = -8561413333351366934L;
/** Stores which columns to transform. */
private final Range m_Cols = new Range();
/** Class containing transformation method. */
private String m_Class;
/** Transformation method. */
private String m_Method;
/**
* Returns a string describing this filter
*
* @return a description of the filter suitable for displaying in the
* explorer/experimenter gui
*/
public String globalInfo() {
return "Transforms numeric attributes using a given transformation method.";
}
/**
* Default constructor -- sets the default transform method to
* java.lang.Math.abs().
*/
public NumericTransform() {
m_Class = "java.lang.Math";
m_Method = "abs";
}
/**
* Returns the Capabilities of this filter.
*
* @return the capabilities of this object
* @see Capabilities
*/
@Override
public Capabilities getCapabilities() {
Capabilities result = super.getCapabilities();
result.disableAll();
// attributes
result.enableAllAttributes();
result.enable(Capability.MISSING_VALUES);
// class
result.enableAllClasses();
result.enable(Capability.MISSING_CLASS_VALUES);
result.enable(Capability.NO_CLASS);
return result;
}
/**
* 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 input format can't be set successfully
*/
@Override
public boolean setInputFormat(Instances instanceInfo) throws Exception {
if (m_Class == null) {
throw new IllegalStateException("No class has been set.");
}
if (m_Method == null) {
throw new IllegalStateException("No method has been set.");
}
super.setInputFormat(instanceInfo);
m_Cols.setUpper(instanceInfo.numAttributes() - 1);
setOutputFormat(instanceInfo);
return true;
}
/**
* Input an instance for filtering. The instance is processed and made
* available for output immediately.
*
* @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.
* @throws InvocationTargetException if there is a problem applying the
* configured transform method.
*/
@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;
}
Method m = (Class.forName(m_Class)).getMethod(m_Method,
new Class[] { Double.TYPE });
double[] vals = new double[instance.numAttributes()];
Double[] params = new Double[1];
Double newVal;
for (int i = 0; i < instance.numAttributes(); i++) {
if (instance.isMissing(i)) {
vals[i] = Utils.missingValue();
} else {
if (m_Cols.isInRange(i) && instance.attribute(i).isNumeric()) {
params[0] = new Double(instance.value(i));
newVal = (Double) m.invoke(null, (Object[]) params);
if (newVal.isNaN() || newVal.isInfinite()) {
vals[i] = Utils.missingValue();
} else {
vals[i] = newVal.doubleValue();
}
} else {
vals[i] = instance.value(i);
}
}
}
Instance inst = null;
if (instance instanceof SparseInstance) {
inst = new SparseInstance(instance.weight(), vals);
} else {
inst = new DenseInstance(instance.weight(), vals);
}
inst.setDataset(instance.dataset());
push(inst, false); // No need to copy
return true;
}
/**
* Returns an enumeration describing the available options.
*
* @return an enumeration of all the available options.
*/
@Override
public Enumeration
Ads help maintain this website.