| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
package weka.attributeSelection; |
| 23 | |
|
| 24 | |
import java.util.BitSet; |
| 25 | |
import java.util.Enumeration; |
| 26 | |
import java.util.Vector; |
| 27 | |
|
| 28 | |
import weka.core.Instances; |
| 29 | |
import weka.core.Option; |
| 30 | |
import weka.core.OptionHandler; |
| 31 | |
import weka.core.Range; |
| 32 | |
import weka.core.RevisionUtils; |
| 33 | |
import weka.core.Utils; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
public class GreedyStepwise |
| 74 | |
extends ASSearch |
| 75 | |
implements RankedOutputSearch, StartSetHandler, OptionHandler { |
| 76 | |
|
| 77 | |
|
| 78 | |
static final long serialVersionUID = -6312951970168325471L; |
| 79 | |
|
| 80 | |
|
| 81 | |
protected boolean m_hasClass; |
| 82 | |
|
| 83 | |
|
| 84 | |
protected int m_classIndex; |
| 85 | |
|
| 86 | |
|
| 87 | |
protected int m_numAttribs; |
| 88 | |
|
| 89 | |
|
| 90 | |
protected boolean m_rankingRequested; |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
protected boolean m_doRank; |
| 97 | |
|
| 98 | |
|
| 99 | |
protected boolean m_doneRanking; |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
protected double m_threshold; |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | 0 | protected int m_numToSelect = -1; |
| 110 | |
|
| 111 | |
protected int m_calculatedNumToSelect; |
| 112 | |
|
| 113 | |
|
| 114 | |
protected double m_bestMerit; |
| 115 | |
|
| 116 | |
|
| 117 | |
protected double [][] m_rankedAtts; |
| 118 | |
protected int m_rankedSoFar; |
| 119 | |
|
| 120 | |
|
| 121 | |
protected BitSet m_best_group; |
| 122 | |
protected ASEvaluation m_ASEval; |
| 123 | |
|
| 124 | |
protected Instances m_Instances; |
| 125 | |
|
| 126 | |
|
| 127 | |
protected Range m_startRange; |
| 128 | |
|
| 129 | |
|
| 130 | |
protected int [] m_starting; |
| 131 | |
|
| 132 | |
|
| 133 | 0 | protected boolean m_backward = false; |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | 0 | protected boolean m_conservativeSelection = false; |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | 0 | public GreedyStepwise () { |
| 143 | 0 | m_threshold = -Double.MAX_VALUE; |
| 144 | 0 | m_doneRanking = false; |
| 145 | 0 | m_startRange = new Range(); |
| 146 | 0 | m_starting = null; |
| 147 | 0 | resetOptions(); |
| 148 | 0 | } |
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
public String globalInfo() { |
| 156 | 0 | return "GreedyStepwise :\n\nPerforms a greedy forward or backward search " |
| 157 | |
+"through " |
| 158 | |
+"the space of attribute subsets. May start with no/all attributes or from " |
| 159 | |
+"an arbitrary point in the space. Stops when the addition/deletion of any " |
| 160 | |
+"remaining attributes results in a decrease in evaluation. " |
| 161 | |
+"Can also produce a ranked list of " |
| 162 | |
+"attributes by traversing the space from one side to the other and " |
| 163 | |
+"recording the order that attributes are selected.\n"; |
| 164 | |
} |
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
public String searchBackwardsTipText() { |
| 172 | 0 | return "Search backwards rather than forwards."; |
| 173 | |
} |
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
public void setSearchBackwards(boolean back) { |
| 181 | 0 | m_backward = back; |
| 182 | 0 | if (m_backward) { |
| 183 | 0 | setGenerateRanking(false); |
| 184 | |
} |
| 185 | 0 | } |
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
public boolean getSearchBackwards() { |
| 193 | 0 | return m_backward; |
| 194 | |
} |
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
public String thresholdTipText() { |
| 202 | 0 | return "Set threshold by which attributes can be discarded. Default value " |
| 203 | |
+ "results in no attributes being discarded. Use in conjunction with " |
| 204 | |
+ "generateRanking"; |
| 205 | |
} |
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
public void setThreshold(double threshold) { |
| 213 | 0 | m_threshold = threshold; |
| 214 | 0 | } |
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
public double getThreshold() { |
| 221 | 0 | return m_threshold; |
| 222 | |
} |
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
public String numToSelectTipText() { |
| 230 | 0 | return "Specify the number of attributes to retain. The default value " |
| 231 | |
+"(-1) indicates that all attributes are to be retained. Use either " |
| 232 | |
+"this option or a threshold to reduce the attribute set."; |
| 233 | |
} |
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
public void setNumToSelect(int n) { |
| 242 | 0 | m_numToSelect = n; |
| 243 | 0 | } |
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
public int getNumToSelect() { |
| 250 | 0 | return m_numToSelect; |
| 251 | |
} |
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
public int getCalculatedNumToSelect() { |
| 261 | 0 | if (m_numToSelect >= 0) { |
| 262 | 0 | m_calculatedNumToSelect = m_numToSelect; |
| 263 | |
} |
| 264 | 0 | return m_calculatedNumToSelect; |
| 265 | |
} |
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
public String generateRankingTipText() { |
| 273 | 0 | return "Set to true if a ranked list is required."; |
| 274 | |
} |
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
public void setGenerateRanking(boolean doRank) { |
| 281 | 0 | m_rankingRequested = doRank; |
| 282 | 0 | } |
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
public boolean getGenerateRanking() { |
| 291 | 0 | return m_rankingRequested; |
| 292 | |
} |
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
public String startSetTipText() { |
| 300 | 0 | return "Set the start point for the search. This is specified as a comma " |
| 301 | |
+"seperated list off attribute indexes starting at 1. It can include " |
| 302 | |
+"ranges. Eg. 1,2,5-9,17."; |
| 303 | |
} |
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
public void setStartSet (String startSet) throws Exception { |
| 314 | 0 | m_startRange.setRanges(startSet); |
| 315 | 0 | } |
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
public String getStartSet () { |
| 322 | 0 | return m_startRange.getRanges(); |
| 323 | |
} |
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
public String conservativeForwardSelectionTipText() { |
| 331 | 0 | return "If true (and forward search is selected) then attributes " |
| 332 | |
+"will continue to be added to the best subset as long as merit does " |
| 333 | |
+"not degrade."; |
| 334 | |
} |
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
public void setConservativeForwardSelection(boolean c) { |
| 342 | 0 | m_conservativeSelection = c; |
| 343 | 0 | } |
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
public boolean getConservativeForwardSelection() { |
| 350 | 0 | return m_conservativeSelection; |
| 351 | |
} |
| 352 | |
|
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
public Enumeration listOptions () { |
| 358 | 0 | Vector newVector = new Vector(5); |
| 359 | |
|
| 360 | 0 | newVector.addElement(new Option("\tUse conservative forward search" |
| 361 | |
,"-C", 0, "-C")); |
| 362 | |
|
| 363 | 0 | newVector.addElement(new Option("\tUse a backward search instead of a" |
| 364 | |
+"\n\tforward one." |
| 365 | |
,"-B", 0, "-B")); |
| 366 | 0 | newVector |
| 367 | |
.addElement(new Option("\tSpecify a starting set of attributes." |
| 368 | |
+ "\n\tEg. 1,3,5-7." |
| 369 | |
,"P",1 |
| 370 | |
, "-P <start set>")); |
| 371 | |
|
| 372 | 0 | newVector.addElement(new Option("\tProduce a ranked list of attributes." |
| 373 | |
,"R",0,"-R")); |
| 374 | 0 | newVector |
| 375 | |
.addElement(new Option("\tSpecify a theshold by which attributes" |
| 376 | |
+ "\n\tmay be discarded from the ranking." |
| 377 | |
+"\n\tUse in conjuction with -R","T",1 |
| 378 | |
, "-T <threshold>")); |
| 379 | |
|
| 380 | 0 | newVector |
| 381 | |
.addElement(new Option("\tSpecify number of attributes to select" |
| 382 | |
,"N",1 |
| 383 | |
, "-N <num to select>")); |
| 384 | |
|
| 385 | 0 | return newVector.elements(); |
| 386 | |
|
| 387 | |
} |
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
|
| 392 | |
|
| 393 | |
|
| 394 | |
|
| 395 | |
|
| 396 | |
|
| 397 | |
|
| 398 | |
|
| 399 | |
|
| 400 | |
|
| 401 | |
|
| 402 | |
|
| 403 | |
|
| 404 | |
|
| 405 | |
|
| 406 | |
|
| 407 | |
|
| 408 | |
|
| 409 | |
|
| 410 | |
|
| 411 | |
|
| 412 | |
|
| 413 | |
|
| 414 | |
|
| 415 | |
|
| 416 | |
|
| 417 | |
|
| 418 | |
|
| 419 | |
|
| 420 | |
|
| 421 | |
|
| 422 | |
public void setOptions (String[] options) |
| 423 | |
throws Exception { |
| 424 | |
String optionString; |
| 425 | 0 | resetOptions(); |
| 426 | |
|
| 427 | 0 | setSearchBackwards(Utils.getFlag('B', options)); |
| 428 | |
|
| 429 | 0 | setConservativeForwardSelection(Utils.getFlag('C', options)); |
| 430 | |
|
| 431 | 0 | optionString = Utils.getOption('P', options); |
| 432 | 0 | if (optionString.length() != 0) { |
| 433 | 0 | setStartSet(optionString); |
| 434 | |
} |
| 435 | |
|
| 436 | 0 | setGenerateRanking(Utils.getFlag('R', options)); |
| 437 | |
|
| 438 | 0 | optionString = Utils.getOption('T', options); |
| 439 | 0 | if (optionString.length() != 0) { |
| 440 | |
Double temp; |
| 441 | 0 | temp = Double.valueOf(optionString); |
| 442 | 0 | setThreshold(temp.doubleValue()); |
| 443 | |
} |
| 444 | |
|
| 445 | 0 | optionString = Utils.getOption('N', options); |
| 446 | 0 | if (optionString.length() != 0) { |
| 447 | 0 | setNumToSelect(Integer.parseInt(optionString)); |
| 448 | |
} |
| 449 | 0 | } |
| 450 | |
|
| 451 | |
|
| 452 | |
|
| 453 | |
|
| 454 | |
|
| 455 | |
|
| 456 | |
public String[] getOptions () { |
| 457 | 0 | String[] options = new String[9]; |
| 458 | 0 | int current = 0; |
| 459 | |
|
| 460 | 0 | if (getSearchBackwards()) { |
| 461 | 0 | options[current++] = "-B"; |
| 462 | |
} |
| 463 | |
|
| 464 | 0 | if (getConservativeForwardSelection()) { |
| 465 | 0 | options[current++] = "-C"; |
| 466 | |
} |
| 467 | |
|
| 468 | 0 | if (!(getStartSet().equals(""))) { |
| 469 | 0 | options[current++] = "-P"; |
| 470 | 0 | options[current++] = ""+startSetToString(); |
| 471 | |
} |
| 472 | |
|
| 473 | 0 | if (getGenerateRanking()) { |
| 474 | 0 | options[current++] = "-R"; |
| 475 | |
} |
| 476 | 0 | options[current++] = "-T"; |
| 477 | 0 | options[current++] = "" + getThreshold(); |
| 478 | |
|
| 479 | 0 | options[current++] = "-N"; |
| 480 | 0 | options[current++] = ""+getNumToSelect(); |
| 481 | |
|
| 482 | 0 | while (current < options.length) { |
| 483 | 0 | options[current++] = ""; |
| 484 | |
} |
| 485 | 0 | return options; |
| 486 | |
} |
| 487 | |
|
| 488 | |
|
| 489 | |
|
| 490 | |
|
| 491 | |
|
| 492 | |
|
| 493 | |
|
| 494 | |
|
| 495 | |
|
| 496 | |
|
| 497 | |
protected String startSetToString() { |
| 498 | 0 | StringBuffer FString = new StringBuffer(); |
| 499 | |
boolean didPrint; |
| 500 | |
|
| 501 | 0 | if (m_starting == null) { |
| 502 | 0 | return getStartSet(); |
| 503 | |
} |
| 504 | 0 | for (int i = 0; i < m_starting.length; i++) { |
| 505 | 0 | didPrint = false; |
| 506 | |
|
| 507 | 0 | if ((m_hasClass == false) || |
| 508 | |
(m_hasClass == true && i != m_classIndex)) { |
| 509 | 0 | FString.append((m_starting[i] + 1)); |
| 510 | 0 | didPrint = true; |
| 511 | |
} |
| 512 | |
|
| 513 | 0 | if (i == (m_starting.length - 1)) { |
| 514 | 0 | FString.append(""); |
| 515 | |
} |
| 516 | |
else { |
| 517 | 0 | if (didPrint) { |
| 518 | 0 | FString.append(","); |
| 519 | |
} |
| 520 | |
} |
| 521 | |
} |
| 522 | |
|
| 523 | 0 | return FString.toString(); |
| 524 | |
} |
| 525 | |
|
| 526 | |
|
| 527 | |
|
| 528 | |
|
| 529 | |
|
| 530 | |
public String toString() { |
| 531 | 0 | StringBuffer FString = new StringBuffer(); |
| 532 | 0 | FString.append("\tGreedy Stepwise (" |
| 533 | |
+ ((m_backward) |
| 534 | |
? "backwards)" |
| 535 | |
: "forwards)")+".\n\tStart set: "); |
| 536 | |
|
| 537 | 0 | if (m_starting == null) { |
| 538 | 0 | if (m_backward) { |
| 539 | 0 | FString.append("all attributes\n"); |
| 540 | |
} else { |
| 541 | 0 | FString.append("no attributes\n"); |
| 542 | |
} |
| 543 | |
} |
| 544 | |
else { |
| 545 | 0 | FString.append(startSetToString()+"\n"); |
| 546 | |
} |
| 547 | 0 | if (!m_doneRanking) { |
| 548 | 0 | FString.append("\tMerit of best subset found: " |
| 549 | |
+Utils.doubleToString(Math.abs(m_bestMerit),8,3)+"\n"); |
| 550 | |
} else { |
| 551 | 0 | if (m_backward) { |
| 552 | 0 | FString.append("\n\tRanking is the order that attributes were removed, " + |
| 553 | |
"starting \n\twith all attributes. The merit scores in the left" + |
| 554 | |
"\n\tcolumn are the goodness of the remaining attributes in the" + |
| 555 | |
"\n\tsubset after removing the corresponding in the right column" + |
| 556 | |
"\n\tattribute from the subset.\n"); |
| 557 | |
} else { |
| 558 | 0 | FString.append("\n\tRanking is the order that attributes were added, starting " + |
| 559 | |
"\n\twith no attributes. The merit scores in the left column" + |
| 560 | |
"\n\tare the goodness of the subset after the adding the" + |
| 561 | |
"\n\tcorresponding attribute in the right column to the subset.\n"); |
| 562 | |
} |
| 563 | |
} |
| 564 | |
|
| 565 | 0 | if ((m_threshold != -Double.MAX_VALUE) && (m_doneRanking)) { |
| 566 | 0 | FString.append("\tThreshold for discarding attributes: " |
| 567 | |
+ Utils.doubleToString(m_threshold,8,4)+"\n"); |
| 568 | |
} |
| 569 | |
|
| 570 | 0 | return FString.toString(); |
| 571 | |
} |
| 572 | |
|
| 573 | |
|
| 574 | |
|
| 575 | |
|
| 576 | |
|
| 577 | |
|
| 578 | |
|
| 579 | |
|
| 580 | |
|
| 581 | |
|
| 582 | |
public int[] search (ASEvaluation ASEval, Instances data) |
| 583 | |
throws Exception { |
| 584 | |
|
| 585 | |
int i; |
| 586 | 0 | double best_merit = -Double.MAX_VALUE; |
| 587 | |
double temp_best,temp_merit; |
| 588 | 0 | int temp_index=0; |
| 589 | |
BitSet temp_group; |
| 590 | |
|
| 591 | 0 | if (data != null) { |
| 592 | 0 | resetOptions(); |
| 593 | 0 | m_Instances = data; |
| 594 | |
} |
| 595 | 0 | m_ASEval = ASEval; |
| 596 | |
|
| 597 | 0 | m_numAttribs = m_Instances.numAttributes(); |
| 598 | |
|
| 599 | 0 | if (m_best_group == null) { |
| 600 | 0 | m_best_group = new BitSet(m_numAttribs); |
| 601 | |
} |
| 602 | |
|
| 603 | 0 | if (!(m_ASEval instanceof SubsetEvaluator)) { |
| 604 | 0 | throw new Exception(m_ASEval.getClass().getName() |
| 605 | |
+ " is not a " |
| 606 | |
+ "Subset evaluator!"); |
| 607 | |
} |
| 608 | |
|
| 609 | 0 | m_startRange.setUpper(m_numAttribs-1); |
| 610 | 0 | if (!(getStartSet().equals(""))) { |
| 611 | 0 | m_starting = m_startRange.getSelection(); |
| 612 | |
} |
| 613 | |
|
| 614 | 0 | if (m_ASEval instanceof UnsupervisedSubsetEvaluator) { |
| 615 | 0 | m_hasClass = false; |
| 616 | 0 | m_classIndex = -1; |
| 617 | |
} |
| 618 | |
else { |
| 619 | 0 | m_hasClass = true; |
| 620 | 0 | m_classIndex = m_Instances.classIndex(); |
| 621 | |
} |
| 622 | |
|
| 623 | 0 | SubsetEvaluator ASEvaluator = (SubsetEvaluator)m_ASEval; |
| 624 | |
|
| 625 | 0 | if (m_rankedAtts == null) { |
| 626 | 0 | m_rankedAtts = new double[m_numAttribs][2]; |
| 627 | 0 | m_rankedSoFar = 0; |
| 628 | |
} |
| 629 | |
|
| 630 | |
|
| 631 | 0 | if (m_starting != null && m_rankedSoFar <= 0) { |
| 632 | 0 | for (i = 0; i < m_starting.length; i++) { |
| 633 | 0 | if ((m_starting[i]) != m_classIndex) { |
| 634 | 0 | m_best_group.set(m_starting[i]); |
| 635 | |
} |
| 636 | |
} |
| 637 | |
} else { |
| 638 | 0 | if (m_backward && m_rankedSoFar <= 0) { |
| 639 | 0 | for (i = 0; i < m_numAttribs; i++) { |
| 640 | 0 | if (i != m_classIndex) { |
| 641 | 0 | m_best_group.set(i); |
| 642 | |
} |
| 643 | |
} |
| 644 | |
} |
| 645 | |
} |
| 646 | |
|
| 647 | |
|
| 648 | 0 | best_merit = ASEvaluator.evaluateSubset(m_best_group); |
| 649 | |
|
| 650 | |
|
| 651 | 0 | boolean done = false; |
| 652 | 0 | boolean addone = false; |
| 653 | |
boolean z; |
| 654 | 0 | while (!done) { |
| 655 | 0 | temp_group = (BitSet)m_best_group.clone(); |
| 656 | 0 | temp_best = best_merit; |
| 657 | 0 | if (m_doRank) { |
| 658 | 0 | temp_best = -Double.MAX_VALUE; |
| 659 | |
} |
| 660 | 0 | done = true; |
| 661 | 0 | addone = false; |
| 662 | 0 | for (i=0;i<m_numAttribs;i++) { |
| 663 | 0 | if (m_backward) { |
| 664 | 0 | z = ((i != m_classIndex) && (temp_group.get(i))); |
| 665 | |
} else { |
| 666 | 0 | z = ((i != m_classIndex) && (!temp_group.get(i))); |
| 667 | |
} |
| 668 | 0 | if (z) { |
| 669 | |
|
| 670 | 0 | if (m_backward) { |
| 671 | 0 | temp_group.clear(i); |
| 672 | |
} else { |
| 673 | 0 | temp_group.set(i); |
| 674 | |
} |
| 675 | 0 | temp_merit = ASEvaluator.evaluateSubset(temp_group); |
| 676 | 0 | if (m_backward) { |
| 677 | 0 | z = (temp_merit >= temp_best); |
| 678 | |
} else { |
| 679 | 0 | if (m_conservativeSelection) { |
| 680 | 0 | z = (temp_merit >= temp_best); |
| 681 | |
} else { |
| 682 | 0 | z = (temp_merit > temp_best); |
| 683 | |
} |
| 684 | |
} |
| 685 | |
|
| 686 | 0 | if (z) { |
| 687 | 0 | temp_best = temp_merit; |
| 688 | 0 | temp_index = i; |
| 689 | 0 | addone = true; |
| 690 | 0 | done = false; |
| 691 | |
} |
| 692 | |
|
| 693 | |
|
| 694 | 0 | if (m_backward) { |
| 695 | 0 | temp_group.set(i); |
| 696 | |
} else { |
| 697 | 0 | temp_group.clear(i); |
| 698 | |
} |
| 699 | 0 | if (m_doRank) { |
| 700 | 0 | done = false; |
| 701 | |
} |
| 702 | |
} |
| 703 | |
} |
| 704 | 0 | if (addone) { |
| 705 | 0 | if (m_backward) { |
| 706 | 0 | m_best_group.clear(temp_index); |
| 707 | |
} else { |
| 708 | 0 | m_best_group.set(temp_index); |
| 709 | |
} |
| 710 | 0 | best_merit = temp_best; |
| 711 | 0 | m_rankedAtts[m_rankedSoFar][0] = temp_index; |
| 712 | 0 | m_rankedAtts[m_rankedSoFar][1] = best_merit; |
| 713 | 0 | m_rankedSoFar++; |
| 714 | |
} |
| 715 | |
} |
| 716 | 0 | m_bestMerit = best_merit; |
| 717 | 0 | return attributeList(m_best_group); |
| 718 | |
} |
| 719 | |
|
| 720 | |
|
| 721 | |
|
| 722 | |
|
| 723 | |
|
| 724 | |
|
| 725 | |
|
| 726 | |
|
| 727 | |
|
| 728 | |
|
| 729 | |
|
| 730 | |
|
| 731 | |
|
| 732 | |
|
| 733 | |
|
| 734 | |
public double [][] rankedAttributes() throws Exception { |
| 735 | |
|
| 736 | 0 | if (m_rankedAtts == null || m_rankedSoFar == -1) { |
| 737 | 0 | throw new Exception("Search must be performed before attributes " |
| 738 | |
+"can be ranked."); |
| 739 | |
} |
| 740 | |
|
| 741 | 0 | m_doRank = true; |
| 742 | 0 | search (m_ASEval, null); |
| 743 | |
|
| 744 | 0 | double [][] final_rank = new double [m_rankedSoFar][2]; |
| 745 | 0 | for (int i=0;i<m_rankedSoFar;i++) { |
| 746 | 0 | final_rank[i][0] = m_rankedAtts[i][0]; |
| 747 | 0 | final_rank[i][1] = m_rankedAtts[i][1]; |
| 748 | |
} |
| 749 | |
|
| 750 | 0 | resetOptions(); |
| 751 | 0 | m_doneRanking = true; |
| 752 | |
|
| 753 | 0 | if (m_numToSelect > final_rank.length) { |
| 754 | 0 | throw new Exception("More attributes requested than exist in the data"); |
| 755 | |
} |
| 756 | |
|
| 757 | 0 | if (m_numToSelect <= 0) { |
| 758 | 0 | if (m_threshold == -Double.MAX_VALUE) { |
| 759 | 0 | m_calculatedNumToSelect = final_rank.length; |
| 760 | |
} else { |
| 761 | 0 | determineNumToSelectFromThreshold(final_rank); |
| 762 | |
} |
| 763 | |
} |
| 764 | |
|
| 765 | 0 | return final_rank; |
| 766 | |
} |
| 767 | |
|
| 768 | |
private void determineNumToSelectFromThreshold(double [][] ranking) { |
| 769 | 0 | int count = 0; |
| 770 | 0 | for (int i = 0; i < ranking.length; i++) { |
| 771 | 0 | if (ranking[i][1] > m_threshold) { |
| 772 | 0 | count++; |
| 773 | |
} |
| 774 | |
} |
| 775 | 0 | m_calculatedNumToSelect = count; |
| 776 | 0 | } |
| 777 | |
|
| 778 | |
|
| 779 | |
|
| 780 | |
|
| 781 | |
|
| 782 | |
|
| 783 | |
protected int[] attributeList (BitSet group) { |
| 784 | 0 | int count = 0; |
| 785 | |
|
| 786 | |
|
| 787 | 0 | for (int i = 0; i < m_numAttribs; i++) { |
| 788 | 0 | if (group.get(i)) { |
| 789 | 0 | count++; |
| 790 | |
} |
| 791 | |
} |
| 792 | |
|
| 793 | 0 | int[] list = new int[count]; |
| 794 | 0 | count = 0; |
| 795 | |
|
| 796 | 0 | for (int i = 0; i < m_numAttribs; i++) { |
| 797 | 0 | if (group.get(i)) { |
| 798 | 0 | list[count++] = i; |
| 799 | |
} |
| 800 | |
} |
| 801 | |
|
| 802 | 0 | return list; |
| 803 | |
} |
| 804 | |
|
| 805 | |
|
| 806 | |
|
| 807 | |
|
| 808 | |
protected void resetOptions() { |
| 809 | 0 | m_doRank = false; |
| 810 | 0 | m_best_group = null; |
| 811 | 0 | m_ASEval = null; |
| 812 | 0 | m_Instances = null; |
| 813 | 0 | m_rankedSoFar = -1; |
| 814 | 0 | m_rankedAtts = null; |
| 815 | 0 | } |
| 816 | |
|
| 817 | |
|
| 818 | |
|
| 819 | |
|
| 820 | |
|
| 821 | |
|
| 822 | |
public String getRevision() { |
| 823 | 0 | return RevisionUtils.extract("$Revision: 8034 $"); |
| 824 | |
} |
| 825 | |
} |