📄 libsvm.java
字号:
/* * 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 2 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, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * LibSVM.java * Copyright (C) 2005 Yasser EL-Manzalawy (original code) * Copyright (C) 2005 University of Waikato, Hamilton, NZ (adapted code) * */package weka.classifiers.functions;import weka.classifiers.Classifier;import weka.core.Capabilities;import weka.core.Instance;import weka.core.Instances;import weka.core.Option;import weka.core.SelectedTag;import weka.core.Tag;import weka.core.TechnicalInformation;import weka.core.TechnicalInformationHandler;import weka.core.Utils;import weka.core.Capabilities.Capability;import weka.core.TechnicalInformation.Type;import weka.filters.Filter;import weka.filters.unsupervised.attribute.Normalize;import java.lang.reflect.Array;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.util.Enumeration;import java.util.StringTokenizer;import java.util.Vector;/* * Modifications by FracPete: * - complete overhaul to make it useable in Weka * - accesses libsvm classes only via Reflection to make Weka compile without * the libsvm classes * - uses more efficient code to transfer the data into the libsvm sparse format *//** <!-- globalinfo-start --> * A wrapper class for the libsvm tools (the libsvm classes, typically the jar file, need to be in the classpath to use this classifier).<br/> * LibSVM runs faster than SMO since it uses LibSVM to build the SVM classifier.<br/> * 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.).<br/> * <br/> * Yasser EL-Manzalawy (2005). WLSVM. URL http://www.cs.iastate.edu/~yasser/wlsvm/.<br/> * <br/> * Chih-Chung Chang, Chih-Jen Lin (2001). LIBSVM - A Library for Support Vector Machines. URL http://www.csie.ntu.edu.tw/~cjlin/libsvm/. * <p/> <!-- globalinfo-end --> * <!-- technical-bibtex-start --> * BibTeX: * <pre> * @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/} * } * </pre> * <p/> <!-- technical-bibtex-end --> * <!-- options-start --> * Valid options are: <p/> * * <pre> -S <int> * Set type of SVM (default: 0) * 0 = C-SVC * 1 = nu-SVC * 2 = one-class SVM * 3 = epsilon-SVR * 4 = nu-SVR</pre> * * <pre> -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)</pre> * * <pre> -D <int> * Set degree in kernel function (default: 3)</pre> * * <pre> -G <double> * Set gamma in kernel function (default: 1/k)</pre> * * <pre> -R <double> * Set coef0 in kernel function (default: 0)</pre> * * <pre> -C <double> * Set the parameter C of C-SVC, epsilon-SVR, and nu-SVR * (default: 1)</pre> * * <pre> -N <double> * Set the parameter nu of nu-SVC, one-class SVM, and nu-SVR * (default: 0.5)</pre> * * <pre> -Z * Turns on normalization of input data (default: off)</pre> * * <pre> -P <double> * Set the epsilon in loss function of epsilon-SVR (default: 0.1)</pre> * * <pre> -M <double> * Set cache memory size in MB (default: 40)</pre> * * <pre> -E <double> * Set tolerance of termination criterion (default: 0.001)</pre> * * <pre> -H * Turns the shrinking heuristics off (default: on)</pre> * * <pre> -W <double> * Set the parameters C of class i to weight[i]*C, for C-SVC * (default: 1)</pre> * * <pre> -B * Trains a SVC model instead of a SVR one (default: SVR)</pre> * * <pre> -D * If set, classifier is run in debug mode and * may output additional info to the console</pre> * <!-- options-end --> * * @author Yasser EL-Manzalawy * @author FracPete (fracpete at waikato dot ac dot nz) * @version $Revision: 1.12 $ * @see weka.core.converters.LibSVMLoader * @see weka.core.converters.LibSVMSaver */public class LibSVM extends Classifier implements TechnicalInformationHandler { /** the svm classname */ protected final static String CLASS_SVM = "libsvm.svm"; /** the svm_model classname */ protected final static String CLASS_SVMMODEL = "libsvm.svm_model"; /** the svm_problem classname */ protected final static String CLASS_SVMPROBLEM = "libsvm.svm_problem"; /** the svm_parameter classname */ protected final static String CLASS_SVMPARAMETER = "libsvm.svm_parameter"; /** the svm_node classname */ protected final static String CLASS_SVMNODE = "libsvm.svm_node"; /** serial UID */ protected static final long serialVersionUID = 14172; /** LibSVM Model */ protected Object m_Model; /** for normalizing the data */ protected Filter m_Filter = null; /** normalize input data */ protected boolean m_Normalize = false; /** SVM type C-SVC (classification) */ public static final int SVMTYPE_C_SVC = 0; /** SVM type nu-SVC (classification) */ public static final int SVMTYPE_NU_SVC = 1; /** SVM type one-class SVM (classification) */ public static final int SVMTYPE_ONE_CLASS_SVM = 2; /** SVM type epsilon-SVR (regression) */ public static final int SVMTYPE_EPSILON_SVR = 3; /** SVM type nu-SVR (regression) */ public static final int SVMTYPE_NU_SVR = 4; /** SVM types */ public static final Tag[] TAGS_SVMTYPE = { new Tag(SVMTYPE_C_SVC, "C-SVC (classification)"), new Tag(SVMTYPE_NU_SVC, "nu-SVC (classification)"), new Tag(SVMTYPE_ONE_CLASS_SVM, "one-class SVM (classification)"), new Tag(SVMTYPE_EPSILON_SVR, "epsilon-SVR (regression)"), new Tag(SVMTYPE_NU_SVR, "nu-SVR (regression)") }; /** the SVM type */ protected int m_SVMType = SVMTYPE_C_SVC; /** kernel type linear: u'*v */ public static final int KERNELTYPE_LINEAR = 0; /** kernel type polynomial: (gamma*u'*v + coef0)^degree */ public static final int KERNELTYPE_POLYNOMIAL = 1; /** kernel type radial basis function: exp(-gamma*|u-v|^2) */ public static final int KERNELTYPE_RBF = 2; /** kernel type sigmoid: tanh(gamma*u'*v + coef0) */ public static final int KERNELTYPE_SIGMOID = 3; /** the different kernel types */ public static final Tag[] TAGS_KERNELTYPE = { new Tag(KERNELTYPE_LINEAR, "linear: u'*v"), new Tag(KERNELTYPE_POLYNOMIAL, "polynomial: (gamma*u'*v + coef0)^degree"), new Tag(KERNELTYPE_RBF, "radial basis function: exp(-gamma*|u-v|^2)"), new Tag(KERNELTYPE_SIGMOID, "sigmoid: tanh(gamma*u'*v + coef0)") }; /** the kernel type */ protected int m_KernelType = KERNELTYPE_RBF; /** for poly - in older versions of libsvm declared as a double. * At least since 2.82 it is an int. */ protected int m_Degree = 3; /** for poly/rbf/sigmoid */ protected double m_Gamma = 0; /** for poly/rbf/sigmoid (the actual gamma) */ protected double m_GammaActual = 0; /** for poly/sigmoid */ protected double m_Coef0 = 0; /** in MB */ protected double m_CacheSize = 40; /** stopping criteria */ protected double m_eps = 1e-3; /** cost, for C_SVC, EPSILON_SVR and NU_SVR */ protected double m_Cost = 1; /** for C_SVC */ protected int[] m_WeightLabel = new int[0]; /** for C_SVC */ protected double[] m_Weight = new double[0]; /** for NU_SVC, ONE_CLASS, and NU_SVR */ protected double m_nu = 0.5; /** loss, for EPSILON_SVR */ protected double m_Loss = 0.1; /** use the shrinking heuristics */ protected boolean m_Shrinking = true; /** whether to generate probability estimates instead of +1/-1 in case of * classification problems */ protected boolean m_ProbabilityEstimates = false; /** whether the libsvm classes are in the Classpath */ protected static boolean m_Present = false; static { try { Class.forName(CLASS_SVM); m_Present = true; } catch (Exception e) { m_Present = false; } } /** * Returns a string describing classifier * * @return a description suitable for displaying in the * explorer/experimenter gui */ public String globalInfo() { return "A wrapper class for the libsvm tools (the libsvm classes, typically " + "the jar file, need to be in the classpath to use this classifier).\n" + "LibSVM runs faster than SMO since it uses LibSVM to build the SVM " + "classifier.\n" + "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.).\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 */ public TechnicalInformation getTechnicalInformation() { TechnicalInformation result; TechnicalInformation additional; result = new TechnicalInformation(Type.MISC); result.setValue(TechnicalInformation.Field.AUTHOR, "Yasser EL-Manzalawy"); result.setValue(TechnicalInformation.Field.YEAR, "2005"); result.setValue(TechnicalInformation.Field.TITLE, "WLSVM"); result.setValue(TechnicalInformation.Field.NOTE, "LibSVM was originally developed as 'WLSVM'"); result.setValue(TechnicalInformation.Field.URL, "http://www.cs.iastate.edu/~yasser/wlsvm/"); result.setValue(TechnicalInformation.Field.NOTE, "You don't need to include the WLSVM package in the CLASSPATH"); additional = result.add(Type.MISC); additional.setValue(TechnicalInformation.Field.AUTHOR, "Chih-Chung Chang and Chih-Jen Lin"); additional.setValue(TechnicalInformation.Field.TITLE, "LIBSVM - A Library for Support Vector Machines"); additional.setValue(TechnicalInformation.Field.YEAR, "2001"); additional.setValue(TechnicalInformation.Field.URL, "http://www.csie.ntu.edu.tw/~cjlin/libsvm/"); additional.setValue(TechnicalInformation.Field.NOTE, "The Weka classifier works with version 2.82 of LIBSVM"); return result; } /** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options. */ public Enumeration listOptions() { Vector result; result = new Vector(); result.addElement( new Option( "\tSet type of SVM (default: 0)\n" + "\t\t 0 = C-SVC\n" + "\t\t 1 = nu-SVC\n" + "\t\t 2 = one-class SVM\n" + "\t\t 3 = epsilon-SVR\n" + "\t\t 4 = nu-SVR", "S", 1, "-S <int>")); result.addElement( new Option( "\tSet type of kernel function (default: 2)\n" + "\t\t 0 = linear: u'*v\n" + "\t\t 1 = polynomial: (gamma*u'*v + coef0)^degree\n" + "\t\t 2 = radial basis function: exp(-gamma*|u-v|^2)\n" + "\t\t 3 = sigmoid: tanh(gamma*u'*v + coef0)", "K", 1, "-K <int>")); result.addElement( new Option( "\tSet degree in kernel function (default: 3)", "D", 1, "-D <int>")); result.addElement( new Option( "\tSet gamma in kernel function (default: 1/k)", "G", 1, "-G <double>")); result.addElement( new Option( "\tSet coef0 in kernel function (default: 0)", "R", 1, "-R <double>")); result.addElement( new Option( "\tSet the parameter C of C-SVC, epsilon-SVR, and nu-SVR\n" + "\t (default: 1)", "C", 1, "-C <double>")); result.addElement( new Option( "\tSet the parameter nu of nu-SVC, one-class SVM, and nu-SVR\n" + "\t (default: 0.5)", "N", 1, "-N <double>")); result.addElement( new Option( "\tTurns on normalization of input data (default: off)", "Z", 0, "-Z")); result.addElement( new Option( "\tSet the epsilon in loss function of epsilon-SVR (default: 0.1)", "P", 1, "-P <double>")); result.addElement( new Option( "\tSet cache memory size in MB (default: 40)", "M", 1, "-M <double>")); result.addElement( new Option( "\tSet tolerance of termination criterion (default: 0.001)", "E", 1, "-E <double>")); result.addElement( new Option( "\tTurns the shrinking heuristics off (default: on)", "H", 0, "-H")); result.addElement( new Option( "\tSet the parameters C of class i to weight[i]*C, for C-SVC\n" + "\t (default: 1)", "W", 1, "-W <double>")); result.addElement( new Option( "\tTrains a SVC model instead of a SVR one (default: SVR)", "B", 0, "-B")); Enumeration en = super.listOptions(); while (en.hasMoreElements()) result.addElement(en.nextElement()); return result.elements(); } /** * Sets the classifier options <p/> * <!-- options-start --> * Valid options are: <p/> * * <pre> -S <int> * Set type of SVM (default: 0) * 0 = C-SVC * 1 = nu-SVC * 2 = one-class SVM * 3 = epsilon-SVR * 4 = nu-SVR</pre> * * <pre> -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)</pre> * * <pre> -D <int> * Set degree in kernel function (default: 3)</pre> * * <pre> -G <double> * Set gamma in kernel function (default: 1/k)</pre> * * <pre> -R <double> * Set coef0 in kernel function (default: 0)</pre> * * <pre> -C <double> * Set the parameter C of C-SVC, epsilon-SVR, and nu-SVR * (default: 1)</pre> * * <pre> -N <double> * Set the parameter nu of nu-SVC, one-class SVM, and nu-SVR * (default: 0.5)</pre> * * <pre> -Z * Turns on normalization of input data (default: off)</pre> * * <pre> -P <double> * Set the epsilon in loss function of epsilon-SVR (default: 0.1)</pre> * * <pre> -M <double> * Set cache memory size in MB (default: 40)</pre> * * <pre> -E <double> * Set tolerance of termination criterion (default: 0.001)</pre> * * <pre> -H * Turns the shrinking heuristics off (default: on)</pre> * * <pre> -W <double> * Set the parameters C of class i to weight[i]*C, for C-SVC * (default: 1)</pre> * * <pre> -B * Trains a SVC model instead of a SVR one (default: SVR)</pre> * * <pre> -D * If set, classifier is run in debug mode and * may output additional info to the console</pre> * <!-- options-end --> * * @param options the options to parse * @throws Exception if parsing fails */ public void setOptions(String[] options) throws Exception { String tmpStr; tmpStr = Utils.getOption('S', options); if (tmpStr.length() != 0) setSVMType(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -