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

weka.core.neighboursearch.FilteredNeighbourSearch 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 .
 */

/*
 *    FilteredNeighbourSearch.java
 *    Copyright (C) 2014 University of Waikato
 */
package weka.core.neighboursearch;

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

import weka.core.Instance;
import weka.core.Instances;
import weka.core.Option;
import weka.core.OptionHandler;
import weka.core.RevisionUtils;
import weka.core.SerializedObject;
import weka.core.Utils;
import weka.core.Capabilities;
import weka.core.Capabilities.Capability;
import weka.core.CapabilitiesHandler;
import weka.filters.AllFilter;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.AddID;

/**
 * 
 * Applies the given filter before calling the given neighbour search method. The filter must not change the size of the dataset or the order of the instances! Also, the range setting that is specified for the distance function is ignored: all attributes are used for the distance calculation.
 * 

* * * * Valid options are:

* *

 -F
 *  The filter to use. (default: weka.filters.AllFilter)
* *
 -S
 *  The search method to use. (default: weka.core.neighboursearch.LinearNNSearch)
* *
 
 * Options specific to filter weka.filters.AllFilter:
 * 
* *
 -output-debug-info
 *  If set, filter is run in debug mode and
 *  may output additional info to the console
* *
 -do-not-check-capabilities
 *  If set, filter capabilities are not checked before filter is built
 *  (use with caution).
* *
 
 * Options specific to search method weka.core.neighboursearch.LinearNNSearch:
 * 
* *
 -S
 *  Skip identical instances (distances equal to zero).
 * 
* *
 -A <classname and options>
 *  Distance function to use.
 *  (default: weka.core.EuclideanDistance)
* *
 -P
 *  Calculate performance statistics.
* * * * @author Eibe Frank ([email protected]) * @version $Revision: 8034 $ */ public class FilteredNeighbourSearch extends NearestNeighbourSearch implements CapabilitiesHandler { /** For serialization */ private static final long serialVersionUID = 1369174644087067375L; /** Need to use ID filter to add ID so that we can identify instances */ protected AddID m_AddID = new AddID(); /** The index of the ID attribute */ protected int m_IndexOfID = -1; /** The filter object to use. */ protected Filter m_Filter = new AllFilter(); /** The neighborhood search method to use. */ protected NearestNeighbourSearch m_SearchMethod = new LinearNNSearch(); /** The modified search method, where ID is skipped */ protected NearestNeighbourSearch m_ModifiedSearchMethod = null; /** * Returns default capabilities of the classifier. * * @return the capabilities of this classifier */ public Capabilities getCapabilities() { Capabilities result = getFilter().getCapabilities(); // set dependencies for (Capability cap : Capability.values()) result.enableDependency(cap); return result; } /** * Sets the instances to build the filtering model from. * * @param insts the Instances object */ public void setInstances(Instances data) { try { super.setInstances(data); // Apply user-specified filter getCapabilities().testWithFail(data); Instances filteredData = new Instances(data); getFilter().setInputFormat(filteredData); filteredData = Filter.useFilter(data, getFilter()); if (data.numInstances() != filteredData.numInstances()) { throw new IllegalArgumentException( "FilteredNeighbourSearch: Filter has changed the number of instances!"); } // Set up filter to add ID m_IndexOfID = filteredData.numAttributes(); m_AddID.setIDIndex("" + (filteredData.numAttributes() + 1)); ; m_AddID.setInputFormat(filteredData); filteredData = Filter.useFilter(filteredData, m_AddID); // Modify distance function for base method to skip ID // User-specified range setting for the distance function is simply // ignored m_ModifiedSearchMethod = (NearestNeighbourSearch) new SerializedObject( getSearchMethod()).getObject(); m_ModifiedSearchMethod.getDistanceFunction().setAttributeIndices( "1-" + m_IndexOfID); m_ModifiedSearchMethod.getDistanceFunction().setInvertSelection(false); // Set up the distance function m_ModifiedSearchMethod.setInstances(filteredData); } catch (Exception e) { e.printStackTrace(); } } /** * Returns a string describing this object. * * @return a description of the evaluator suitable for displaying in the * explorer/experimenter gui */ public String globalInfo() { return "Applies the given filter before calling the given neighbour search method. The filter " + "must not change the size of the dataset or the order of the instances! Also, the range " + "setting that is specified for the distance function is ignored: all attributes are used " + "for the distance calculation."; } /** * Returns the tip text for this property * * @return tip text for this property suitable for displaying in the * explorer/experimenter gui */ public String filterTipText() { return "The filter to be used."; } /** * Sets the filter * * @param filter the filter with all options set. */ public void setFilter(Filter filter) { m_Filter = filter; } /** * Gets the filter used. * * @return the filter */ public Filter getFilter() { return m_Filter; } /** * Returns the tip text for this property * * @return tip text for this property suitable for displaying in the * explorer/experimenter gui */ public String searchMethodTipText() { return "The search method to be used."; } /** * Sets the search method * * @param searchMethod the search method with all options set. */ public void setSearchMethod(NearestNeighbourSearch search) { m_SearchMethod = search; } /** * Gets the search method used. * * @return the search method */ public NearestNeighbourSearch getSearchMethod() { return m_SearchMethod; } /** * 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