/*
 *   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 .
 */

/*
 *    MakeIndicator.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;

/**
 *  A filter that creates a new dataset with a Boolean
 * attribute replacing a nominal attribute. In the new dataset, a value of 1 is
 * assigned to an instance that exhibits a value in the given range of attribute values,
 * and a value of 0 is assigned to an instance that does not. The Boolean attribute is coded as numeric by
 * default.
 * 

* * * Valid options are: *

* *

 * -C <col>
 *  Sets the attribute index.
 * 
* *
 * -V <index1,index2-index4,...>
 *  Specify the list of values to indicate. First and last are
 *  valid indexes (default last)
 * 
* *
 * -N <index>
 *  Set if new Boolean attribute nominal.
 * 
* * * * @author Eibe Frank (eibe@cs.waikato.ac.nz) * @version $Revision: 14508 $ */ public class MakeIndicator extends Filter implements UnsupervisedFilter, StreamableFilter, OptionHandler, WeightedAttributesHandler, WeightedInstancesHandler { /** for serialization */ static final long serialVersionUID = 766001176862773163L; /** The attribute's index setting. */ private final SingleIndex m_AttIndex = new SingleIndex("last"); /** The value's index */ private final Range m_ValIndex; /** Make Boolean attribute numeric. */ private boolean m_Numeric = true; /** * Constructor */ public MakeIndicator() { m_ValIndex = new Range("last"); } /** * 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 UnsupportedAttributeTypeException the selecte attribute is not * nominal * @throws UnsupportedAttributeTypeException the selecte attribute has fewer * than two values. */ @Override public boolean setInputFormat(Instances instanceInfo) throws Exception { super.setInputFormat(instanceInfo); m_AttIndex.setUpper(instanceInfo.numAttributes() - 1); m_ValIndex.setUpper(instanceInfo.attribute(m_AttIndex.getIndex()) .numValues() - 1); if (!instanceInfo.attribute(m_AttIndex.getIndex()).isNominal()) { throw new UnsupportedAttributeTypeException( "Chosen attribute not nominal."); } if (instanceInfo.attribute(m_AttIndex.getIndex()).numValues() < 2) { throw new UnsupportedAttributeTypeException("Chosen attribute has less " + "than two values."); } setOutputFormat(); 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. */ @Override public boolean input(Instance instance) { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (m_NewBatch) { resetQueue(); m_NewBatch = false; } Instance newInstance = (Instance) instance.copy(); if (!newInstance.isMissing(m_AttIndex.getIndex())) { if (m_ValIndex.isInRange((int) newInstance.value(m_AttIndex.getIndex()))) { newInstance.setValue(m_AttIndex.getIndex(), 1); } else { newInstance.setValue(m_AttIndex.getIndex(), 0); } } push(newInstance, false); // No need to copy instance 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.