[4] | 1 | /* |
---|
| 2 | * This program is free software; you can redistribute it and/or modify |
---|
| 3 | * it under the terms of the GNU General Public License as published by |
---|
| 4 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 5 | * (at your option) any later version. |
---|
| 6 | * |
---|
| 7 | * This program is distributed in the hope that it will be useful, |
---|
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 10 | * GNU General Public License for more details. |
---|
| 11 | * |
---|
| 12 | * You should have received a copy of the GNU General Public License |
---|
| 13 | * along with this program; if not, write to the Free Software |
---|
| 14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | /* |
---|
| 18 | * MIEMDD.java |
---|
| 19 | * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package weka.classifiers.mi; |
---|
| 24 | |
---|
| 25 | import weka.classifiers.RandomizableClassifier; |
---|
| 26 | import weka.core.Capabilities; |
---|
| 27 | import weka.core.FastVector; |
---|
| 28 | import weka.core.Instance; |
---|
| 29 | import weka.core.Instances; |
---|
| 30 | import weka.core.MultiInstanceCapabilitiesHandler; |
---|
| 31 | import weka.core.Optimization; |
---|
| 32 | import weka.core.Option; |
---|
| 33 | import weka.core.OptionHandler; |
---|
| 34 | import weka.core.RevisionUtils; |
---|
| 35 | import weka.core.SelectedTag; |
---|
| 36 | import weka.core.Tag; |
---|
| 37 | import weka.core.TechnicalInformation; |
---|
| 38 | import weka.core.TechnicalInformationHandler; |
---|
| 39 | import weka.core.Utils; |
---|
| 40 | import weka.core.Capabilities.Capability; |
---|
| 41 | import weka.core.TechnicalInformation.Field; |
---|
| 42 | import weka.core.TechnicalInformation.Type; |
---|
| 43 | import weka.filters.Filter; |
---|
| 44 | import weka.filters.unsupervised.attribute.Normalize; |
---|
| 45 | import weka.filters.unsupervised.attribute.ReplaceMissingValues; |
---|
| 46 | import weka.filters.unsupervised.attribute.Standardize; |
---|
| 47 | |
---|
| 48 | import java.util.Enumeration; |
---|
| 49 | import java.util.Random; |
---|
| 50 | import java.util.Vector; |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | <!-- globalinfo-start --> |
---|
| 54 | * EMDD model builds heavily upon Dietterich's Diverse Density (DD) algorithm.<br/> |
---|
| 55 | * It is a general framework for MI learning of converting the MI problem to a single-instance setting using EM. In this implementation, we use most-likely cause DD model and only use 3 random selected postive bags as initial starting points of EM.<br/> |
---|
| 56 | * <br/> |
---|
| 57 | * For more information see:<br/> |
---|
| 58 | * <br/> |
---|
| 59 | * Qi Zhang, Sally A. Goldman: EM-DD: An Improved Multiple-Instance Learning Technique. In: Advances in Neural Information Processing Systems 14, 1073-108, 2001. |
---|
| 60 | * <p/> |
---|
| 61 | <!-- globalinfo-end --> |
---|
| 62 | * |
---|
| 63 | <!-- technical-bibtex-start --> |
---|
| 64 | * BibTeX: |
---|
| 65 | * <pre> |
---|
| 66 | * @inproceedings{Zhang2001, |
---|
| 67 | * author = {Qi Zhang and Sally A. Goldman}, |
---|
| 68 | * booktitle = {Advances in Neural Information Processing Systems 14}, |
---|
| 69 | * pages = {1073-108}, |
---|
| 70 | * publisher = {MIT Press}, |
---|
| 71 | * title = {EM-DD: An Improved Multiple-Instance Learning Technique}, |
---|
| 72 | * year = {2001} |
---|
| 73 | * } |
---|
| 74 | * </pre> |
---|
| 75 | * <p/> |
---|
| 76 | <!-- technical-bibtex-end --> |
---|
| 77 | * |
---|
| 78 | <!-- options-start --> |
---|
| 79 | * Valid options are: <p/> |
---|
| 80 | * |
---|
| 81 | * <pre> -N <num> |
---|
| 82 | * Whether to 0=normalize/1=standardize/2=neither. |
---|
| 83 | * (default 1=standardize)</pre> |
---|
| 84 | * |
---|
| 85 | * <pre> -S <num> |
---|
| 86 | * Random number seed. |
---|
| 87 | * (default 1)</pre> |
---|
| 88 | * |
---|
| 89 | * <pre> -D |
---|
| 90 | * If set, classifier is run in debug mode and |
---|
| 91 | * may output additional info to the console</pre> |
---|
| 92 | * |
---|
| 93 | <!-- options-end --> |
---|
| 94 | * |
---|
| 95 | * @author Eibe Frank (eibe@cs.waikato.ac.nz) |
---|
| 96 | * @author Lin Dong (ld21@cs.waikato.ac.nz) |
---|
| 97 | * @version $Revision: 5481 $ |
---|
| 98 | */ |
---|
| 99 | public class MIEMDD |
---|
| 100 | extends RandomizableClassifier |
---|
| 101 | implements OptionHandler, MultiInstanceCapabilitiesHandler, |
---|
| 102 | TechnicalInformationHandler { |
---|
| 103 | |
---|
| 104 | /** for serialization */ |
---|
| 105 | static final long serialVersionUID = 3899547154866223734L; |
---|
| 106 | |
---|
| 107 | /** The index of the class attribute */ |
---|
| 108 | protected int m_ClassIndex; |
---|
| 109 | |
---|
| 110 | protected double[] m_Par; |
---|
| 111 | |
---|
| 112 | /** The number of the class labels */ |
---|
| 113 | protected int m_NumClasses; |
---|
| 114 | |
---|
| 115 | /** Class labels for each bag */ |
---|
| 116 | protected int[] m_Classes; |
---|
| 117 | |
---|
| 118 | /** MI data */ |
---|
| 119 | protected double[][][] m_Data; |
---|
| 120 | |
---|
| 121 | /** All attribute names */ |
---|
| 122 | protected Instances m_Attributes; |
---|
| 123 | |
---|
| 124 | /** MI data */ |
---|
| 125 | protected double[][] m_emData; |
---|
| 126 | |
---|
| 127 | /** The filter used to standardize/normalize all values. */ |
---|
| 128 | protected Filter m_Filter = null; |
---|
| 129 | |
---|
| 130 | /** Whether to normalize/standardize/neither, default:standardize */ |
---|
| 131 | protected int m_filterType = FILTER_STANDARDIZE; |
---|
| 132 | |
---|
| 133 | /** Normalize training data */ |
---|
| 134 | public static final int FILTER_NORMALIZE = 0; |
---|
| 135 | /** Standardize training data */ |
---|
| 136 | public static final int FILTER_STANDARDIZE = 1; |
---|
| 137 | /** No normalization/standardization */ |
---|
| 138 | public static final int FILTER_NONE = 2; |
---|
| 139 | /** The filter to apply to the training data */ |
---|
| 140 | public static final Tag[] TAGS_FILTER = { |
---|
| 141 | new Tag(FILTER_NORMALIZE, "Normalize training data"), |
---|
| 142 | new Tag(FILTER_STANDARDIZE, "Standardize training data"), |
---|
| 143 | new Tag(FILTER_NONE, "No normalization/standardization"), |
---|
| 144 | }; |
---|
| 145 | |
---|
| 146 | /** The filter used to get rid of missing values. */ |
---|
| 147 | protected ReplaceMissingValues m_Missing = new ReplaceMissingValues(); |
---|
| 148 | |
---|
| 149 | /** |
---|
| 150 | * Returns a string describing this filter |
---|
| 151 | * |
---|
| 152 | * @return a description of the filter suitable for |
---|
| 153 | * displaying in the explorer/experimenter gui |
---|
| 154 | */ |
---|
| 155 | public String globalInfo() { |
---|
| 156 | return |
---|
| 157 | "EMDD model builds heavily upon Dietterich's Diverse Density (DD) " |
---|
| 158 | + "algorithm.\nIt is a general framework for MI learning of converting " |
---|
| 159 | + "the MI problem to a single-instance setting using EM. In this " |
---|
| 160 | + "implementation, we use most-likely cause DD model and only use 3 " |
---|
| 161 | + "random selected postive bags as initial starting points of EM.\n\n" |
---|
| 162 | + "For more information see:\n\n" |
---|
| 163 | + getTechnicalInformation().toString(); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | /** |
---|
| 167 | * Returns an instance of a TechnicalInformation object, containing |
---|
| 168 | * detailed information about the technical background of this class, |
---|
| 169 | * e.g., paper reference or book this class is based on. |
---|
| 170 | * |
---|
| 171 | * @return the technical information about this class |
---|
| 172 | */ |
---|
| 173 | public TechnicalInformation getTechnicalInformation() { |
---|
| 174 | TechnicalInformation result; |
---|
| 175 | |
---|
| 176 | result = new TechnicalInformation(Type.INPROCEEDINGS); |
---|
| 177 | result.setValue(Field.AUTHOR, "Qi Zhang and Sally A. Goldman"); |
---|
| 178 | result.setValue(Field.TITLE, "EM-DD: An Improved Multiple-Instance Learning Technique"); |
---|
| 179 | result.setValue(Field.BOOKTITLE, "Advances in Neural Information Processing Systems 14"); |
---|
| 180 | result.setValue(Field.YEAR, "2001"); |
---|
| 181 | result.setValue(Field.PAGES, "1073-108"); |
---|
| 182 | result.setValue(Field.PUBLISHER, "MIT Press"); |
---|
| 183 | |
---|
| 184 | return result; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | /** |
---|
| 188 | * Returns an enumeration describing the available options |
---|
| 189 | * |
---|
| 190 | * @return an enumeration of all the available options |
---|
| 191 | */ |
---|
| 192 | public Enumeration listOptions() { |
---|
| 193 | Vector result = new Vector(); |
---|
| 194 | |
---|
| 195 | result.addElement(new Option( |
---|
| 196 | "\tWhether to 0=normalize/1=standardize/2=neither.\n" |
---|
| 197 | + "\t(default 1=standardize)", |
---|
| 198 | "N", 1, "-N <num>")); |
---|
| 199 | |
---|
| 200 | Enumeration enm = super.listOptions(); |
---|
| 201 | while (enm.hasMoreElements()) |
---|
| 202 | result.addElement(enm.nextElement()); |
---|
| 203 | |
---|
| 204 | return result.elements(); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | /** |
---|
| 208 | * Parses a given list of options. <p/> |
---|
| 209 | * |
---|
| 210 | <!-- options-start --> |
---|
| 211 | * Valid options are: <p/> |
---|
| 212 | * |
---|
| 213 | * <pre> -N <num> |
---|
| 214 | * Whether to 0=normalize/1=standardize/2=neither. |
---|
| 215 | * (default 1=standardize)</pre> |
---|
| 216 | * |
---|
| 217 | * <pre> -S <num> |
---|
| 218 | * Random number seed. |
---|
| 219 | * (default 1)</pre> |
---|
| 220 | * |
---|
| 221 | * <pre> -D |
---|
| 222 | * If set, classifier is run in debug mode and |
---|
| 223 | * may output additional info to the console</pre> |
---|
| 224 | * |
---|
| 225 | <!-- options-end --> |
---|
| 226 | * |
---|
| 227 | * @param options the list of options as an array of strings |
---|
| 228 | * @throws Exception if an option is not supported |
---|
| 229 | */ |
---|
| 230 | public void setOptions(String[] options) throws Exception { |
---|
| 231 | String tmpStr; |
---|
| 232 | |
---|
| 233 | tmpStr = Utils.getOption('N', options); |
---|
| 234 | if (tmpStr.length() != 0) { |
---|
| 235 | setFilterType(new SelectedTag(Integer.parseInt(tmpStr), TAGS_FILTER)); |
---|
| 236 | } else { |
---|
| 237 | setFilterType(new SelectedTag(FILTER_STANDARDIZE, TAGS_FILTER)); |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | super.setOptions(options); |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | |
---|
| 244 | /** |
---|
| 245 | * Gets the current settings of the classifier. |
---|
| 246 | * |
---|
| 247 | * @return an array of strings suitable for passing to setOptions |
---|
| 248 | */ |
---|
| 249 | public String[] getOptions() { |
---|
| 250 | Vector result; |
---|
| 251 | String[] options; |
---|
| 252 | int i; |
---|
| 253 | |
---|
| 254 | result = new Vector(); |
---|
| 255 | options = super.getOptions(); |
---|
| 256 | for (i = 0; i < options.length; i++) |
---|
| 257 | result.add(options[i]); |
---|
| 258 | |
---|
| 259 | result.add("-N"); |
---|
| 260 | result.add("" + m_filterType); |
---|
| 261 | |
---|
| 262 | return (String[]) result.toArray(new String[result.size()]); |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | /** |
---|
| 266 | * Returns the tip text for this property |
---|
| 267 | * |
---|
| 268 | * @return tip text for this property suitable for |
---|
| 269 | * displaying in the explorer/experimenter gui |
---|
| 270 | */ |
---|
| 271 | public String filterTypeTipText() { |
---|
| 272 | return "The filter type for transforming the training data."; |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | /** |
---|
| 276 | * Gets how the training data will be transformed. Will be one of |
---|
| 277 | * FILTER_NORMALIZE, FILTER_STANDARDIZE, FILTER_NONE. |
---|
| 278 | * |
---|
| 279 | * @return the filtering mode |
---|
| 280 | */ |
---|
| 281 | public SelectedTag getFilterType() { |
---|
| 282 | return new SelectedTag(m_filterType, TAGS_FILTER); |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | /** |
---|
| 286 | * Sets how the training data will be transformed. Should be one of |
---|
| 287 | * FILTER_NORMALIZE, FILTER_STANDARDIZE, FILTER_NONE. |
---|
| 288 | * |
---|
| 289 | * @param newType the new filtering mode |
---|
| 290 | */ |
---|
| 291 | public void setFilterType(SelectedTag newType) { |
---|
| 292 | |
---|
| 293 | if (newType.getTags() == TAGS_FILTER) { |
---|
| 294 | m_filterType = newType.getSelectedTag().getID(); |
---|
| 295 | } |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | private class OptEng |
---|
| 299 | extends Optimization { |
---|
| 300 | /** |
---|
| 301 | * Evaluate objective function |
---|
| 302 | * @param x the current values of variables |
---|
| 303 | * @return the value of the objective function |
---|
| 304 | */ |
---|
| 305 | protected double objectiveFunction(double[] x){ |
---|
| 306 | double nll = 0; // -LogLikelihood |
---|
| 307 | for (int i=0; i<m_Classes.length; i++){ // ith bag |
---|
| 308 | double ins=0.0; |
---|
| 309 | for (int k=0; k<m_emData[i].length; k++) //attribute index |
---|
| 310 | ins += (m_emData[i][k]-x[k*2])*(m_emData[i][k]-x[k*2])* |
---|
| 311 | x[k*2+1]*x[k*2+1]; |
---|
| 312 | ins = Math.exp(-ins); // Pr. of being positive |
---|
| 313 | |
---|
| 314 | if (m_Classes[i]==1){ |
---|
| 315 | if (ins <= m_Zero) ins = m_Zero; |
---|
| 316 | nll -= Math.log(ins); //bag level -LogLikelihood |
---|
| 317 | } |
---|
| 318 | else{ |
---|
| 319 | ins = 1.0 - ins; //Pr. of being negative |
---|
| 320 | if(ins<=m_Zero) ins=m_Zero; |
---|
| 321 | nll -= Math.log(ins); |
---|
| 322 | } |
---|
| 323 | } |
---|
| 324 | return nll; |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | /** |
---|
| 328 | * Evaluate Jacobian vector |
---|
| 329 | * @param x the current values of variables |
---|
| 330 | * @return the gradient vector |
---|
| 331 | */ |
---|
| 332 | protected double[] evaluateGradient(double[] x){ |
---|
| 333 | double[] grad = new double[x.length]; |
---|
| 334 | for (int i=0; i<m_Classes.length; i++){ // ith bag |
---|
| 335 | double[] numrt = new double[x.length]; |
---|
| 336 | double exp=0.0; |
---|
| 337 | for (int k=0; k<m_emData[i].length; k++) //attr index |
---|
| 338 | exp += (m_emData[i][k]-x[k*2])*(m_emData[i][k]-x[k*2]) |
---|
| 339 | *x[k*2+1]*x[k*2+1]; |
---|
| 340 | exp = Math.exp(-exp); //Pr. of being positive |
---|
| 341 | |
---|
| 342 | //Instance-wise update |
---|
| 343 | for (int p=0; p<m_emData[i].length; p++){ // pth variable |
---|
| 344 | numrt[2*p] = 2.0*(x[2*p]-m_emData[i][p])*x[p*2+1]*x[p*2+1]; |
---|
| 345 | numrt[2*p+1] = 2.0*(x[2*p]-m_emData[i][p])*(x[2*p]-m_emData[i][p]) |
---|
| 346 | *x[p*2+1]; |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | //Bag-wise update |
---|
| 350 | for (int q=0; q<m_emData[i].length; q++){ |
---|
| 351 | if (m_Classes[i] == 1) {//derivation of (-LogLikeliHood) for positive bags |
---|
| 352 | grad[2*q] += numrt[2*q]; |
---|
| 353 | grad[2*q+1] += numrt[2*q+1]; |
---|
| 354 | } |
---|
| 355 | else{ //derivation of (-LogLikeliHood) for negative bags |
---|
| 356 | grad[2*q] -= numrt[2*q]*exp/(1.0-exp); |
---|
| 357 | grad[2*q+1] -= numrt[2*q+1]*exp/(1.0-exp); |
---|
| 358 | } |
---|
| 359 | } |
---|
| 360 | } // one bag |
---|
| 361 | |
---|
| 362 | return grad; |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | /** |
---|
| 366 | * Returns the revision string. |
---|
| 367 | * |
---|
| 368 | * @return the revision |
---|
| 369 | */ |
---|
| 370 | public String getRevision() { |
---|
| 371 | return RevisionUtils.extract("$Revision: 5481 $"); |
---|
| 372 | } |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | /** |
---|
| 376 | * Returns default capabilities of the classifier. |
---|
| 377 | * |
---|
| 378 | * @return the capabilities of this classifier |
---|
| 379 | */ |
---|
| 380 | public Capabilities getCapabilities() { |
---|
| 381 | Capabilities result = super.getCapabilities(); |
---|
| 382 | result.disableAll(); |
---|
| 383 | |
---|
| 384 | // attributes |
---|
| 385 | result.enable(Capability.NOMINAL_ATTRIBUTES); |
---|
| 386 | result.enable(Capability.RELATIONAL_ATTRIBUTES); |
---|
| 387 | result.enable(Capability.MISSING_VALUES); |
---|
| 388 | |
---|
| 389 | // class |
---|
| 390 | result.enable(Capability.BINARY_CLASS); |
---|
| 391 | result.enable(Capability.MISSING_CLASS_VALUES); |
---|
| 392 | |
---|
| 393 | // other |
---|
| 394 | result.enable(Capability.ONLY_MULTIINSTANCE); |
---|
| 395 | |
---|
| 396 | return result; |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | /** |
---|
| 400 | * Returns the capabilities of this multi-instance classifier for the |
---|
| 401 | * relational data. |
---|
| 402 | * |
---|
| 403 | * @return the capabilities of this object |
---|
| 404 | * @see Capabilities |
---|
| 405 | */ |
---|
| 406 | public Capabilities getMultiInstanceCapabilities() { |
---|
| 407 | Capabilities result = super.getCapabilities(); |
---|
| 408 | result.disableAll(); |
---|
| 409 | |
---|
| 410 | // attributes |
---|
| 411 | result.enable(Capability.NOMINAL_ATTRIBUTES); |
---|
| 412 | result.enable(Capability.NUMERIC_ATTRIBUTES); |
---|
| 413 | result.enable(Capability.DATE_ATTRIBUTES); |
---|
| 414 | result.enable(Capability.MISSING_VALUES); |
---|
| 415 | |
---|
| 416 | // class |
---|
| 417 | result.disableAllClasses(); |
---|
| 418 | result.enable(Capability.NO_CLASS); |
---|
| 419 | |
---|
| 420 | return result; |
---|
| 421 | } |
---|
| 422 | |
---|
| 423 | /** |
---|
| 424 | * Builds the classifier |
---|
| 425 | * |
---|
| 426 | * @param train the training data to be used for generating the |
---|
| 427 | * boosted classifier. |
---|
| 428 | * @throws Exception if the classifier could not be built successfully |
---|
| 429 | */ |
---|
| 430 | public void buildClassifier(Instances train) throws Exception { |
---|
| 431 | // can classifier handle the data? |
---|
| 432 | getCapabilities().testWithFail(train); |
---|
| 433 | |
---|
| 434 | // remove instances with missing class |
---|
| 435 | train = new Instances(train); |
---|
| 436 | train.deleteWithMissingClass(); |
---|
| 437 | |
---|
| 438 | m_ClassIndex = train.classIndex(); |
---|
| 439 | m_NumClasses = train.numClasses(); |
---|
| 440 | |
---|
| 441 | int nR = train.attribute(1).relation().numAttributes(); |
---|
| 442 | int nC = train.numInstances(); |
---|
| 443 | int[] bagSize = new int[nC]; |
---|
| 444 | Instances datasets = new Instances(train.attribute(1).relation(), 0); |
---|
| 445 | |
---|
| 446 | m_Data = new double [nC][nR][]; // Data values |
---|
| 447 | m_Classes = new int [nC]; // Class values |
---|
| 448 | m_Attributes = datasets.stringFreeStructure(); |
---|
| 449 | if (m_Debug) { |
---|
| 450 | System.out.println("\n\nExtracting data..."); |
---|
| 451 | } |
---|
| 452 | |
---|
| 453 | for (int h = 0; h < nC; h++) {//h_th bag |
---|
| 454 | Instance current = train.instance(h); |
---|
| 455 | m_Classes[h] = (int)current.classValue(); // Class value starts from 0 |
---|
| 456 | Instances currInsts = current.relationalValue(1); |
---|
| 457 | for (int i = 0; i < currInsts.numInstances(); i++){ |
---|
| 458 | Instance inst = currInsts.instance(i); |
---|
| 459 | datasets.add(inst); |
---|
| 460 | } |
---|
| 461 | |
---|
| 462 | int nI = currInsts.numInstances(); |
---|
| 463 | bagSize[h] = nI; |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | |
---|
| 467 | /* filter the training data */ |
---|
| 468 | if (m_filterType == FILTER_STANDARDIZE) |
---|
| 469 | m_Filter = new Standardize(); |
---|
| 470 | else if (m_filterType == FILTER_NORMALIZE) |
---|
| 471 | m_Filter = new Normalize(); |
---|
| 472 | else |
---|
| 473 | m_Filter = null; |
---|
| 474 | |
---|
| 475 | if (m_Filter != null) { |
---|
| 476 | m_Filter.setInputFormat(datasets); |
---|
| 477 | datasets = Filter.useFilter(datasets, m_Filter); |
---|
| 478 | } |
---|
| 479 | |
---|
| 480 | m_Missing.setInputFormat(datasets); |
---|
| 481 | datasets = Filter.useFilter(datasets, m_Missing); |
---|
| 482 | |
---|
| 483 | int instIndex = 0; |
---|
| 484 | int start = 0; |
---|
| 485 | for (int h = 0; h < nC; h++) { |
---|
| 486 | for (int i = 0; i < datasets.numAttributes(); i++) { |
---|
| 487 | // initialize m_data[][][] |
---|
| 488 | m_Data[h][i] = new double[bagSize[h]]; |
---|
| 489 | instIndex=start; |
---|
| 490 | for (int k = 0; k < bagSize[h]; k++){ |
---|
| 491 | m_Data[h][i][k] = datasets.instance(instIndex).value(i); |
---|
| 492 | instIndex++; |
---|
| 493 | } |
---|
| 494 | } |
---|
| 495 | start=instIndex; |
---|
| 496 | } |
---|
| 497 | |
---|
| 498 | if (m_Debug) { |
---|
| 499 | System.out.println("\n\nIteration History..." ); |
---|
| 500 | } |
---|
| 501 | |
---|
| 502 | m_emData =new double[nC][nR]; |
---|
| 503 | m_Par= new double[2*nR]; |
---|
| 504 | |
---|
| 505 | double[] x = new double[nR*2]; |
---|
| 506 | double[] tmp = new double[x.length]; |
---|
| 507 | double[] pre_x = new double[x.length]; |
---|
| 508 | double[] best_hypothesis = new double[x.length]; |
---|
| 509 | double[][] b = new double[2][x.length]; |
---|
| 510 | |
---|
| 511 | OptEng opt; |
---|
| 512 | double bestnll = Double.MAX_VALUE; |
---|
| 513 | double min_error = Double.MAX_VALUE; |
---|
| 514 | double nll, pre_nll; |
---|
| 515 | int iterationCount; |
---|
| 516 | |
---|
| 517 | |
---|
| 518 | for (int t = 0; t < x.length; t++) { |
---|
| 519 | b[0][t] = Double.NaN; |
---|
| 520 | b[1][t] = Double.NaN; |
---|
| 521 | } |
---|
| 522 | |
---|
| 523 | //random pick 3 positive bags |
---|
| 524 | Random r = new Random(getSeed()); |
---|
| 525 | FastVector index = new FastVector(); |
---|
| 526 | int n1, n2, n3; |
---|
| 527 | do { |
---|
| 528 | n1 = r.nextInt(nC-1); |
---|
| 529 | } while (m_Classes[n1] == 0); |
---|
| 530 | index.addElement(new Integer(n1)); |
---|
| 531 | |
---|
| 532 | do { |
---|
| 533 | n2 = r.nextInt(nC-1); |
---|
| 534 | } while (n2 == n1|| m_Classes[n2] == 0); |
---|
| 535 | index.addElement(new Integer(n2)); |
---|
| 536 | |
---|
| 537 | do { |
---|
| 538 | n3 = r.nextInt(nC-1); |
---|
| 539 | } while (n3 == n1 || n3 == n2 || m_Classes[n3] == 0); |
---|
| 540 | index.addElement(new Integer(n3)); |
---|
| 541 | |
---|
| 542 | for (int s = 0; s < index.size(); s++){ |
---|
| 543 | int exIdx = ((Integer)index.elementAt(s)).intValue(); |
---|
| 544 | if (m_Debug) |
---|
| 545 | System.out.println("\nH0 at "+exIdx); |
---|
| 546 | |
---|
| 547 | |
---|
| 548 | for (int p = 0; p < m_Data[exIdx][0].length; p++) { |
---|
| 549 | //initialize a hypothesis |
---|
| 550 | for (int q = 0; q < nR; q++) { |
---|
| 551 | x[2 * q] = m_Data[exIdx][q][p]; |
---|
| 552 | x[2 * q + 1] = 1.0; |
---|
| 553 | } |
---|
| 554 | |
---|
| 555 | pre_nll = Double.MAX_VALUE; |
---|
| 556 | nll = Double.MAX_VALUE/10.0; |
---|
| 557 | iterationCount = 0; |
---|
| 558 | //while (Math.abs(nll-pre_nll)>0.01*pre_nll && iterationCount<10) { //stop condition |
---|
| 559 | while (nll < pre_nll && iterationCount < 10) { |
---|
| 560 | iterationCount++; |
---|
| 561 | pre_nll = nll; |
---|
| 562 | |
---|
| 563 | if (m_Debug) |
---|
| 564 | System.out.println("\niteration: "+iterationCount); |
---|
| 565 | |
---|
| 566 | //E-step (find one instance from each bag with max likelihood ) |
---|
| 567 | for (int i = 0; i < m_Data.length; i++) { //for each bag |
---|
| 568 | |
---|
| 569 | int insIndex = findInstance(i, x); |
---|
| 570 | |
---|
| 571 | for (int att = 0; att < m_Data[0].length; att++) //for each attribute |
---|
| 572 | m_emData[i][att] = m_Data[i][att][insIndex]; |
---|
| 573 | } |
---|
| 574 | if (m_Debug) |
---|
| 575 | System.out.println("E-step for new H' finished"); |
---|
| 576 | |
---|
| 577 | //M-step |
---|
| 578 | opt = new OptEng(); |
---|
| 579 | tmp = opt.findArgmin(x, b); |
---|
| 580 | while (tmp == null) { |
---|
| 581 | tmp = opt.getVarbValues(); |
---|
| 582 | if (m_Debug) |
---|
| 583 | System.out.println("200 iterations finished, not enough!"); |
---|
| 584 | tmp = opt.findArgmin(tmp, b); |
---|
| 585 | } |
---|
| 586 | nll = opt.getMinFunction(); |
---|
| 587 | |
---|
| 588 | pre_x = x; |
---|
| 589 | x = tmp; // update hypothesis |
---|
| 590 | |
---|
| 591 | |
---|
| 592 | //keep the track of the best target point which has the minimum nll |
---|
| 593 | /* if (nll < bestnll) { |
---|
| 594 | bestnll = nll; |
---|
| 595 | m_Par = tmp; |
---|
| 596 | if (m_Debug) |
---|
| 597 | System.out.println("!!!!!!!!!!!!!!!!Smaller NLL found: " + nll); |
---|
| 598 | }*/ |
---|
| 599 | |
---|
| 600 | //if (m_Debug) |
---|
| 601 | //System.out.println(exIdx+" "+p+": "+nll+" "+pre_nll+" " +bestnll); |
---|
| 602 | |
---|
| 603 | } //converged for one instance |
---|
| 604 | |
---|
| 605 | //evaluate the hypothesis on the training data and |
---|
| 606 | //keep the track of the hypothesis with minimum error on training data |
---|
| 607 | double distribution[] = new double[2]; |
---|
| 608 | int error = 0; |
---|
| 609 | if (nll > pre_nll) |
---|
| 610 | m_Par = pre_x; |
---|
| 611 | else |
---|
| 612 | m_Par = x; |
---|
| 613 | |
---|
| 614 | for (int i = 0; i<train.numInstances(); i++) { |
---|
| 615 | distribution = distributionForInstance (train.instance(i)); |
---|
| 616 | if (distribution[1] >= 0.5 && m_Classes[i] == 0) |
---|
| 617 | error++; |
---|
| 618 | else if (distribution[1]<0.5 && m_Classes[i] == 1) |
---|
| 619 | error++; |
---|
| 620 | } |
---|
| 621 | if (error < min_error) { |
---|
| 622 | best_hypothesis = m_Par; |
---|
| 623 | min_error = error; |
---|
| 624 | if (nll > pre_nll) |
---|
| 625 | bestnll = pre_nll; |
---|
| 626 | else |
---|
| 627 | bestnll = nll; |
---|
| 628 | if (m_Debug) |
---|
| 629 | System.out.println("error= "+ error +" nll= " + bestnll); |
---|
| 630 | } |
---|
| 631 | } |
---|
| 632 | if (m_Debug) { |
---|
| 633 | System.out.println(exIdx+ ": -------------<Converged>--------------"); |
---|
| 634 | System.out.println("current minimum error= "+min_error+" nll= "+bestnll); |
---|
| 635 | } |
---|
| 636 | } |
---|
| 637 | m_Par = best_hypothesis; |
---|
| 638 | } |
---|
| 639 | |
---|
| 640 | |
---|
| 641 | /** |
---|
| 642 | * given x, find the instance in ith bag with the most likelihood |
---|
| 643 | * probability, which is most likely to responsible for the label of the |
---|
| 644 | * bag For a positive bag, find the instance with the maximal probability |
---|
| 645 | * of being positive For a negative bag, find the instance with the minimal |
---|
| 646 | * probability of being negative |
---|
| 647 | * |
---|
| 648 | * @param i the bag index |
---|
| 649 | * @param x the current values of variables |
---|
| 650 | * @return index of the instance in the bag |
---|
| 651 | */ |
---|
| 652 | protected int findInstance(int i, double[] x){ |
---|
| 653 | |
---|
| 654 | double min=Double.MAX_VALUE; |
---|
| 655 | int insIndex=0; |
---|
| 656 | int nI = m_Data[i][0].length; // numInstances in ith bag |
---|
| 657 | |
---|
| 658 | for (int j=0; j<nI; j++){ |
---|
| 659 | double ins=0.0; |
---|
| 660 | for (int k=0; k<m_Data[i].length; k++) // for each attribute |
---|
| 661 | ins += (m_Data[i][k][j]-x[k*2])*(m_Data[i][k][j]-x[k*2])* |
---|
| 662 | x[k*2+1]*x[k*2+1]; |
---|
| 663 | |
---|
| 664 | //the probability can be calculated as Math.exp(-ins) |
---|
| 665 | //to find the maximum Math.exp(-ins) is equivalent to find the minimum of (ins) |
---|
| 666 | if (ins<min) { |
---|
| 667 | min=ins; |
---|
| 668 | insIndex=j; |
---|
| 669 | } |
---|
| 670 | } |
---|
| 671 | return insIndex; |
---|
| 672 | } |
---|
| 673 | |
---|
| 674 | |
---|
| 675 | /** |
---|
| 676 | * Computes the distribution for a given exemplar |
---|
| 677 | * |
---|
| 678 | * @param exmp the exemplar for which distribution is computed |
---|
| 679 | * @return the distribution |
---|
| 680 | * @throws Exception if the distribution can't be computed successfully |
---|
| 681 | */ |
---|
| 682 | public double[] distributionForInstance(Instance exmp) |
---|
| 683 | throws Exception { |
---|
| 684 | |
---|
| 685 | // Extract the data |
---|
| 686 | Instances ins = exmp.relationalValue(1); |
---|
| 687 | if (m_Filter != null) |
---|
| 688 | ins = Filter.useFilter(ins, m_Filter); |
---|
| 689 | |
---|
| 690 | ins = Filter.useFilter(ins, m_Missing); |
---|
| 691 | |
---|
| 692 | int nI = ins.numInstances(), nA = ins.numAttributes(); |
---|
| 693 | double[][] dat = new double [nI][nA]; |
---|
| 694 | for (int j = 0; j < nI; j++){ |
---|
| 695 | for (int k=0; k<nA; k++){ |
---|
| 696 | dat[j][k] = ins.instance(j).value(k); |
---|
| 697 | } |
---|
| 698 | } |
---|
| 699 | //find the concept instance in the exemplar |
---|
| 700 | double min = Double.MAX_VALUE; |
---|
| 701 | double maxProb = -1.0; |
---|
| 702 | for (int j = 0; j < nI; j++){ |
---|
| 703 | double exp = 0.0; |
---|
| 704 | for (int k = 0; k<nA; k++) // for each attribute |
---|
| 705 | exp += (dat[j][k]-m_Par[k*2])*(dat[j][k]-m_Par[k*2])*m_Par[k*2+1]*m_Par[k*2+1]; |
---|
| 706 | //the probability can be calculated as Math.exp(-exp) |
---|
| 707 | //to find the maximum Math.exp(-exp) is equivalent to find the minimum of (exp) |
---|
| 708 | if (exp < min) { |
---|
| 709 | min = exp; |
---|
| 710 | maxProb = Math.exp(-exp); //maximum probability of being positive |
---|
| 711 | } |
---|
| 712 | } |
---|
| 713 | |
---|
| 714 | // Compute the probability of the bag |
---|
| 715 | double[] distribution = new double[2]; |
---|
| 716 | distribution[1] = maxProb; |
---|
| 717 | distribution[0] = 1.0 - distribution[1]; //mininum prob. of being negative |
---|
| 718 | |
---|
| 719 | return distribution; |
---|
| 720 | } |
---|
| 721 | |
---|
| 722 | |
---|
| 723 | /** |
---|
| 724 | * Gets a string describing the classifier. |
---|
| 725 | * |
---|
| 726 | * @return a string describing the classifer built. |
---|
| 727 | */ |
---|
| 728 | public String toString() { |
---|
| 729 | |
---|
| 730 | String result = "MIEMDD"; |
---|
| 731 | if (m_Par == null) { |
---|
| 732 | return result + ": No model built yet."; |
---|
| 733 | } |
---|
| 734 | |
---|
| 735 | result += "\nCoefficients...\n" |
---|
| 736 | + "Variable Point Scale\n"; |
---|
| 737 | for (int j = 0, idx=0; j < m_Par.length/2; j++, idx++) { |
---|
| 738 | result += m_Attributes.attribute(idx).name(); |
---|
| 739 | result += " "+Utils.doubleToString(m_Par[j*2], 12, 4); |
---|
| 740 | result += " "+Utils.doubleToString(m_Par[j*2+1], 12, 4)+"\n"; |
---|
| 741 | } |
---|
| 742 | |
---|
| 743 | return result; |
---|
| 744 | } |
---|
| 745 | |
---|
| 746 | /** |
---|
| 747 | * Returns the revision string. |
---|
| 748 | * |
---|
| 749 | * @return the revision |
---|
| 750 | */ |
---|
| 751 | public String getRevision() { |
---|
| 752 | return RevisionUtils.extract("$Revision: 5481 $"); |
---|
| 753 | } |
---|
| 754 | |
---|
| 755 | /** |
---|
| 756 | * Main method for testing this class. |
---|
| 757 | * |
---|
| 758 | * @param argv should contain the command line arguments to the |
---|
| 759 | * scheme (see Evaluation) |
---|
| 760 | */ |
---|
| 761 | public static void main(String[] argv) { |
---|
| 762 | runClassifier(new MIEMDD(), argv); |
---|
| 763 | } |
---|
| 764 | } |
---|