⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 misvm.java

📁 代码是一个分类器的实现,其中使用了部分weka的源代码。可以将项目导入eclipse运行
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* *    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, InumBag., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * MISVM.java * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand * */package weka.classifiers.mi;import weka.classifiers.Classifier;import weka.classifiers.functions.SMO;import weka.classifiers.functions.supportVector.Kernel;import weka.classifiers.functions.supportVector.PolyKernel;import weka.core.Capabilities;import weka.core.Instance;import weka.core.Instances;import weka.core.MultiInstanceCapabilitiesHandler;import weka.core.Option;import weka.core.OptionHandler;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.Field;import weka.core.TechnicalInformation.Type;import weka.filters.Filter;import weka.filters.unsupervised.attribute.MultiInstanceToPropositional;import weka.filters.unsupervised.attribute.Normalize;import weka.filters.unsupervised.attribute.Standardize;import weka.filters.unsupervised.instance.SparseToNonSparse;import java.util.Enumeration;import java.util.Vector;/** <!-- globalinfo-start --> * Implements Stuart Andrews' mi_SVM (Maximum pattern Margin Formulation of MIL). Applying weka.classifiers.functions.SMO to solve multiple instances problem.<br/> * The algorithm first assign the bag label to each instance in the bag as its initial class label.  After that applying SMO to compute SVM solution for all instances in positive bags And then reassign the class label of each instance in the positive bag according to the SVM result Keep on iteration until labels do not change anymore.<br/> * <br/> * For more information see:<br/> * <br/> * Stuart Andrews, Ioannis Tsochantaridis, Thomas Hofmann: Support Vector Machines for Multiple-Instance Learning. In: Advances in Neural Information Processing Systems 15, 561-568, 2003. * <p/> <!-- globalinfo-end --> *  <!-- technical-bibtex-start --> * BibTeX: * <pre> * &#64;inproceedings{Andrews2003, *    author = {Stuart Andrews and Ioannis Tsochantaridis and Thomas Hofmann}, *    booktitle = {Advances in Neural Information Processing Systems 15}, *    pages = {561-568}, *    publisher = {MIT Press}, *    title = {Support Vector Machines for Multiple-Instance Learning}, *    year = {2003} * } * </pre> * <p/> <!-- technical-bibtex-end --> * <!-- options-start --> * Valid options are: <p/> *  * <pre> -D *  If set, classifier is run in debug mode and *  may output additional info to the console</pre> *  * <pre> -C &lt;double&gt; *  The complexity constant C. (default 1)</pre> *  * <pre> -N &lt;default 0&gt; *  Whether to 0=normalize/1=standardize/2=neither. *  (default: 0=normalize)</pre> *  * <pre> -I &lt;num&gt; *  The maximum number of iterations to perform. *  (default: 500)</pre> *  * <pre> -K &lt;classname and parameters&gt; *  The Kernel to use. *  (default: weka.classifiers.functions.supportVector.PolyKernel)</pre> *  * <pre>  * Options specific to kernel weka.classifiers.functions.supportVector.PolyKernel: * </pre> *  * <pre> -D *  Enables debugging output (if available) to be printed. *  (default: off)</pre> *  * <pre> -no-checks *  Turns off all checks - use with caution! *  (default: checks on)</pre> *  * <pre> -C &lt;num&gt; *  The size of the cache (a prime number). *  (default: 250007)</pre> *  * <pre> -E &lt;num&gt; *  The Exponent to use. *  (default: 1.0)</pre> *  * <pre> -L *  Use lower-order terms. *  (default: no)</pre> *  <!-- options-end --> * * @author Lin Dong (ld21@cs.waikato.ac.nz) * @version $Revision: 1.4 $  * @see weka.classifiers.functions.SMO */public class MISVM   extends Classifier   implements OptionHandler, MultiInstanceCapabilitiesHandler,             TechnicalInformationHandler {  /** for serialization */  static final long serialVersionUID = 7622231064035278145L;    /** The filter used to transform the sparse datasets to nonsparse */  protected Filter m_SparseFilter = new SparseToNonSparse();  /** The SMO classifier used to compute SVM soluton w,b for the dataset */  protected SVM m_SVM;  /** the kernel to use */  protected Kernel m_kernel = new PolyKernel();  /** The complexity parameter. */  protected double m_C = 1.0;  /** The filter used to standardize/normalize all values. */  protected Filter m_Filter =null;  /** Whether to normalize/standardize/neither */  protected int m_filterType = FILTER_NORMALIZE;  /** Normalize training data */  public static final int FILTER_NORMALIZE = 0;  /** Standardize training data */  public static final int FILTER_STANDARDIZE = 1;  /** No normalization/standardization */  public static final int FILTER_NONE = 2;  /** The filter to apply to the training data */  public static final Tag [] TAGS_FILTER = {    new Tag(FILTER_NORMALIZE, "Normalize training data"),    new Tag(FILTER_STANDARDIZE, "Standardize training data"),    new Tag(FILTER_NONE, "No normalization/standardization"),  };  /** the maximum number of iterations to perform */  protected int m_MaxIterations = 500;    /** filter used to convert the MI dataset into single-instance dataset */  protected MultiInstanceToPropositional m_ConvertToProp = new MultiInstanceToPropositional();  /**   * Returns a string describing this filter   *   * @return a description of the filter suitable for   * displaying in the explorer/experimenter gui   */  public String globalInfo() {    return          "Implements Stuart Andrews' mi_SVM (Maximum pattern Margin "       + "Formulation of MIL). Applying weka.classifiers.functions.SMO "       + "to solve multiple instances problem.\n"       + "The algorithm first assign the bag label to each instance in the "       + "bag as its initial class label.  After that applying SMO to compute "       + "SVM solution for all instances in positive bags And then reassign "       + "the class label of each instance in the positive bag according to "       + "the SVM result Keep on iteration until labels do not change "       + "anymore.\n\n"       + "For more information 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   */  public TechnicalInformation getTechnicalInformation() {    TechnicalInformation 	result;        result = new TechnicalInformation(Type.INPROCEEDINGS);    result.setValue(Field.AUTHOR, "Stuart Andrews and Ioannis Tsochantaridis and Thomas Hofmann");    result.setValue(Field.YEAR, "2003");    result.setValue(Field.TITLE, "Support Vector Machines for Multiple-Instance Learning");    result.setValue(Field.BOOKTITLE, "Advances in Neural Information Processing Systems 15");    result.setValue(Field.PUBLISHER, "MIT Press");    result.setValue(Field.PAGES, "561-568");        return result;  }  /**   * Returns an enumeration describing the available options   *   * @return an enumeration of all the available options   */  public Enumeration listOptions() {    Vector result = new Vector();        Enumeration enm = super.listOptions();    while (enm.hasMoreElements())      result.addElement(enm.nextElement());    result.addElement(new Option(          "\tThe complexity constant C. (default 1)",          "C", 1, "-C <double>"));        result.addElement(new Option(        "\tWhether to 0=normalize/1=standardize/2=neither.\n"        + "\t(default: 0=normalize)",        "N", 1, "-N <default 0>"));      result.addElement(new Option(        "\tThe maximum number of iterations to perform.\n"        + "\t(default: 500)",        "I", 1, "-I <num>"));      result.addElement(new Option(	"\tThe Kernel to use.\n"	+ "\t(default: weka.classifiers.functions.supportVector.PolyKernel)",	"K", 1, "-K <classname and parameters>"));    result.addElement(new Option(	"",	"", 0, "\nOptions specific to kernel "	+ getKernel().getClass().getName() + ":"));        enm = ((OptionHandler) getKernel()).listOptions();    while (enm.hasMoreElements())      result.addElement(enm.nextElement());    return result.elements();  }  /**   * Parses a given list of options. <p/>   *    <!-- options-start -->   * Valid options are: <p/>   *    * <pre> -D   *  If set, classifier is run in debug mode and   *  may output additional info to the console</pre>   *    * <pre> -C &lt;double&gt;   *  The complexity constant C. (default 1)</pre>   *    * <pre> -N &lt;default 0&gt;   *  Whether to 0=normalize/1=standardize/2=neither.   *  (default: 0=normalize)</pre>   *    * <pre> -I &lt;num&gt;   *  The maximum number of iterations to perform.   *  (default: 500)</pre>   *    * <pre> -K &lt;classname and parameters&gt;   *  The Kernel to use.   *  (default: weka.classifiers.functions.supportVector.PolyKernel)</pre>   *    * <pre>    * Options specific to kernel weka.classifiers.functions.supportVector.PolyKernel:   * </pre>   *    * <pre> -D   *  Enables debugging output (if available) to be printed.   *  (default: off)</pre>   *    * <pre> -no-checks   *  Turns off all checks - use with caution!   *  (default: checks on)</pre>   *    * <pre> -C &lt;num&gt;   *  The size of the cache (a prime number).   *  (default: 250007)</pre>   *    * <pre> -E &lt;num&gt;   *  The Exponent to use.   *  (default: 1.0)</pre>   *    * <pre> -L   *  Use lower-order terms.   *  (default: no)</pre>   *    <!-- options-end -->   *    * @param options the list of options as an array of strings   * @throws Exception if an option is not supported   */  public void setOptions(String[] options) throws Exception {    String	tmpStr;    String[]	tmpOptions;        tmpStr = Utils.getOption('C', options);    if (tmpStr.length() != 0)      setC(Double.parseDouble(tmpStr));    else      setC(1.0);        tmpStr = Utils.getOption('N', options);    if (tmpStr.length() != 0)      setFilterType(new SelectedTag(Integer.parseInt(tmpStr), TAGS_FILTER));    else      setFilterType(new SelectedTag(FILTER_NORMALIZE, TAGS_FILTER));    tmpStr = Utils.getOption('I', options);    if (tmpStr.length() != 0)      setMaxIterations(Integer.parseInt(tmpStr));    else      setMaxIterations(500);        tmpStr     = Utils.getOption('K', options);    tmpOptions = Utils.splitOptions(tmpStr);    if (tmpOptions.length != 0) {      tmpStr        = tmpOptions[0];      tmpOptions[0] = "";      setKernel(Kernel.forName(tmpStr, tmpOptions));    }        super.setOptions(options);  }  /**   * Gets the current settings of the classifier.   *   * @return an array of strings suitable for passing to setOptions   */  public String[] getOptions() {    Vector        result;        result = new Vector();    if (getDebug())      result.add("-D");        result.add("-C");    result.add("" + getC());        result.add("-N");    result.add("" + m_filterType);    result.add("-K");    result.add("" + getKernel().getClass().getName() + " " + Utils.joinOptions(getKernel().getOptions()));    return (String[]) result.toArray(new String[result.size()]);  }    /**   * Returns the tip text for this property   *    * @return 		tip text for this property suitable for   * 			displaying in the explorer/experimenter gui   */  public String kernelTipText() {    return "The kernel to use.";  }  /**   * Gets the kernel to use.   *   * @return 		the kernel   */  public Kernel getKernel() {    return m_kernel;  }      /**   * Sets the kernel to use.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -