Class LibSVM

All Implemented Interfaces:
Serializable, Cloneable, CapabilitiesHandler, OptionHandler, Randomizable, RevisionHandler, TechnicalInformationHandler

public class LibSVM extends RandomizableClassifier implements TechnicalInformationHandler
A wrapper class for the libsvm tools (the libsvm classes, typically the jar file, need to be in the classpath to use this classifier).
LibSVM runs faster than SMO since it uses LibSVM to build the SVM classifier.
LibSVM allows users to experiment with One-class SVM, Regressing SVM, and nu-SVM supported by LibSVM tool. LibSVM reports many useful statistics about LibSVM classifier (e.g., confusion matrix,precision, recall, ROC score, etc.).

Yasser EL-Manzalawy (2005). WLSVM. URL http://www.cs.iastate.edu/~yasser/wlsvm/.

Chih-Chung Chang, Chih-Jen Lin (2001). LIBSVM - A Library for Support Vector Machines. URL http://www.csie.ntu.edu.tw/~cjlin/libsvm/.

BibTeX:

 @misc{EL-Manzalawy2005,
    author = {Yasser EL-Manzalawy},
    note = {You don't need to include the WLSVM package in the CLASSPATH},
    title = {WLSVM},
    year = {2005},
    URL = {http://www.cs.iastate.edu/\~yasser/wlsvm/}
 }
 
 @misc{Chang2001,
    author = {Chih-Chung Chang and Chih-Jen Lin},
    note = {The Weka classifier works with version 2.82 of LIBSVM},
    title = {LIBSVM - A Library for Support Vector Machines},
    year = {2001},
    URL = {http://www.csie.ntu.edu.tw/\~cjlin/libsvm/}
 }
 

Valid options are:

 -S <int>
  Set type of SVM (default: 0)
    0 = C-SVC
    1 = nu-SVC
    2 = one-class SVM
    3 = epsilon-SVR
    4 = nu-SVR
 
 -K <int>
  Set type of kernel function (default: 2)
    0 = linear: u'*v
    1 = polynomial: (gamma*u'*v + coef0)^degree
    2 = radial basis function: exp(-gamma*|u-v|^2)
    3 = sigmoid: tanh(gamma*u'*v + coef0)
 
 -D <int>
  Set degree in kernel function (default: 3)
 
 -G <double>
  Set gamma in kernel function (default: 1/k)
 
 -R <double>
  Set coef0 in kernel function (default: 0)
 
 -C <double>
  Set the parameter C of C-SVC, epsilon-SVR, and nu-SVR
   (default: 1)
 
 -N <double>
  Set the parameter nu of nu-SVC, one-class SVM, and nu-SVR
   (default: 0.5)
 
 -Z
  Turns on normalization of input data (default: off)
 
 -J
  Turn off nominal to binary conversion.
  WARNING: use only if your data is all numeric!
 
 -V
  Turn off missing value replacement.
  WARNING: use only if your data has no missing values.
 
 -P <double>
  Set the epsilon in loss function of epsilon-SVR (default: 0.1)
 
 -M <double>
  Set cache memory size in MB (default: 40)
 
 -E <double>
  Set tolerance of termination criterion (default: 0.001)
 
 -H
  Turns the shrinking heuristics off (default: on)
 
 -W <double>
  Set the parameters C of class i to weight[i]*C, for C-SVC
  E.g., for a 3-class problem, you could use "1 1 1" for equally
  weighted classes.
  (default: 1 for all classes)
 
 -B
  Generate probability estimates for classification
 
 -seed <num>
  Random seed
  (default = 1)
 
Version:
$Revision: 10660 $
Author:
Yasser EL-Manzalawy, FracPete (fracpete at waikato dot ac dot nz)
See Also:
  • Field Details

    • SVMTYPE_C_SVC

      public static final int SVMTYPE_C_SVC
      SVM type C-SVC (classification)
      See Also:
    • SVMTYPE_NU_SVC

      public static final int SVMTYPE_NU_SVC
      SVM type nu-SVC (classification)
      See Also:
    • SVMTYPE_ONE_CLASS_SVM

      public static final int SVMTYPE_ONE_CLASS_SVM
      SVM type one-class SVM (classification)
      See Also:
    • SVMTYPE_EPSILON_SVR

      public static final int SVMTYPE_EPSILON_SVR
      SVM type epsilon-SVR (regression)
      See Also:
    • SVMTYPE_NU_SVR

      public static final int SVMTYPE_NU_SVR
      SVM type nu-SVR (regression)
      See Also:
    • TAGS_SVMTYPE

      public static final Tag[] TAGS_SVMTYPE
      SVM types
    • KERNELTYPE_LINEAR

      public static final int KERNELTYPE_LINEAR
      kernel type linear: u'*v
      See Also:
    • KERNELTYPE_POLYNOMIAL

      public static final int KERNELTYPE_POLYNOMIAL
      kernel type polynomial: (gamma*u'*v + coef0)^degree
      See Also:
    • KERNELTYPE_RBF

      public static final int KERNELTYPE_RBF
      kernel type radial basis function: exp(-gamma*|u-v|^2)
      See Also:
    • KERNELTYPE_SIGMOID

      public static final int KERNELTYPE_SIGMOID
      kernel type sigmoid: tanh(gamma*u'*v + coef0)
      See Also:
    • TAGS_KERNELTYPE

      public static final Tag[] TAGS_KERNELTYPE
      the different kernel types
  • Constructor Details

    • LibSVM

      public LibSVM()
  • Method Details

    • globalInfo

      public String globalInfo()
      Returns a string describing classifier
      Returns:
      a description suitable for displaying in the explorer/experimenter gui
    • getTechnicalInformation

      public TechnicalInformation getTechnicalInformation()
      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.
      Specified by:
      getTechnicalInformation in interface TechnicalInformationHandler
      Returns:
      the technical information about this class
    • listOptions

      public Enumeration listOptions()
      Returns an enumeration describing the available options.
      Specified by:
      listOptions in interface OptionHandler
      Overrides:
      listOptions in class RandomizableClassifier
      Returns:
      an enumeration of all the available options.
    • setOptions

      public void setOptions(String[] options) throws Exception
      Sets the classifier options

      Valid options are:

       -S <int>
        Set type of SVM (default: 0)
          0 = C-SVC
          1 = nu-SVC
          2 = one-class SVM
          3 = epsilon-SVR
          4 = nu-SVR
       
       -K <int>
        Set type of kernel function (default: 2)
          0 = linear: u'*v
          1 = polynomial: (gamma*u'*v + coef0)^degree
          2 = radial basis function: exp(-gamma*|u-v|^2)
          3 = sigmoid: tanh(gamma*u'*v + coef0)
       
       -D <int>
        Set degree in kernel function (default: 3)
       
       -G <double>
        Set gamma in kernel function (default: 1/k)
       
       -R <double>
        Set coef0 in kernel function (default: 0)
       
       -C <double>
        Set the parameter C of C-SVC, epsilon-SVR, and nu-SVR
         (default: 1)
       
       -N <double>
        Set the parameter nu of nu-SVC, one-class SVM, and nu-SVR
         (default: 0.5)
       
       -Z
        Turns on normalization of input data (default: off)
       
       -J
        Turn off nominal to binary conversion.
        WARNING: use only if your data is all numeric!
       
       -V
        Turn off missing value replacement.
        WARNING: use only if your data has no missing values.
       
       -P <double>
        Set the epsilon in loss function of epsilon-SVR (default: 0.1)
       
       -M <double>
        Set cache memory size in MB (default: 40)
       
       -E <double>
        Set tolerance of termination criterion (default: 0.001)
       
       -H
        Turns the shrinking heuristics off (default: on)
       
       -W <double>
        Set the parameters C of class i to weight[i]*C, for C-SVC
        E.g., for a 3-class problem, you could use "1 1 1" for equally
        weighted classes.
        (default: 1 for all classes)
       
       -B
        Generate probability estimates for classification
       
       -seed <num>
        Random seed
        (default = 1)
       
      Specified by:
      setOptions in interface OptionHandler
      Overrides:
      setOptions in class RandomizableClassifier
      Parameters:
      options - the options to parse
      Throws:
      Exception - if parsing fails
    • getOptions

      public String[] getOptions()
      Returns the current options
      Specified by:
      getOptions in interface OptionHandler
      Overrides:
      getOptions in class RandomizableClassifier
      Returns:
      the current setup
    • isPresent

      public static boolean isPresent()
      returns whether the libsvm classes are present or not, i.e. whether the classes are in the classpath or not
      Returns:
      whether the libsvm classes are available
    • setSVMType

      public void setSVMType(SelectedTag value)
      Sets type of SVM (default SVMTYPE_C_SVC)
      Parameters:
      value - the type of the SVM
    • getSVMType

      public SelectedTag getSVMType()
      Gets type of SVM
      Returns:
      the type of the SVM
    • SVMTypeTipText

      public String SVMTypeTipText()
      Returns the tip text for this property
      Returns:
      tip text for this property suitable for displaying in the explorer/experimenter gui
    • setKernelType

      public void setKernelType(SelectedTag value)
      Sets type of kernel function (default KERNELTYPE_RBF)
      Parameters:
      value - the kernel type
    • getKernelType

      public SelectedTag getKernelType()
      Gets type of kernel function
      Returns:
      the kernel type
    • kernelTypeTipText

      public String kernelTypeTipText()
      Returns the tip text for this property
      Returns:
      tip text for this property suitable for displaying in the explorer/experimenter gui
    • setDegree

      public void setDegree(int value)
      Sets the degree of the kernel
      Parameters:
      value - the degree of the kernel
    • getDegree

      public int getDegree()
      Gets the degree of the kernel
      Returns:
      the degree of the kernel
    • degreeTipText

      public String degreeTipText()
      Returns the tip text for this property
      Returns:
      tip text for this property suitable for displaying in the explorer/experimenter gui
    • setGamma

      public void setGamma(double value)
      Sets gamma (default = 1/no of attributes)
      Parameters:
      value - the gamma value
    • getGamma

      public double getGamma()
      Gets gamma
      Returns:
      the current gamma
    • gammaTipText

      public String gammaTipText()
      Returns the tip text for this property
      Returns:
      tip text for this property suitable for displaying in the explorer/experimenter gui
    • setCoef0

      public void setCoef0(double value)
      Sets coef (default 0)
      Parameters:
      value - the coef
    • getCoef0

      public double getCoef0()
      Gets coef
      Returns:
      the coef
    • coef0TipText

      public String coef0TipText()
      Returns the tip text for this property
      Returns:
      tip text for this property suitable for displaying in the explorer/experimenter gui
    • setNu

      public void setNu(double value)
      Sets nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)
      Parameters:
      value - the new nu value
    • getNu

      public double getNu()
      Gets nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)
      Returns:
      the current nu value
    • nuTipText

      public String nuTipText()
      Returns the tip text for this property
      Returns:
      tip text for this property suitable for displaying in the explorer/experimenter gui
    • setCacheSize

      public void setCacheSize(double value)
      Sets cache memory size in MB (default 40)
      Parameters:
      value - the memory size in MB
    • getCacheSize

      public double getCacheSize()
      Gets cache memory size in MB
      Returns:
      the memory size in MB
    • cacheSizeTipText

      public String cacheSizeTipText()
      Returns the tip text for this property
      Returns:
      tip text for this property suitable for displaying in the explorer/experimenter gui
    • setCost

      public void setCost(double value)
      Sets the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)
      Parameters:
      value - the cost value
    • getCost

      public double getCost()
      Sets the parameter C of C-SVC, epsilon-SVR, and nu-SVR
      Returns:
      the cost value
    • costTipText

      public String costTipText()
      Returns the tip text for this property
      Returns:
      tip text for this property suitable for displaying in the explorer/experimenter gui
    • setEps

      public void setEps(double value)
      Sets tolerance of termination criterion (default 0.001)
      Parameters:
      value - the tolerance
    • getEps

      public double getEps()
      Gets tolerance of termination criterion
      Returns:
      the current tolerance
    • epsTipText

      public String epsTipText()
      Returns the tip text for this property
      Returns:
      tip text for this property suitable for displaying in the explorer/experimenter gui
    • setLoss

      public void setLoss(double value)
      Sets the epsilon in loss function of epsilon-SVR (default 0.1)
      Parameters:
      value - the loss epsilon
    • getLoss

      public double getLoss()
      Gets the epsilon in loss function of epsilon-SVR
      Returns:
      the loss epsilon
    • lossTipText

      public String lossTipText()
      Returns the tip text for this property
      Returns:
      tip text for this property suitable for displaying in the explorer/experimenter gui
    • setShrinking

      public void setShrinking(boolean value)
      whether to use the shrinking heuristics
      Parameters:
      value - true uses shrinking
    • getShrinking

      public boolean getShrinking()
      whether to use the shrinking heuristics
      Returns:
      true, if shrinking is used
    • shrinkingTipText

      public String shrinkingTipText()
      Returns the tip text for this property
      Returns:
      tip text for this property suitable for displaying in the explorer/experimenter gui
    • setNormalize

      public void setNormalize(boolean value)
      whether to normalize input data
      Parameters:
      value - whether to normalize the data
    • getNormalize

      public boolean getNormalize()
      whether to normalize input data
      Returns:
      true, if the data is normalized
    • normalizeTipText

      public String normalizeTipText()
      Returns the tip text for this property
      Returns:
      tip text for this property suitable for displaying in the explorer/experimenter gui
    • doNotReplaceMissingValuesTipText

      public String doNotReplaceMissingValuesTipText()
      Returns the tip text for this property
      Returns:
      tip text for this property suitable for displaying in the explorer/experimenter gui
    • setDoNotReplaceMissingValues

      public void setDoNotReplaceMissingValues(boolean b)
      Whether to turn off automatic replacement of missing values. Set to true only if the data does not contain missing values.
      Parameters:
      b - true if automatic missing values replacement is to be disabled.
    • getDoNotReplaceMissingValues

      public boolean getDoNotReplaceMissingValues()
      Gets whether automatic replacement of missing values is disabled.
      Returns:
      true if automatic replacement of missing values is disabled.
    • setWeights

      public void setWeights(String weightsStr)
      Sets the parameters C of class i to weight[i]*C, for C-SVC (default 1). Blank separated list of doubles.
      Parameters:
      weightsStr - the weights (doubles, separated by blanks)
    • getWeights

      public String getWeights()
      Gets the parameters C of class i to weight[i]*C, for C-SVC (default 1). Blank separated doubles.
      Returns:
      the weights (doubles separated by blanks)
    • weightsTipText

      public String weightsTipText()
      Returns the tip text for this property
      Returns:
      tip text for this property suitable for displaying in the explorer/experimenter gui
    • setProbabilityEstimates

      public void setProbabilityEstimates(boolean value)
      Returns whether probability estimates are generated instead of -1/+1 for classification problems.
      Parameters:
      value - whether to predict probabilities
    • getProbabilityEstimates

      public boolean getProbabilityEstimates()
      Sets whether to generate probability estimates instead of -1/+1 for classification problems.
      Returns:
      true, if probability estimates should be returned
    • probabilityEstimatesTipText

      public String probabilityEstimatesTipText()
      Returns the tip text for this property
      Returns:
      tip text for this property suitable for displaying in the explorer/experimenter gui
    • distributionForInstance

      public double[] distributionForInstance(Instance instance) throws Exception
      Computes the distribution for a given instance. In case of 1-class classification, 1 is returned at index 0 if libsvm returns 1 and NaN (= missing) if libsvm returns -1.
      Overrides:
      distributionForInstance in class Classifier
      Parameters:
      instance - the instance for which distribution is computed
      Returns:
      the distribution
      Throws:
      Exception - if the distribution can't be computed successfully
    • getCapabilities

      public Capabilities getCapabilities()
      Returns default capabilities of the classifier.
      Specified by:
      getCapabilities in interface CapabilitiesHandler
      Overrides:
      getCapabilities in class Classifier
      Returns:
      the capabilities of this classifier
      See Also:
    • buildClassifier

      public void buildClassifier(Instances insts) throws Exception
      builds the classifier
      Specified by:
      buildClassifier in class Classifier
      Parameters:
      insts - the training instances
      Throws:
      Exception - if libsvm classes not in classpath or libsvm encountered a problem
    • toString

      public String toString()
      returns a string representation
      Overrides:
      toString in class Object
      Returns:
      a string representation
    • getRevision

      public String getRevision()
      Returns the revision string.
      Specified by:
      getRevision in interface RevisionHandler
      Overrides:
      getRevision in class Classifier
      Returns:
      the revision
    • main

      public static void main(String[] args)
      Main method for testing this class.
      Parameters:
      args - the options