[Home] DMelt member access. Java source code: weka.filters.unsupervised.attribute.KernelFilter
/*
* 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 .
*/
/*
* KernelFilter.java
* Copyright (C) 2005-2012 University of Waikato, Hamilton, New Zealand
*
*/
package weka.filters.unsupervised.attribute;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Vector;
import weka.classifiers.functions.supportVector.Kernel;
import weka.classifiers.functions.supportVector.PolyKernel;
import weka.classifiers.functions.supportVector.RBFKernel;
import weka.core.Attribute;
import weka.core.Capabilities;
import weka.core.Capabilities.Capability;
import weka.core.DenseInstance;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.Option;
import weka.core.OptionHandler;
import weka.core.RevisionUtils;
import weka.core.SingleIndex;
import weka.core.TechnicalInformation;
import weka.core.TechnicalInformation.Field;
import weka.core.TechnicalInformation.Type;
import weka.core.TechnicalInformationHandler;
import weka.core.Utils;
import weka.core.converters.ConverterUtils.DataSource;
import weka.core.expressionlanguage.common.IfElseMacro;
import weka.core.expressionlanguage.common.JavaMacro;
import weka.core.expressionlanguage.common.MacroDeclarationsCompositor;
import weka.core.expressionlanguage.common.MathFunctions;
import weka.core.expressionlanguage.common.Primitives.DoubleExpression;
import weka.core.expressionlanguage.common.SimpleVariableDeclarations;
import weka.core.expressionlanguage.core.Node;
import weka.core.expressionlanguage.parser.Parser;
import weka.filters.AllFilter;
import weka.filters.Filter;
import weka.filters.SimpleBatchFilter;
import weka.filters.UnsupervisedFilter;
/**
* Converts the given set of data into
* a kernel matrix. The class value remains unchanged as long as the chosen
* preprocessing filter does not change it.
* By default, the data is preprocessed with the Center filter, but the user can
* choose any filter. (NB: one must be careful that the filter does not alter the
* class attribute unintentionally.) With weka.filters.AllFilter the
* preprocessing gets disabled.
*
* For more information regarding preprocessing the data, see:
*
* K.P. Bennett, M.J. Embrechts: An Optimization Perspective on Kernel Partial
* Least Squares Regression. In: Advances in Learning Theory: Methods, Models
* and Applications, 227-249, 2003.
*
*
*
* BibTeX:
*
*
* @inproceedings{Bennett2003,
* author = {K.P. Bennett and M.J. Embrechts},
* booktitle = {Advances in Learning Theory: Methods, Models and Applications},
* editor = {J. Suykens et al.},
* pages = {227-249},
* publisher = {IOS Press, Amsterdam, The Netherlands},
* series = {NATO Science Series, Series III: Computer and System Sciences},
* title = {An Optimization Perspective on Kernel Partial Least Squares Regression},
* volume = {190},
* year = {2003}
* }
*
*
*
*
* Valid options are:
*
*
*
* -output-debug-info
* Turns on output of debugging information.
*
*
*
* -no-checks
* Turns off all checks - use with caution!
* Turning them off assumes that data is purely numeric, doesn't
* contain any missing values, and has a nominal class. Turning them
* off also means that no header information will be stored if the
* machine is linear. Finally, it also assumes that no instance has
* a weight equal to 0.
* (default: checks on)
*
*
*
* -F <filename>
* The file to initialize the filter with (optional).
*
*
*
* -C <num>
* The class index for the file to initialize with,
* First and last are valid (optional, default: last).
*
*
*
* -K <classname and parameters>
* The Kernel to use.
* (default: weka.classifiers.functions.supportVector.PolyKernel)
*
*
*
* -kernel-factor
* Defines a factor for the kernel.
* - RBFKernel: a factor for gamma
* Standardize: 1/(2*N)
* Normalize..: 6/N
* Available parameters are:
* N for # of instances, A for # of attributes
* (default: 1)
*
*
*
* -P <classname and parameters>
* The Filter used for preprocessing (use weka.filters.AllFilter
* to disable preprocessing).
* (default: weka.filters.unsupervised.attribute.Center)
*
*
*
* Options specific to kernel weka.classifiers.functions.supportVector.PolyKernel:
*
*
*
* -D
* Enables debugging output (if available) to be printed.
* (default: off)
*
*
*
* -no-checks
* Turns off all checks - use with caution!
* (default: checks on)
*
*
*
* -C <num>
* The size of the cache (a prime number), 0 for full cache and
* -1 to turn it off.
* (default: 250007)
*
*
*
* -E <num>
* The Exponent to use.
* (default: 1.0)
*
*
*
* -L
* Use lower-order terms.
* (default: no)
*
*
*
* Options specific to preprocessing filter weka.filters.unsupervised.attribute.Center:
*
*
*
* -unset-class-temporarily
* Unsets the class index temporarily before the filter is
* applied to the data.
* (default: no)
*
*
*
*
* @author Jonathan Miles (jdm18@cs.waikato.ac.nz)
* @author FracPete (fracpete at waikato dot ac dot nz)
* @version $Revision: 14266 $
*/
public class KernelFilter extends SimpleBatchFilter implements
UnsupervisedFilter, TechnicalInformationHandler {
/** for serialization */
static final long serialVersionUID = 213800899640387499L;
/** The number of instances in the training data. */
protected int m_NumTrainInstances;
/** Kernel to use **/
protected Kernel m_Kernel = new PolyKernel();
/** the Kernel which is actually used for computation */
protected Kernel m_ActualKernel = null;
/**
* Turn off all checks and conversions? Turning them off assumes that data is
* purely numeric, doesn't contain any missing values, and has a nominal
* class. Turning them off also means that no header information will be
* stored if the machine is linear. Finally, it also assumes that no instance
* has a weight equal to 0.
*/
protected boolean m_checksTurnedOff;
/** The filter used to make attributes numeric. */
protected NominalToBinary m_NominalToBinary;
/** The filter used to get rid of missing values. */
protected ReplaceMissingValues m_Missing;
/** The dataset to initialize the filter with */
protected File m_InitFile = new File(System.getProperty("user.dir"));
/**
* the class index for the file to initialized with
*
* @see #m_InitFile
*/
protected SingleIndex m_InitFileClassIndex = new SingleIndex("last");
/** whether the filter was initialized */
protected boolean m_Initialized = false;
/**
* optimizes the kernel with this formula (A = # of attributes, N = # of
* instances)
*/
protected String m_KernelFactorExpression = "1";
/**
* the calculated kernel factor
*
* @see #m_KernelFactorExpression
*/
protected double m_KernelFactor = 1.0;
/** for centering/standardizing the data */
protected Filter m_Filter = new Center();
/** for centering/standardizing the data (the actual filter to use) */
protected Filter m_ActualFilter = null;
/**
* Returns a string describing this filter.
*
* @return a description of the filter suitable for displaying in the
* explorer/experimenter gui
*/
@Override
public String globalInfo() {
return "Converts the given set of data into a kernel matrix. "
+ "The class value remains unchanged as long as the chosen preprocessing "
+ "filter does not change it.\n\n"
+ "By default, the data is preprocessed with the Center filter, but the "
+ "user can choose any filter. (NB: one must be careful that the filter "
+ "does not alter the class attribute unintentionally.) With "
+ "weka.filters.AllFilter the preprocessing gets disabled.\n\n"
+ "For more information regarding preprocessing the data, see:\n\n"
+ getTechnicalInformation().toString();
}
/**
* Returns an instance of a TechnicalInformation object, containing detailed
* information about the technical background of this class, e.g., paper
* reference or book this class is based on.
*
* @return the technical information about this class
*/
@Override
public TechnicalInformation getTechnicalInformation() {
TechnicalInformation result;
result = new TechnicalInformation(Type.INPROCEEDINGS);
result.setValue(Field.AUTHOR, "K.P. Bennett and M.J. Embrechts");
result.setValue(Field.TITLE,
"An Optimization Perspective on Kernel Partial Least Squares Regression");
result.setValue(Field.YEAR, "2003");
result.setValue(Field.EDITOR, "J. Suykens et al.");
result.setValue(Field.BOOKTITLE,
"Advances in Learning Theory: Methods, Models and Applications");
result.setValue(Field.PAGES, "227-249");
result.setValue(Field.PUBLISHER, "IOS Press, Amsterdam, The Netherlands");
result.setValue(Field.SERIES,
"NATO Science Series, Series III: Computer and System Sciences");
result.setValue(Field.VOLUME, "190");
return result;
}
/**
* Returns an enumeration describing the available options.
*
* @return an enumeration of all the available options.
*/
@Override
public Enumeration