[Home] DMelt member access. Java source code: weka.filters.unsupervised.attribute.NominalToBinary
/*
* 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 .
*/
/*
* NominalToBinary.java
* Copyright (C) 1999-2012 University of Waikato, Hamilton, New Zealand
*
*/
package weka.filters.unsupervised.attribute;
import java.util.ArrayList;
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;
/**
* Converts all nominal attributes into binary numeric
* attributes. An attribute with k values is transformed into k binary
* attributes if the class is nominal (using the one-attribute-per-value
* approach). Binary attributes are left binary if option '-A' is not given. If
* the class is numeric, you might want to use the supervised version of this
* filter.
*
*
*
* Valid options are:
*
*
*
* -N
* Sets if binary attributes are to be coded as nominal ones.
*
*
*
* -A
* For each nominal value a new attribute is created,
* not only if there are more than 2 values.
*
*
*
* -R <col1,col2-col4,...>
* Specifies list of columns to act on. First and last are
* valid indexes.
* (default: first-last)
*
*
*
* -V
* Invert matching sense of column indexes.
*
*
*
-spread-attribute-weight
* When generating binary attributes, spread weight of old
* attribute across new attributes. Do not give each new attribute the old weight.
*
*
*
* @author Eibe Frank (eibe@cs.waikato.ac.nz)
* @version $Revision: 14509 $
*/
public class NominalToBinary extends Filter implements UnsupervisedFilter,
OptionHandler, StreamableFilter, WeightedAttributesHandler, WeightedInstancesHandler {
/** for serialization */
static final long serialVersionUID = -1130642825710549138L;
/** Stores which columns to act on */
protected Range m_Columns = new Range();
/** Are the new attributes going to be nominal or numeric ones? */
private boolean m_Numeric = true;
/** Are all values transformed into new attributes? */
private boolean m_TransformAll = false;
/** Whether we need to transform at all */
private boolean m_needToTransform = false;
/** Whether to spread attribute weight when creating binary attributes */
protected boolean m_SpreadAttributeWeight = false;
/** Constructor - initialises the filter */
public NominalToBinary() {
setAttributeIndices("first-last");
}
/**
* Returns a string describing this filter
*
* @return a description of the filter suitable for displaying in the
* explorer/experimenter gui
*/
public String globalInfo() {
return "Converts all nominal attributes into binary numeric attributes. An "
+ "attribute with k values is transformed into k binary attributes if "
+ "the class is nominal (using the one-attribute-per-value approach). "
+ "Binary attributes are left binary if option '-A' is not given. "
+ "If the class is numeric, you might want to use the supervised version of "
+ "this filter.";
}
/**
* 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 {
super.setInputFormat(instanceInfo);
m_Columns.setUpper(instanceInfo.numAttributes() - 1);
setOutputFormat();
return true;
}
/**
* Input an instance for filtering. Filter requires all training instances be
* read before producing output.
*
* @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
*/
@Override
public boolean input(Instance instance) {
if (getInputFormat() == null) {
throw new IllegalStateException("No input instance format defined");
}
if (m_NewBatch) {
resetQueue();
m_NewBatch = false;
}
convertInstance(instance);
return true;
}
/**
* Returns an enumeration describing the available options.
*
* @return an enumeration of all the available options.
*/
@Override
public Enumeration