/* * 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. */ /* * MIOptimalBall.java * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand * */ package weka.classifiers.mi; import weka.classifiers.Classifier; import weka.classifiers.AbstractClassifier; 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.RevisionUtils; import weka.core.SelectedTag; import weka.core.Tag; import weka.core.TechnicalInformation; import weka.core.TechnicalInformationHandler; import weka.core.Utils; import weka.core.WeightedInstancesHandler; import weka.core.Capabilities.Capability; import weka.core.TechnicalInformation.Field; import weka.core.TechnicalInformation.Type; import weka.core.matrix.DoubleVector; import weka.filters.Filter; import weka.filters.unsupervised.attribute.MultiInstanceToPropositional; import weka.filters.unsupervised.attribute.Normalize; import weka.filters.unsupervised.attribute.PropositionalToMultiInstance; import weka.filters.unsupervised.attribute.Standardize; import java.util.Enumeration; import java.util.Vector; /** * This classifier tries to find a suitable ball in the multiple-instance space, with a certain data point in the instance space as a ball center. The possible ball center is a certain instance in a positive bag. The possible radiuses are those which can achieve the highest classification accuracy. The model selects the maximum radius as the radius of the optimal ball.
*
* For more information about this algorithm, see:
*
* Peter Auer, Ronald Ortner: A Boosting Approach to Multiple Instance Learning. In: 15th European Conference on Machine Learning, 63-74, 2004. *

* * BibTeX: *

 * @inproceedings{Auer2004,
 *    author = {Peter Auer and Ronald Ortner},
 *    booktitle = {15th European Conference on Machine Learning},
 *    note = {LNAI 3201},
 *    pages = {63-74},
 *    publisher = {Springer},
 *    title = {A Boosting Approach to Multiple Instance Learning},
 *    year = {2004}
 * }
 * 
*

* * Valid options are:

* *

 -N <num>
 *  Whether to 0=normalize/1=standardize/2=neither. 
 *  (default 0=normalize)
* * * @author Lin Dong (ld21@cs.waikato.ac.nz) * @version $Revision: 5928 $ */ public class MIOptimalBall extends AbstractClassifier implements OptionHandler, WeightedInstancesHandler, MultiInstanceCapabilitiesHandler, TechnicalInformationHandler { /** for serialization */ static final long serialVersionUID = -6465750129576777254L; /** center of the optimal ball */ protected double[] m_Center; /** radius of the optimal ball */ protected double m_Radius; /** the distances from each instance in a positive bag to each bag*/ protected double [][][]m_Distance; /** 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"), }; /** filter used to convert the MI dataset into single-instance dataset */ protected MultiInstanceToPropositional m_ConvertToSI = new MultiInstanceToPropositional(); /** filter used to convert the single-instance dataset into MI dataset */ protected PropositionalToMultiInstance m_ConvertToMI = new PropositionalToMultiInstance(); /** * Returns a string describing this filter * * @return a description of the filter suitable for * displaying in the explorer/experimenter gui */ public String globalInfo() { return "This classifier tries to find a suitable ball in the " + "multiple-instance space, with a certain data point in the instance " + "space as a ball center. The possible ball center is a certain " + "instance in a positive bag. The possible radiuses are those which can " + "achieve the highest classification accuracy. The model selects the " + "maximum radius as the radius of the optimal ball.\n\n" + "For more information about this algorithm, 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, "Peter Auer and Ronald Ortner"); result.setValue(Field.TITLE, "A Boosting Approach to Multiple Instance Learning"); result.setValue(Field.BOOKTITLE, "15th European Conference on Machine Learning"); result.setValue(Field.YEAR, "2004"); result.setValue(Field.PAGES, "63-74"); result.setValue(Field.PUBLISHER, "Springer"); result.setValue(Field.NOTE, "LNAI 3201"); return result; } /** * Returns default capabilities of the classifier. * * @return the capabilities of this classifier */ public Capabilities getCapabilities() { Capabilities result = super.getCapabilities(); result.disableAll(); // attributes result.enable(Capability.NOMINAL_ATTRIBUTES); result.enable(Capability.RELATIONAL_ATTRIBUTES); result.enable(Capability.MISSING_VALUES); // class result.enable(Capability.BINARY_CLASS); result.enable(Capability.MISSING_CLASS_VALUES); // other result.enable(Capability.ONLY_MULTIINSTANCE); return result; } /** * Returns the capabilities of this multi-instance classifier for the * relational data. * * @return the capabilities of this object * @see Capabilities */ public Capabilities getMultiInstanceCapabilities() { Capabilities result = super.getCapabilities(); result.disableAll(); // attributes result.enable(Capability.NOMINAL_ATTRIBUTES); result.enable(Capability.NUMERIC_ATTRIBUTES); result.enable(Capability.DATE_ATTRIBUTES); result.enable(Capability.MISSING_VALUES); // class result.disableAllClasses(); result.enable(Capability.NO_CLASS); return result; } /** * Builds the classifier * * @param data the training data to be used for generating the * boosted classifier. * @throws Exception if the classifier could not be built successfully */ public void buildClassifier(Instances data) throws Exception { // can classifier handle the data? getCapabilities().testWithFail(data); // remove instances with missing class Instances train = new Instances(data); train.deleteWithMissingClass(); int numAttributes = train.attribute(1).relation().numAttributes(); m_Center = new double[numAttributes]; if (getDebug()) System.out.println("Start training ..."); // convert the training dataset into single-instance dataset m_ConvertToSI.setInputFormat(train); train = Filter.useFilter( train, m_ConvertToSI); if (m_filterType == FILTER_STANDARDIZE) m_Filter = new Standardize(); else if (m_filterType == FILTER_NORMALIZE) m_Filter = new Normalize(); else m_Filter = null; if (m_Filter!=null) { // normalize/standardize the converted training dataset m_Filter.setInputFormat(train); train = Filter.useFilter(train, m_Filter); } // convert the single-instance dataset into multi-instance dataset m_ConvertToMI.setInputFormat(train); train = Filter.useFilter(train, m_ConvertToMI); /*calculate all the distances (and store them in m_Distance[][][]), which are from each instance in all positive bags to all bags */ calculateDistance(train); /*find the suitable ball center (m_Center) and the corresponding radius (m_Radius)*/ findRadius(train); if (getDebug()) System.out.println("Finish building optimal ball model"); } /** * calculate the distances from each instance in a positive bag to each bag. * All result distances are stored in m_Distance[i][j][k], where * m_Distance[i][j][k] refers the distances from the jth instance in ith bag * to the kth bag * * @param train the multi-instance dataset (with relational attribute) */ public void calculateDistance (Instances train) { int numBags =train.numInstances(); int numInstances; Instance tempCenter; m_Distance = new double [numBags][][]; for (int i=0; idistance) minDistance = distance; } return Math.sqrt(minDistance); } /** * Find the maximum radius for the optimal ball. * * @param train the multi-instance data */ public void findRadius(Instances train) { int numBags, numInstances; double radius, bagDistance; int highestCount=0; numBags = train.numInstances(); //try each instance in all positive bag as a ball center (tempCenter), for (int i=0; i radius && train.instance(n).classValue ()==0.0)) correctCount += train.instance(n).weight(); } //and keep the track of the ball center and the maximum radius which can achieve the highest accuracy. if (correctCount > highestCount || (correctCount==highestCount && radius > m_Radius)){ highestCount = correctCount; m_Radius = radius; for (int p=0; p")); return result.elements(); } /** * 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("-N"); result.add("" + m_filterType); return (String[]) result.toArray(new String[result.size()]); } /** * Parses a given list of options.

* * Valid options are:

* *

 -N <num>
   *  Whether to 0=normalize/1=standardize/2=neither. 
   *  (default 0=normalize)
* * * @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 { setDebug(Utils.getFlag('D', options)); String nString = Utils.getOption('N', options); if (nString.length() != 0) { setFilterType(new SelectedTag(Integer.parseInt(nString), TAGS_FILTER)); } else { setFilterType(new SelectedTag(FILTER_NORMALIZE, TAGS_FILTER)); } } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String filterTypeTipText() { return "The filter type for transforming the training data."; } /** * Sets how the training data will be transformed. Should be one of * FILTER_NORMALIZE, FILTER_STANDARDIZE, FILTER_NONE. * * @param newType the new filtering mode */ public void setFilterType(SelectedTag newType) { if (newType.getTags() == TAGS_FILTER) { m_filterType = newType.getSelectedTag().getID(); } } /** * Gets how the training data will be transformed. Will be one of * FILTER_NORMALIZE, FILTER_STANDARDIZE, FILTER_NONE. * * @return the filtering mode */ public SelectedTag getFilterType() { return new SelectedTag(m_filterType, TAGS_FILTER); } /** * Returns the revision string. * * @return the revision */ public String getRevision() { return RevisionUtils.extract("$Revision: 5928 $"); } /** * Main method for testing this class. * * @param argv should contain the command line arguments to the * scheme (see Evaluation) */ public static void main(String[] argv) { runClassifier(new MIOptimalBall(), argv); } }