All Downloads are FREE. Search and download functionalities are using the official Maven repository.

weka.filters.unsupervised.attribute.RemoveType Maven / Gradle / Ivy

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

/*
 *    RemoveType.java
 *    Copyright (C) 2002-2012 University of Waikato, Hamilton, New Zealand
 *
 */

package weka.filters.unsupervised.attribute;

import java.util.Enumeration;
import java.util.Vector;

import weka.core.Attribute;
import weka.core.Capabilities;
import weka.core.Capabilities.Capability;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.Option;
import weka.core.OptionHandler;
import weka.core.RevisionUtils;
import weka.core.SelectedTag;
import weka.core.Tag;
import weka.core.Utils;
import weka.filters.Filter;
import weka.filters.StreamableFilter;
import weka.filters.UnsupervisedFilter;

/**
 *  Removes attributes of a given type.
 * 

* * * Valid options are: *

* *

 * -T <nominal|numeric|string|date|relational>
 *  Attribute type to delete. Valid options are "nominal", 
 *  "numeric", "string", "date" and "relational".
 *  (default "string")
 * 
* *
 * -V
 *  Invert matching sense (i.e. only keep specified columns)
 * 
* * * * @author Richard Kirkby ([email protected]) * @version $Revision: 10215 $ */ public class RemoveType extends Filter implements UnsupervisedFilter, StreamableFilter, OptionHandler { /** for serialization */ static final long serialVersionUID = -3563999462782486279L; /** The attribute filter used to do the filtering */ protected Remove m_attributeFilter = new Remove(); /** The type of attribute to delete */ protected int m_attTypeToDelete = Attribute.STRING; /** Whether to invert selection */ protected boolean m_invert = false; /** Tag allowing selection of attribute type to delete */ public static final Tag[] TAGS_ATTRIBUTETYPE = { new Tag(Attribute.NOMINAL, "Delete nominal attributes"), new Tag(Attribute.NUMERIC, "Delete numeric attributes"), new Tag(Attribute.STRING, "Delete string attributes"), new Tag(Attribute.DATE, "Delete date attributes"), new Tag(Attribute.RELATIONAL, "Delete relational attributes") }; /** * 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.enable(Capability.NOMINAL_ATTRIBUTES); result.enable(Capability.NUMERIC_ATTRIBUTES); result.enable(Capability.DATE_ATTRIBUTES); result.enable(Capability.STRING_ATTRIBUTES); result.enable(Capability.RELATIONAL_ATTRIBUTES); 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 inputFormat can't be set successfully */ @Override public boolean setInputFormat(Instances instanceInfo) throws Exception { super.setInputFormat(instanceInfo); int[] attsToDelete = new int[instanceInfo.numAttributes()]; int numToDelete = 0; for (int i = 0; i < instanceInfo.numAttributes(); i++) { if (i == instanceInfo.classIndex()) { if (!m_invert) { continue; // skip class } else { attsToDelete[numToDelete++] = i; // Need to keep the class even if // selection is inverted } } if (instanceInfo.attribute(i).type() == m_attTypeToDelete) { attsToDelete[numToDelete++] = i; } } int[] finalAttsToDelete = new int[numToDelete]; System.arraycopy(attsToDelete, 0, finalAttsToDelete, 0, numToDelete); m_attributeFilter.setAttributeIndicesArray(finalAttsToDelete); m_attributeFilter.setInvertSelection(m_invert); boolean result = m_attributeFilter.setInputFormat(instanceInfo); Instances afOutputFormat = m_attributeFilter.getOutputFormat(); // restore old relation name to hide attribute filter stamp afOutputFormat.setRelationName(instanceInfo.relationName()); setOutputFormat(afOutputFormat); return result; } /** * Input an instance for filtering. * * @param instance the input instance * @return true if the filtered instance may now be collected with output(). */ @Override public boolean input(Instance instance) { return m_attributeFilter.input(instance); } /** * Signify that this batch of input to the filter is finished. * * @return true if there are instances pending output * @throws Exception if something goes wrong */ @Override public boolean batchFinished() throws Exception { return m_attributeFilter.batchFinished(); } /** * Output an instance after filtering and remove from the output queue. * * @return the instance that has most recently been filtered (or null if the * queue is empty). */ @Override public Instance output() { return m_attributeFilter.output(); } /** * Output an instance after filtering but do not remove from the output queue. * * @return the instance that has most recently been filtered (or null if the * queue is empty). */ @Override public Instance outputPeek() { return m_attributeFilter.outputPeek(); } /** * Returns the number of instances pending output * * @return the number of instances pending output */ @Override public int numPendingOutput() { return m_attributeFilter.numPendingOutput(); } /** * Returns whether the output format is ready to be collected * * @return true if the output format is set */ @Override public boolean isOutputFormatDefined() { return m_attributeFilter.isOutputFormatDefined(); } /** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options. */ @Override public Enumeration




© 2015 - 2024 Weber Informatics LLC | Privacy Policy