/* * 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. */ /* * SimpleMI.java * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand */ package weka.classifiers.mi; import weka.classifiers.SingleClassifierEnhancer; import weka.core.Attribute; import weka.core.Capabilities; import weka.core.Instance; import weka.core.DenseInstance; 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.Utils; import weka.core.Capabilities.Capability; import java.util.Enumeration; import java.util.Vector; /** * Reduces MI data into mono-instance data. *
* * Valid options are: * *-M [1|2|3] * The method used in transformation: * 1.arithmatic average; 2.geometric centor; * 3.using minimax combined features of a bag (default: 1) * * Method 3: * Define s to be the vector of the coordinate-wise maxima * and minima of X, ie., * s(X)=(minx1, ..., minxm, maxx1, ...,maxxm), transform * the exemplars into mono-instance which contains attributes * s(X)* *
-D * If set, classifier is run in debug mode and * may output additional info to the console* *
-W * Full name of base classifier. * (default: weka.classifiers.rules.ZeroR)* *
* Options specific to classifier weka.classifiers.rules.ZeroR: ** *
-D * If set, classifier is run in debug mode and * may output additional info to the console* * * @author Eibe Frank (eibe@cs.waikato.ac.nz) * @author Xin Xu (xx5@cs.waikato.ac.nz) * @author Lin Dong (ld21@cs.waikato.ac.nz) * @version $Revision: 5987 $ */ public class SimpleMI extends SingleClassifierEnhancer implements OptionHandler, MultiInstanceCapabilitiesHandler { /** for serialization */ static final long serialVersionUID = 9137795893666592662L; /** arithmetic average */ public static final int TRANSFORMMETHOD_ARITHMETIC = 1; /** geometric average */ public static final int TRANSFORMMETHOD_GEOMETRIC = 2; /** using minimax combined features of a bag */ public static final int TRANSFORMMETHOD_MINIMAX = 3; /** the transformation methods */ public static final Tag[] TAGS_TRANSFORMMETHOD = { new Tag(TRANSFORMMETHOD_ARITHMETIC, "arithmetic average"), new Tag(TRANSFORMMETHOD_GEOMETRIC, "geometric average"), new Tag(TRANSFORMMETHOD_MINIMAX, "using minimax combined features of a bag") }; /** the method used in transformation */ protected int m_TransformMethod = TRANSFORMMETHOD_ARITHMETIC; /** * Returns a string describing this filter * * @return a description of the filter suitable for * displaying in the explorer/experimenter gui */ public String globalInfo() { return "Reduces MI data into mono-instance data."; } /** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options. */ public Enumeration listOptions() { Vector result = new Vector(); result.addElement(new Option( "\tThe method used in transformation:\n" + "\t1.arithmatic average; 2.geometric centor;\n" + "\t3.using minimax combined features of a bag (default: 1)\n\n" + "\tMethod 3:\n" + "\tDefine s to be the vector of the coordinate-wise maxima\n" + "\tand minima of X, ie., \n" + "\ts(X)=(minx1, ..., minxm, maxx1, ...,maxxm), transform\n" + "\tthe exemplars into mono-instance which contains attributes\n" + "\ts(X)", "M", 1, "-M [1|2|3]")); Enumeration enu = super.listOptions(); while (enu.hasMoreElements()) { result.addElement(enu.nextElement()); } return result.elements(); } /** * Parses a given list of options. * * Valid options are: * *
-M [1|2|3] * The method used in transformation: * 1.arithmatic average; 2.geometric centor; * 3.using minimax combined features of a bag (default: 1) * * Method 3: * Define s to be the vector of the coordinate-wise maxima * and minima of X, ie., * s(X)=(minx1, ..., minxm, maxx1, ...,maxxm), transform * the exemplars into mono-instance which contains attributes * s(X)* *
-D * If set, classifier is run in debug mode and * may output additional info to the console* *
-W * Full name of base classifier. * (default: weka.classifiers.rules.ZeroR)* *
* Options specific to classifier weka.classifiers.rules.ZeroR: ** *
-D * If set, classifier is run in debug mode and * may output additional info to the console* * * @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 methodString = Utils.getOption('M', options); if (methodString.length() != 0) { setTransformMethod( new SelectedTag( Integer.parseInt(methodString), TAGS_TRANSFORMMETHOD)); } else { setTransformMethod( new SelectedTag( TRANSFORMMETHOD_ARITHMETIC, TAGS_TRANSFORMMETHOD)); } 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; String[] options; int i; result = new Vector(); result.add("-M"); result.add("" + m_TransformMethod); options = super.getOptions(); for (i = 0; i < options.length; i++) result.add(options[i]); 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 transformMethodTipText() { return "The method used in transformation."; } /** * Set the method used in transformation. * * @param newMethod the index of method to use. */ public void setTransformMethod(SelectedTag newMethod) { if (newMethod.getTags() == TAGS_TRANSFORMMETHOD) m_TransformMethod = newMethod.getSelectedTag().getID(); } /** * Get the method used in transformation. * * @return the index of method used. */ public SelectedTag getTransformMethod() { return new SelectedTag(m_TransformMethod, TAGS_TRANSFORMMETHOD); } /** * Implements MITransform (3 type of transformation) 1.arithmatic average; * 2.geometric centor; 3.merge minima and maxima attribute value together * * @param train the multi-instance dataset (with relational attribute) * @return the transformed dataset with each bag contain mono-instance * (without relational attribute) so that any classifier not for MI dataset * can be applied on it. * @throws Exception if the transformation fails */ public Instances transform(Instances train) throws Exception{ Attribute classAttribute = (Attribute) train.classAttribute().copy(); Attribute bagLabel = (Attribute) train.attribute(0); double labelValue; Instances newData = train.attribute(1).relation().stringFreeStructure(); //insert a bag label attribute at the begining newData.insertAttributeAt(bagLabel, 0); //insert a class attribute at the end newData.insertAttributeAt(classAttribute, newData.numAttributes()); newData.setClassIndex(newData.numAttributes()-1); Instances mini_data = newData.stringFreeStructure(); Instances max_data = newData.stringFreeStructure(); Instance newInst = new DenseInstance(newData.numAttributes()); Instance mini_Inst = new DenseInstance(mini_data.numAttributes()); Instance max_Inst = new DenseInstance(max_data.numAttributes()); newInst.setDataset(newData); mini_Inst.setDataset(mini_data); max_Inst.setDataset(max_data); double N= train.numInstances( );//number of bags for(int i=0; i