| 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.Enumeration; |
| 25 | |
import java.util.Random; |
| 26 | |
import java.util.Vector; |
| 27 | |
|
| 28 | |
import weka.core.Attribute; |
| 29 | |
import weka.core.Capabilities; |
| 30 | |
import weka.core.Capabilities.Capability; |
| 31 | |
import weka.core.Instance; |
| 32 | |
import weka.core.Instances; |
| 33 | |
import weka.core.Option; |
| 34 | |
import weka.core.OptionHandler; |
| 35 | |
import weka.core.RevisionUtils; |
| 36 | |
import weka.core.TechnicalInformation; |
| 37 | |
import weka.core.TechnicalInformation.Field; |
| 38 | |
import weka.core.TechnicalInformation.Type; |
| 39 | |
import weka.core.TechnicalInformationHandler; |
| 40 | |
import weka.core.Utils; |
| 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 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
public class ReliefFAttributeEval |
| 130 | |
extends ASEvaluation |
| 131 | |
implements AttributeEvaluator, |
| 132 | |
OptionHandler, |
| 133 | |
TechnicalInformationHandler { |
| 134 | |
|
| 135 | |
|
| 136 | |
static final long serialVersionUID = -8422186665795839379L; |
| 137 | |
|
| 138 | |
|
| 139 | |
private Instances m_trainInstances; |
| 140 | |
|
| 141 | |
|
| 142 | |
private int m_classIndex; |
| 143 | |
|
| 144 | |
|
| 145 | |
private int m_numAttribs; |
| 146 | |
|
| 147 | |
|
| 148 | |
private int m_numInstances; |
| 149 | |
|
| 150 | |
|
| 151 | |
private boolean m_numericClass; |
| 152 | |
|
| 153 | |
|
| 154 | |
private int m_numClasses; |
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
private double m_ndc; |
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
private double[] m_nda; |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
private double[] m_ndcda; |
| 173 | |
|
| 174 | |
|
| 175 | |
private double[] m_weights; |
| 176 | |
|
| 177 | |
|
| 178 | |
private double[] m_classProbs; |
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
private int m_sampleM; |
| 185 | |
|
| 186 | |
|
| 187 | |
private int m_Knn; |
| 188 | |
|
| 189 | |
|
| 190 | |
private double[][][] m_karray; |
| 191 | |
|
| 192 | |
|
| 193 | |
private double[] m_maxArray; |
| 194 | |
|
| 195 | |
|
| 196 | |
private double[] m_minArray; |
| 197 | |
|
| 198 | |
|
| 199 | |
private double[] m_worst; |
| 200 | |
|
| 201 | |
|
| 202 | |
private int[] m_index; |
| 203 | |
|
| 204 | |
|
| 205 | |
private int[] m_stored; |
| 206 | |
|
| 207 | |
|
| 208 | |
private int m_seed; |
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
private double[] m_weightsByRank; |
| 218 | |
private int m_sigma; |
| 219 | |
|
| 220 | |
|
| 221 | |
private boolean m_weightByDistance; |
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | 0 | public ReliefFAttributeEval () { |
| 227 | 0 | resetOptions(); |
| 228 | 0 | } |
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
public String globalInfo() { |
| 236 | 0 | return "ReliefFAttributeEval :\n\nEvaluates the worth of an attribute by " |
| 237 | |
+"repeatedly sampling an instance and considering the value of the " |
| 238 | |
+"given attribute for the nearest instance of the same and different " |
| 239 | |
+"class. Can operate on both discrete and continuous class data.\n\n" |
| 240 | |
+ "For more information see:\n\n" |
| 241 | |
+ getTechnicalInformation().toString(); |
| 242 | |
} |
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
public TechnicalInformation getTechnicalInformation() { |
| 252 | |
TechnicalInformation result; |
| 253 | |
TechnicalInformation additional; |
| 254 | |
|
| 255 | 0 | result = new TechnicalInformation(Type.INPROCEEDINGS); |
| 256 | 0 | result.setValue(Field.AUTHOR, "Kenji Kira and Larry A. Rendell"); |
| 257 | 0 | result.setValue(Field.TITLE, "A Practical Approach to Feature Selection"); |
| 258 | 0 | result.setValue(Field.BOOKTITLE, "Ninth International Workshop on Machine Learning"); |
| 259 | 0 | result.setValue(Field.EDITOR, "Derek H. Sleeman and Peter Edwards"); |
| 260 | 0 | result.setValue(Field.YEAR, "1992"); |
| 261 | 0 | result.setValue(Field.PAGES, "249-256"); |
| 262 | 0 | result.setValue(Field.PUBLISHER, "Morgan Kaufmann"); |
| 263 | |
|
| 264 | 0 | additional = result.add(Type.INPROCEEDINGS); |
| 265 | 0 | additional.setValue(Field.AUTHOR, "Igor Kononenko"); |
| 266 | 0 | additional.setValue(Field.TITLE, "Estimating Attributes: Analysis and Extensions of RELIEF"); |
| 267 | 0 | additional.setValue(Field.BOOKTITLE, "European Conference on Machine Learning"); |
| 268 | 0 | additional.setValue(Field.EDITOR, "Francesco Bergadano and Luc De Raedt"); |
| 269 | 0 | additional.setValue(Field.YEAR, "1994"); |
| 270 | 0 | additional.setValue(Field.PAGES, "171-182"); |
| 271 | 0 | additional.setValue(Field.PUBLISHER, "Springer"); |
| 272 | |
|
| 273 | 0 | additional = result.add(Type.INPROCEEDINGS); |
| 274 | 0 | additional.setValue(Field.AUTHOR, "Marko Robnik-Sikonja and Igor Kononenko"); |
| 275 | 0 | additional.setValue(Field.TITLE, "An adaptation of Relief for attribute estimation in regression"); |
| 276 | 0 | additional.setValue(Field.BOOKTITLE, "Fourteenth International Conference on Machine Learning"); |
| 277 | 0 | additional.setValue(Field.EDITOR, "Douglas H. Fisher"); |
| 278 | 0 | additional.setValue(Field.YEAR, "1997"); |
| 279 | 0 | additional.setValue(Field.PAGES, "296-304"); |
| 280 | 0 | additional.setValue(Field.PUBLISHER, "Morgan Kaufmann"); |
| 281 | |
|
| 282 | 0 | return result; |
| 283 | |
} |
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
public Enumeration listOptions () { |
| 290 | 0 | Vector newVector = new Vector(4); |
| 291 | 0 | newVector |
| 292 | |
.addElement(new Option("\tSpecify the number of instances to\n" |
| 293 | |
+ "\tsample when estimating attributes.\n" |
| 294 | |
+ "\tIf not specified, then all instances\n" |
| 295 | |
+ "\twill be used.", "M", 1 |
| 296 | |
, "-M <num instances>")); |
| 297 | 0 | newVector. |
| 298 | |
addElement(new Option("\tSeed for randomly sampling instances.\n" |
| 299 | |
+ "\t(Default = 1)", "D", 1 |
| 300 | |
, "-D <seed>")); |
| 301 | 0 | newVector. |
| 302 | |
addElement(new Option("\tNumber of nearest neighbours (k) used\n" |
| 303 | |
+ "\tto estimate attribute relevances\n" |
| 304 | |
+ "\t(Default = 10).", "K", 1 |
| 305 | |
, "-K <number of neighbours>")); |
| 306 | 0 | newVector. |
| 307 | |
addElement(new Option("\tWeight nearest neighbours by distance", "W" |
| 308 | |
, 0, "-W")); |
| 309 | 0 | newVector. |
| 310 | |
addElement(new Option("\tSpecify sigma value (used in an exp\n" |
| 311 | |
+ "\tfunction to control how quickly\n" |
| 312 | |
+ "\tweights for more distant instances\n" |
| 313 | |
+ "\tdecrease. Use in conjunction with -W.\n" |
| 314 | |
+ "\tSensible value=1/5 to 1/10 of the\n" |
| 315 | |
+ "\tnumber of nearest neighbours.\n" |
| 316 | |
+ "\t(Default = 2)", "A", 1, "-A <num>")); |
| 317 | 0 | return newVector.elements(); |
| 318 | |
} |
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
public void setOptions (String[] options) |
| 360 | |
throws Exception { |
| 361 | |
String optionString; |
| 362 | 0 | resetOptions(); |
| 363 | 0 | setWeightByDistance(Utils.getFlag('W', options)); |
| 364 | 0 | optionString = Utils.getOption('M', options); |
| 365 | |
|
| 366 | 0 | if (optionString.length() != 0) { |
| 367 | 0 | setSampleSize(Integer.parseInt(optionString)); |
| 368 | |
} |
| 369 | |
|
| 370 | 0 | optionString = Utils.getOption('D', options); |
| 371 | |
|
| 372 | 0 | if (optionString.length() != 0) { |
| 373 | 0 | setSeed(Integer.parseInt(optionString)); |
| 374 | |
} |
| 375 | |
|
| 376 | 0 | optionString = Utils.getOption('K', options); |
| 377 | |
|
| 378 | 0 | if (optionString.length() != 0) { |
| 379 | 0 | setNumNeighbours(Integer.parseInt(optionString)); |
| 380 | |
} |
| 381 | |
|
| 382 | 0 | optionString = Utils.getOption('A', options); |
| 383 | |
|
| 384 | 0 | if (optionString.length() != 0) { |
| 385 | 0 | setWeightByDistance(true); |
| 386 | 0 | setSigma(Integer.parseInt(optionString)); |
| 387 | |
} |
| 388 | 0 | } |
| 389 | |
|
| 390 | |
|
| 391 | |
|
| 392 | |
|
| 393 | |
|
| 394 | |
|
| 395 | |
public String sigmaTipText() { |
| 396 | 0 | return "Set influence of nearest neighbours. Used in an exp function to " |
| 397 | |
+"control how quickly weights decrease for more distant instances. " |
| 398 | |
+"Use in conjunction with weightByDistance. Sensible values = 1/5 to " |
| 399 | |
+"1/10 the number of nearest neighbours."; |
| 400 | |
} |
| 401 | |
|
| 402 | |
|
| 403 | |
|
| 404 | |
|
| 405 | |
|
| 406 | |
|
| 407 | |
|
| 408 | |
public void setSigma (int s) |
| 409 | |
throws Exception { |
| 410 | 0 | if (s <= 0) { |
| 411 | 0 | throw new Exception("value of sigma must be > 0!"); |
| 412 | |
} |
| 413 | |
|
| 414 | 0 | m_sigma = s; |
| 415 | 0 | } |
| 416 | |
|
| 417 | |
|
| 418 | |
|
| 419 | |
|
| 420 | |
|
| 421 | |
|
| 422 | |
|
| 423 | |
public int getSigma () { |
| 424 | 0 | return m_sigma; |
| 425 | |
} |
| 426 | |
|
| 427 | |
|
| 428 | |
|
| 429 | |
|
| 430 | |
|
| 431 | |
|
| 432 | |
public String numNeighboursTipText() { |
| 433 | 0 | return "Number of nearest neighbours for attribute estimation."; |
| 434 | |
} |
| 435 | |
|
| 436 | |
|
| 437 | |
|
| 438 | |
|
| 439 | |
|
| 440 | |
|
| 441 | |
public void setNumNeighbours (int n) { |
| 442 | 0 | m_Knn = n; |
| 443 | 0 | } |
| 444 | |
|
| 445 | |
|
| 446 | |
|
| 447 | |
|
| 448 | |
|
| 449 | |
|
| 450 | |
|
| 451 | |
public int getNumNeighbours () { |
| 452 | 0 | return m_Knn; |
| 453 | |
} |
| 454 | |
|
| 455 | |
|
| 456 | |
|
| 457 | |
|
| 458 | |
|
| 459 | |
|
| 460 | |
public String seedTipText() { |
| 461 | 0 | return "Random seed for sampling instances."; |
| 462 | |
} |
| 463 | |
|
| 464 | |
|
| 465 | |
|
| 466 | |
|
| 467 | |
|
| 468 | |
|
| 469 | |
public void setSeed (int s) { |
| 470 | 0 | m_seed = s; |
| 471 | 0 | } |
| 472 | |
|
| 473 | |
|
| 474 | |
|
| 475 | |
|
| 476 | |
|
| 477 | |
|
| 478 | |
|
| 479 | |
public int getSeed () { |
| 480 | 0 | return m_seed; |
| 481 | |
} |
| 482 | |
|
| 483 | |
|
| 484 | |
|
| 485 | |
|
| 486 | |
|
| 487 | |
|
| 488 | |
public String sampleSizeTipText() { |
| 489 | 0 | return "Number of instances to sample. Default (-1) indicates that all " |
| 490 | |
+"instances will be used for attribute estimation."; |
| 491 | |
} |
| 492 | |
|
| 493 | |
|
| 494 | |
|
| 495 | |
|
| 496 | |
|
| 497 | |
|
| 498 | |
public void setSampleSize (int s) { |
| 499 | 0 | m_sampleM = s; |
| 500 | 0 | } |
| 501 | |
|
| 502 | |
|
| 503 | |
|
| 504 | |
|
| 505 | |
|
| 506 | |
|
| 507 | |
|
| 508 | |
public int getSampleSize () { |
| 509 | 0 | return m_sampleM; |
| 510 | |
} |
| 511 | |
|
| 512 | |
|
| 513 | |
|
| 514 | |
|
| 515 | |
|
| 516 | |
|
| 517 | |
public String weightByDistanceTipText() { |
| 518 | 0 | return "Weight nearest neighbours by their distance."; |
| 519 | |
} |
| 520 | |
|
| 521 | |
|
| 522 | |
|
| 523 | |
|
| 524 | |
|
| 525 | |
|
| 526 | |
public void setWeightByDistance (boolean b) { |
| 527 | 0 | m_weightByDistance = b; |
| 528 | 0 | } |
| 529 | |
|
| 530 | |
|
| 531 | |
|
| 532 | |
|
| 533 | |
|
| 534 | |
|
| 535 | |
|
| 536 | |
public boolean getWeightByDistance () { |
| 537 | 0 | return m_weightByDistance; |
| 538 | |
} |
| 539 | |
|
| 540 | |
|
| 541 | |
|
| 542 | |
|
| 543 | |
|
| 544 | |
|
| 545 | |
|
| 546 | |
public String[] getOptions () { |
| 547 | 0 | String[] options = new String[9]; |
| 548 | 0 | int current = 0; |
| 549 | |
|
| 550 | 0 | if (getWeightByDistance()) { |
| 551 | 0 | options[current++] = "-W"; |
| 552 | |
} |
| 553 | |
|
| 554 | 0 | options[current++] = "-M"; |
| 555 | 0 | options[current++] = "" + getSampleSize(); |
| 556 | 0 | options[current++] = "-D"; |
| 557 | 0 | options[current++] = "" + getSeed(); |
| 558 | 0 | options[current++] = "-K"; |
| 559 | 0 | options[current++] = "" + getNumNeighbours(); |
| 560 | |
|
| 561 | 0 | if (getWeightByDistance()) { |
| 562 | 0 | options[current++] = "-A"; |
| 563 | 0 | options[current++] = "" + getSigma(); |
| 564 | |
} |
| 565 | |
|
| 566 | 0 | while (current < options.length) { |
| 567 | 0 | options[current++] = ""; |
| 568 | |
} |
| 569 | |
|
| 570 | 0 | return options; |
| 571 | |
} |
| 572 | |
|
| 573 | |
|
| 574 | |
|
| 575 | |
|
| 576 | |
|
| 577 | |
|
| 578 | |
|
| 579 | |
public String toString () { |
| 580 | 0 | StringBuffer text = new StringBuffer(); |
| 581 | |
|
| 582 | 0 | if (m_trainInstances == null) { |
| 583 | 0 | text.append("ReliefF feature evaluator has not been built yet\n"); |
| 584 | |
} |
| 585 | |
else { |
| 586 | 0 | text.append("\tReliefF Ranking Filter"); |
| 587 | 0 | text.append("\n\tInstances sampled: "); |
| 588 | |
|
| 589 | 0 | if (m_sampleM == -1) { |
| 590 | 0 | text.append("all\n"); |
| 591 | |
} |
| 592 | |
else { |
| 593 | 0 | text.append(m_sampleM + "\n"); |
| 594 | |
} |
| 595 | |
|
| 596 | 0 | text.append("\tNumber of nearest neighbours (k): " + m_Knn + "\n"); |
| 597 | |
|
| 598 | 0 | if (m_weightByDistance) { |
| 599 | 0 | text.append("\tExponentially decreasing (with distance) " |
| 600 | |
+ "influence for\n" |
| 601 | |
+ "\tnearest neighbours. Sigma: " |
| 602 | |
+ m_sigma + "\n"); |
| 603 | |
} |
| 604 | |
else { |
| 605 | 0 | text.append("\tEqual influence nearest neighbours\n"); |
| 606 | |
} |
| 607 | |
} |
| 608 | |
|
| 609 | 0 | return text.toString(); |
| 610 | |
} |
| 611 | |
|
| 612 | |
|
| 613 | |
|
| 614 | |
|
| 615 | |
|
| 616 | |
|
| 617 | |
|
| 618 | |
public Capabilities getCapabilities() { |
| 619 | 0 | Capabilities result = super.getCapabilities(); |
| 620 | 0 | result.disableAll(); |
| 621 | |
|
| 622 | |
|
| 623 | 0 | result.enable(Capability.NOMINAL_ATTRIBUTES); |
| 624 | 0 | result.enable(Capability.NUMERIC_ATTRIBUTES); |
| 625 | 0 | result.enable(Capability.DATE_ATTRIBUTES); |
| 626 | 0 | result.enable(Capability.MISSING_VALUES); |
| 627 | |
|
| 628 | |
|
| 629 | 0 | result.enable(Capability.NOMINAL_CLASS); |
| 630 | 0 | result.enable(Capability.NUMERIC_CLASS); |
| 631 | 0 | result.enable(Capability.DATE_CLASS); |
| 632 | 0 | result.enable(Capability.MISSING_CLASS_VALUES); |
| 633 | |
|
| 634 | 0 | return result; |
| 635 | |
} |
| 636 | |
|
| 637 | |
|
| 638 | |
|
| 639 | |
|
| 640 | |
|
| 641 | |
|
| 642 | |
|
| 643 | |
|
| 644 | |
public void buildEvaluator (Instances data) |
| 645 | |
throws Exception { |
| 646 | |
|
| 647 | |
int z, totalInstances; |
| 648 | 0 | Random r = new Random(m_seed); |
| 649 | |
|
| 650 | |
|
| 651 | 0 | getCapabilities().testWithFail(data); |
| 652 | |
|
| 653 | 0 | m_trainInstances = data; |
| 654 | 0 | m_classIndex = m_trainInstances.classIndex(); |
| 655 | 0 | m_numAttribs = m_trainInstances.numAttributes(); |
| 656 | 0 | m_numInstances = m_trainInstances.numInstances(); |
| 657 | |
|
| 658 | 0 | if (m_trainInstances.attribute(m_classIndex).isNumeric()) { |
| 659 | 0 | m_numericClass = true; |
| 660 | |
} |
| 661 | |
else { |
| 662 | 0 | m_numericClass = false; |
| 663 | |
} |
| 664 | |
|
| 665 | 0 | if (!m_numericClass) { |
| 666 | 0 | m_numClasses = m_trainInstances.attribute(m_classIndex).numValues(); |
| 667 | |
} |
| 668 | |
else { |
| 669 | 0 | m_ndc = 0; |
| 670 | 0 | m_numClasses = 1; |
| 671 | 0 | m_nda = new double[m_numAttribs]; |
| 672 | 0 | m_ndcda = new double[m_numAttribs]; |
| 673 | |
} |
| 674 | |
|
| 675 | 0 | if (m_weightByDistance) |
| 676 | |
{ |
| 677 | 0 | m_weightsByRank = new double[m_Knn]; |
| 678 | |
|
| 679 | 0 | for (int i = 0; i < m_Knn; i++) { |
| 680 | 0 | m_weightsByRank[i] = |
| 681 | |
Math.exp(-((i/(double)m_sigma)*(i/(double)m_sigma))); |
| 682 | |
} |
| 683 | |
} |
| 684 | |
|
| 685 | |
|
| 686 | 0 | m_weights = new double[m_numAttribs]; |
| 687 | |
|
| 688 | |
|
| 689 | 0 | m_karray = new double[m_numClasses][m_Knn][2]; |
| 690 | |
|
| 691 | 0 | if (!m_numericClass) { |
| 692 | 0 | m_classProbs = new double[m_numClasses]; |
| 693 | |
|
| 694 | 0 | for (int i = 0; i < m_numInstances; i++) { |
| 695 | 0 | m_classProbs[(int)m_trainInstances.instance(i).value(m_classIndex)]++; |
| 696 | |
} |
| 697 | |
|
| 698 | 0 | for (int i = 0; i < m_numClasses; i++) { |
| 699 | 0 | m_classProbs[i] /= m_numInstances; |
| 700 | |
} |
| 701 | |
} |
| 702 | |
|
| 703 | 0 | m_worst = new double[m_numClasses]; |
| 704 | 0 | m_index = new int[m_numClasses]; |
| 705 | 0 | m_stored = new int[m_numClasses]; |
| 706 | 0 | m_minArray = new double[m_numAttribs]; |
| 707 | 0 | m_maxArray = new double[m_numAttribs]; |
| 708 | |
|
| 709 | 0 | for (int i = 0; i < m_numAttribs; i++) { |
| 710 | 0 | m_minArray[i] = m_maxArray[i] = Double.NaN; |
| 711 | |
} |
| 712 | |
|
| 713 | 0 | for (int i = 0; i < m_numInstances; i++) { |
| 714 | 0 | updateMinMax(m_trainInstances.instance(i)); |
| 715 | |
} |
| 716 | |
|
| 717 | 0 | if ((m_sampleM > m_numInstances) || (m_sampleM < 0)) { |
| 718 | 0 | totalInstances = m_numInstances; |
| 719 | |
} |
| 720 | |
else { |
| 721 | 0 | totalInstances = m_sampleM; |
| 722 | |
} |
| 723 | |
|
| 724 | |
|
| 725 | 0 | for (int i = 0; i < totalInstances; i++) { |
| 726 | 0 | if (totalInstances == m_numInstances) { |
| 727 | 0 | z = i; |
| 728 | |
} |
| 729 | |
else { |
| 730 | 0 | z = r.nextInt()%m_numInstances; |
| 731 | |
} |
| 732 | |
|
| 733 | 0 | if (z < 0) { |
| 734 | 0 | z *= -1; |
| 735 | |
} |
| 736 | |
|
| 737 | 0 | if (!(m_trainInstances.instance(z).isMissing(m_classIndex))) { |
| 738 | |
|
| 739 | 0 | for (int j = 0; j < m_numClasses; j++) { |
| 740 | 0 | m_index[j] = m_stored[j] = 0; |
| 741 | |
|
| 742 | 0 | for (int k = 0; k < m_Knn; k++) { |
| 743 | 0 | m_karray[j][k][0] = m_karray[j][k][1] = 0; |
| 744 | |
} |
| 745 | |
} |
| 746 | |
|
| 747 | 0 | findKHitMiss(z); |
| 748 | |
|
| 749 | 0 | if (m_numericClass) { |
| 750 | 0 | updateWeightsNumericClass(z); |
| 751 | |
} |
| 752 | |
else { |
| 753 | 0 | updateWeightsDiscreteClass(z); |
| 754 | |
} |
| 755 | |
} |
| 756 | |
} |
| 757 | |
|
| 758 | |
|
| 759 | |
|
| 760 | |
|
| 761 | 0 | for (int i = 0; i < m_numAttribs; i++) {if (i != m_classIndex) { |
| 762 | 0 | if (m_numericClass) { |
| 763 | 0 | m_weights[i] = m_ndcda[i]/m_ndc - |
| 764 | |
((m_nda[i] - m_ndcda[i])/((double)totalInstances - m_ndc)); |
| 765 | |
} |
| 766 | |
else { |
| 767 | 0 | m_weights[i] *= (1.0/(double)totalInstances); |
| 768 | |
} |
| 769 | |
|
| 770 | |
|
| 771 | |
} |
| 772 | |
} |
| 773 | 0 | } |
| 774 | |
|
| 775 | |
|
| 776 | |
|
| 777 | |
|
| 778 | |
|
| 779 | |
|
| 780 | |
|
| 781 | |
|
| 782 | |
|
| 783 | |
public double evaluateAttribute (int attribute) |
| 784 | |
throws Exception { |
| 785 | 0 | return m_weights[attribute]; |
| 786 | |
} |
| 787 | |
|
| 788 | |
|
| 789 | |
|
| 790 | |
|
| 791 | |
|
| 792 | |
protected void resetOptions () { |
| 793 | 0 | m_trainInstances = null; |
| 794 | 0 | m_sampleM = -1; |
| 795 | 0 | m_Knn = 10; |
| 796 | 0 | m_sigma = 2; |
| 797 | 0 | m_weightByDistance = false; |
| 798 | 0 | m_seed = 1; |
| 799 | 0 | } |
| 800 | |
|
| 801 | |
|
| 802 | |
|
| 803 | |
|
| 804 | |
|
| 805 | |
|
| 806 | |
|
| 807 | |
|
| 808 | |
|
| 809 | |
private double norm (double x, int i) { |
| 810 | 0 | if (Double.isNaN(m_minArray[i]) || |
| 811 | |
Utils.eq(m_maxArray[i], m_minArray[i])) { |
| 812 | 0 | return 0; |
| 813 | |
} |
| 814 | |
else { |
| 815 | 0 | return (x - m_minArray[i])/(m_maxArray[i] - m_minArray[i]); |
| 816 | |
} |
| 817 | |
} |
| 818 | |
|
| 819 | |
|
| 820 | |
|
| 821 | |
|
| 822 | |
|
| 823 | |
|
| 824 | |
|
| 825 | |
|
| 826 | |
private void updateMinMax (Instance instance) { |
| 827 | |
|
| 828 | |
try { |
| 829 | 0 | for (int j = 0; j < instance.numValues(); j++) { |
| 830 | 0 | if ((instance.attributeSparse(j).isNumeric()) && |
| 831 | |
(!instance.isMissingSparse(j))) { |
| 832 | 0 | if (Double.isNaN(m_minArray[instance.index(j)])) { |
| 833 | 0 | m_minArray[instance.index(j)] = instance.valueSparse(j); |
| 834 | 0 | m_maxArray[instance.index(j)] = instance.valueSparse(j); |
| 835 | |
} |
| 836 | |
else { |
| 837 | 0 | if (instance.valueSparse(j) < m_minArray[instance.index(j)]) { |
| 838 | 0 | m_minArray[instance.index(j)] = instance.valueSparse(j); |
| 839 | |
} |
| 840 | |
else { |
| 841 | 0 | if (instance.valueSparse(j) > m_maxArray[instance.index(j)]) { |
| 842 | 0 | m_maxArray[instance.index(j)] = instance.valueSparse(j); |
| 843 | |
} |
| 844 | |
} |
| 845 | |
} |
| 846 | |
} |
| 847 | |
} |
| 848 | 0 | } catch (Exception ex) { |
| 849 | 0 | System.err.println(ex); |
| 850 | 0 | ex.printStackTrace(); |
| 851 | 0 | } |
| 852 | 0 | } |
| 853 | |
|
| 854 | |
|
| 855 | |
|
| 856 | |
|
| 857 | |
|
| 858 | |
private double difference(int index, double val1, double val2) { |
| 859 | |
|
| 860 | 0 | switch (m_trainInstances.attribute(index).type()) { |
| 861 | |
case Attribute.NOMINAL: |
| 862 | |
|
| 863 | |
|
| 864 | 0 | if (Utils.isMissingValue(val1) || |
| 865 | |
Utils.isMissingValue(val2)) { |
| 866 | 0 | return (1.0 - (1.0/((double)m_trainInstances. |
| 867 | |
attribute(index).numValues()))); |
| 868 | 0 | } else if ((int)val1 != (int)val2) { |
| 869 | 0 | return 1; |
| 870 | |
} else { |
| 871 | 0 | return 0; |
| 872 | |
} |
| 873 | |
case Attribute.NUMERIC: |
| 874 | |
|
| 875 | |
|
| 876 | 0 | if (Utils.isMissingValue(val1) || |
| 877 | |
Utils.isMissingValue(val2)) { |
| 878 | 0 | if (Utils.isMissingValue(val1) && |
| 879 | |
Utils.isMissingValue(val2)) { |
| 880 | 0 | return 1; |
| 881 | |
} else { |
| 882 | |
double diff; |
| 883 | 0 | if (Utils.isMissingValue(val2)) { |
| 884 | 0 | diff = norm(val1, index); |
| 885 | |
} else { |
| 886 | 0 | diff = norm(val2, index); |
| 887 | |
} |
| 888 | 0 | if (diff < 0.5) { |
| 889 | 0 | diff = 1.0 - diff; |
| 890 | |
} |
| 891 | 0 | return diff; |
| 892 | |
} |
| 893 | |
} else { |
| 894 | 0 | return Math.abs(norm(val1, index) - norm(val2, index)); |
| 895 | |
} |
| 896 | |
default: |
| 897 | 0 | return 0; |
| 898 | |
} |
| 899 | |
} |
| 900 | |
|
| 901 | |
|
| 902 | |
|
| 903 | |
|
| 904 | |
|
| 905 | |
|
| 906 | |
|
| 907 | |
|
| 908 | |
private double distance(Instance first, Instance second) { |
| 909 | |
|
| 910 | 0 | double distance = 0; |
| 911 | |
int firstI, secondI; |
| 912 | |
|
| 913 | 0 | for (int p1 = 0, p2 = 0; |
| 914 | 0 | p1 < first.numValues() || p2 < second.numValues();) { |
| 915 | 0 | if (p1 >= first.numValues()) { |
| 916 | 0 | firstI = m_trainInstances.numAttributes(); |
| 917 | |
} else { |
| 918 | 0 | firstI = first.index(p1); |
| 919 | |
} |
| 920 | 0 | if (p2 >= second.numValues()) { |
| 921 | 0 | secondI = m_trainInstances.numAttributes(); |
| 922 | |
} else { |
| 923 | 0 | secondI = second.index(p2); |
| 924 | |
} |
| 925 | 0 | if (firstI == m_trainInstances.classIndex()) { |
| 926 | 0 | p1++; continue; |
| 927 | |
} |
| 928 | 0 | if (secondI == m_trainInstances.classIndex()) { |
| 929 | 0 | p2++; continue; |
| 930 | |
} |
| 931 | |
double diff; |
| 932 | 0 | if (firstI == secondI) { |
| 933 | 0 | diff = difference(firstI, |
| 934 | |
first.valueSparse(p1), |
| 935 | |
second.valueSparse(p2)); |
| 936 | 0 | p1++; p2++; |
| 937 | 0 | } else if (firstI > secondI) { |
| 938 | 0 | diff = difference(secondI, |
| 939 | |
0, second.valueSparse(p2)); |
| 940 | 0 | p2++; |
| 941 | |
} else { |
| 942 | 0 | diff = difference(firstI, |
| 943 | |
first.valueSparse(p1), 0); |
| 944 | 0 | p1++; |
| 945 | |
} |
| 946 | |
|
| 947 | 0 | distance += diff; |
| 948 | 0 | } |
| 949 | |
|
| 950 | |
|
| 951 | 0 | return distance; |
| 952 | |
} |
| 953 | |
|
| 954 | |
|
| 955 | |
|
| 956 | |
|
| 957 | |
|
| 958 | |
|
| 959 | |
|
| 960 | |
private void updateWeightsNumericClass (int instNum) { |
| 961 | |
int i, j; |
| 962 | |
double temp,temp2; |
| 963 | 0 | int[] tempSorted = null; |
| 964 | 0 | double[] tempDist = null; |
| 965 | 0 | double distNorm = 1.0; |
| 966 | |
int firstI, secondI; |
| 967 | |
|
| 968 | 0 | Instance inst = m_trainInstances.instance(instNum); |
| 969 | |
|
| 970 | |
|
| 971 | 0 | if (m_weightByDistance) { |
| 972 | 0 | tempDist = new double[m_stored[0]]; |
| 973 | |
|
| 974 | 0 | for (j = 0, distNorm = 0; j < m_stored[0]; j++) { |
| 975 | |
|
| 976 | 0 | tempDist[j] = m_karray[0][j][0]; |
| 977 | |
|
| 978 | 0 | distNorm += m_weightsByRank[j]; |
| 979 | |
} |
| 980 | |
|
| 981 | 0 | tempSorted = Utils.sort(tempDist); |
| 982 | |
} |
| 983 | |
|
| 984 | 0 | for (i = 0; i < m_stored[0]; i++) { |
| 985 | |
|
| 986 | 0 | if (m_weightByDistance) { |
| 987 | 0 | temp = difference(m_classIndex, |
| 988 | |
inst.value(m_classIndex), |
| 989 | |
m_trainInstances. |
| 990 | |
instance((int)m_karray[0][tempSorted[i]][1]). |
| 991 | |
value(m_classIndex)); |
| 992 | 0 | temp *= (m_weightsByRank[i]/distNorm); |
| 993 | |
} |
| 994 | |
else { |
| 995 | 0 | temp = difference(m_classIndex, |
| 996 | |
inst.value(m_classIndex), |
| 997 | |
m_trainInstances. |
| 998 | |
instance((int)m_karray[0][i][1]). |
| 999 | |
value(m_classIndex)); |
| 1000 | 0 | temp *= (1.0/(double)m_stored[0]); |
| 1001 | |
} |
| 1002 | |
|
| 1003 | 0 | m_ndc += temp; |
| 1004 | |
|
| 1005 | |
Instance cmp; |
| 1006 | 0 | cmp = (m_weightByDistance) |
| 1007 | |
? m_trainInstances.instance((int)m_karray[0][tempSorted[i]][1]) |
| 1008 | |
: m_trainInstances.instance((int)m_karray[0][i][1]); |
| 1009 | |
|
| 1010 | 0 | double temp_diffP_diffA_givNearest = |
| 1011 | |
difference(m_classIndex, inst.value(m_classIndex), |
| 1012 | |
cmp.value(m_classIndex)); |
| 1013 | |
|
| 1014 | 0 | for (int p1 = 0, p2 = 0; |
| 1015 | 0 | p1 < inst.numValues() || p2 < cmp.numValues();) { |
| 1016 | 0 | if (p1 >= inst.numValues()) { |
| 1017 | 0 | firstI = m_trainInstances.numAttributes(); |
| 1018 | |
} else { |
| 1019 | 0 | firstI = inst.index(p1); |
| 1020 | |
} |
| 1021 | 0 | if (p2 >= cmp.numValues()) { |
| 1022 | 0 | secondI = m_trainInstances.numAttributes(); |
| 1023 | |
} else { |
| 1024 | 0 | secondI = cmp.index(p2); |
| 1025 | |
} |
| 1026 | 0 | if (firstI == m_trainInstances.classIndex()) { |
| 1027 | 0 | p1++; continue; |
| 1028 | |
} |
| 1029 | 0 | if (secondI == m_trainInstances.classIndex()) { |
| 1030 | 0 | p2++; continue; |
| 1031 | |
} |
| 1032 | 0 | temp = 0.0; |
| 1033 | 0 | temp2 = 0.0; |
| 1034 | |
|
| 1035 | 0 | if (firstI == secondI) { |
| 1036 | 0 | j = firstI; |
| 1037 | 0 | temp = difference(j, inst.valueSparse(p1), cmp.valueSparse(p2)); |
| 1038 | 0 | p1++;p2++; |
| 1039 | 0 | } else if (firstI > secondI) { |
| 1040 | 0 | j = secondI; |
| 1041 | 0 | temp = difference(j, 0, cmp.valueSparse(p2)); |
| 1042 | 0 | p2++; |
| 1043 | |
} else { |
| 1044 | 0 | j = firstI; |
| 1045 | 0 | temp = difference(j, inst.valueSparse(p1), 0); |
| 1046 | 0 | p1++; |
| 1047 | |
} |
| 1048 | |
|
| 1049 | 0 | temp2 = temp_diffP_diffA_givNearest * temp; |
| 1050 | |
|
| 1051 | |
|
| 1052 | 0 | if (m_weightByDistance) { |
| 1053 | 0 | temp2 *= (m_weightsByRank[i]/distNorm); |
| 1054 | |
} |
| 1055 | |
else { |
| 1056 | 0 | temp2 *= (1.0/(double)m_stored[0]); |
| 1057 | |
} |
| 1058 | |
|
| 1059 | 0 | m_ndcda[j] += temp2; |
| 1060 | |
|
| 1061 | |
|
| 1062 | 0 | if (m_weightByDistance) { |
| 1063 | 0 | temp *= (m_weightsByRank[i]/distNorm); |
| 1064 | |
} |
| 1065 | |
else { |
| 1066 | 0 | temp *= (1.0/(double)m_stored[0]); |
| 1067 | |
} |
| 1068 | |
|
| 1069 | 0 | m_nda[j] += temp; |
| 1070 | |
} |
| 1071 | |
} |
| 1072 | 0 | } |
| 1073 | |
|
| 1074 | |
|
| 1075 | |
|
| 1076 | |
|
| 1077 | |
|
| 1078 | |
|
| 1079 | |
|
| 1080 | |
private void updateWeightsDiscreteClass (int instNum) { |
| 1081 | |
int i, j, k; |
| 1082 | |
int cl; |
| 1083 | 0 | double temp_diff, w_norm = 1.0; |
| 1084 | |
double[] tempDistClass; |
| 1085 | 0 | int[] tempSortedClass = null; |
| 1086 | 0 | double distNormClass = 1.0; |
| 1087 | |
double[] tempDistAtt; |
| 1088 | 0 | int[][] tempSortedAtt = null; |
| 1089 | 0 | double[] distNormAtt = null; |
| 1090 | |
int firstI, secondI; |
| 1091 | |
|
| 1092 | |
|
| 1093 | 0 | Instance inst = m_trainInstances.instance(instNum); |
| 1094 | |
|
| 1095 | |
|
| 1096 | 0 | cl = (int)m_trainInstances.instance(instNum).value(m_classIndex); |
| 1097 | |
|
| 1098 | |
|
| 1099 | 0 | if (m_weightByDistance) { |
| 1100 | |
|
| 1101 | |
|
| 1102 | 0 | tempDistClass = new double[m_stored[cl]]; |
| 1103 | |
|
| 1104 | 0 | for (j = 0, distNormClass = 0; j < m_stored[cl]; j++) { |
| 1105 | |
|
| 1106 | 0 | tempDistClass[j] = m_karray[cl][j][0]; |
| 1107 | |
|
| 1108 | 0 | distNormClass += m_weightsByRank[j]; |
| 1109 | |
} |
| 1110 | |
|
| 1111 | 0 | tempSortedClass = Utils.sort(tempDistClass); |
| 1112 | |
|
| 1113 | 0 | tempSortedAtt = new int[m_numClasses][1]; |
| 1114 | 0 | distNormAtt = new double[m_numClasses]; |
| 1115 | |
|
| 1116 | 0 | for (k = 0; k < m_numClasses; k++) { |
| 1117 | 0 | if (k != cl) |
| 1118 | |
{ |
| 1119 | |
|
| 1120 | 0 | tempDistAtt = new double[m_stored[k]]; |
| 1121 | |
|
| 1122 | 0 | for (j = 0, distNormAtt[k] = 0; j < m_stored[k]; j++) { |
| 1123 | |
|
| 1124 | 0 | tempDistAtt[j] = m_karray[k][j][0]; |
| 1125 | |
|
| 1126 | 0 | distNormAtt[k] += m_weightsByRank[j]; |
| 1127 | |
} |
| 1128 | |
|
| 1129 | 0 | tempSortedAtt[k] = Utils.sort(tempDistAtt); |
| 1130 | |
} |
| 1131 | |
} |
| 1132 | |
} |
| 1133 | |
|
| 1134 | 0 | if (m_numClasses > 2) { |
| 1135 | |
|
| 1136 | |
|
| 1137 | 0 | w_norm = (1.0 - m_classProbs[cl]); |
| 1138 | |
} |
| 1139 | |
|
| 1140 | |
|
| 1141 | 0 | for (j = 0, temp_diff = 0.0; j < m_stored[cl]; j++) { |
| 1142 | |
Instance cmp; |
| 1143 | 0 | cmp = (m_weightByDistance) |
| 1144 | |
? m_trainInstances. |
| 1145 | |
instance((int)m_karray[cl][tempSortedClass[j]][1]) |
| 1146 | |
: m_trainInstances.instance((int)m_karray[cl][j][1]); |
| 1147 | |
|
| 1148 | 0 | for (int p1 = 0, p2 = 0; |
| 1149 | 0 | p1 < inst.numValues() || p2 < cmp.numValues();) { |
| 1150 | 0 | if (p1 >= inst.numValues()) { |
| 1151 | 0 | firstI = m_trainInstances.numAttributes(); |
| 1152 | |
} else { |
| 1153 | 0 | firstI = inst.index(p1); |
| 1154 | |
} |
| 1155 | 0 | if (p2 >= cmp.numValues()) { |
| 1156 | 0 | secondI = m_trainInstances.numAttributes(); |
| 1157 | |
} else { |
| 1158 | 0 | secondI = cmp.index(p2); |
| 1159 | |
} |
| 1160 | 0 | if (firstI == m_trainInstances.classIndex()) { |
| 1161 | 0 | p1++; continue; |
| 1162 | |
} |
| 1163 | 0 | if (secondI == m_trainInstances.classIndex()) { |
| 1164 | 0 | p2++; continue; |
| 1165 | |
} |
| 1166 | 0 | if (firstI == secondI) { |
| 1167 | 0 | i = firstI; |
| 1168 | 0 | temp_diff = difference(i, inst.valueSparse(p1), |
| 1169 | |
cmp.valueSparse(p2)); |
| 1170 | 0 | p1++;p2++; |
| 1171 | 0 | } else if (firstI > secondI) { |
| 1172 | 0 | i = secondI; |
| 1173 | 0 | temp_diff = difference(i, 0, cmp.valueSparse(p2)); |
| 1174 | 0 | p2++; |
| 1175 | |
} else { |
| 1176 | 0 | i = firstI; |
| 1177 | 0 | temp_diff = difference(i, inst.valueSparse(p1), 0); |
| 1178 | 0 | p1++; |
| 1179 | |
} |
| 1180 | |
|
| 1181 | 0 | if (m_weightByDistance) { |
| 1182 | 0 | temp_diff *= |
| 1183 | |
(m_weightsByRank[j]/distNormClass); |
| 1184 | |
} else { |
| 1185 | 0 | if (m_stored[cl] > 0) { |
| 1186 | 0 | temp_diff /= (double)m_stored[cl]; |
| 1187 | |
} |
| 1188 | |
} |
| 1189 | 0 | m_weights[i] -= temp_diff; |
| 1190 | |
|
| 1191 | |
} |
| 1192 | |
} |
| 1193 | |
|
| 1194 | |
|
| 1195 | |
|
| 1196 | 0 | temp_diff = 0.0; |
| 1197 | |
|
| 1198 | 0 | for (k = 0; k < m_numClasses; k++) { |
| 1199 | 0 | if (k != cl) |
| 1200 | |
{ |
| 1201 | 0 | for (j = 0; j < m_stored[k]; j++) { |
| 1202 | |
Instance cmp; |
| 1203 | 0 | cmp = (m_weightByDistance) |
| 1204 | |
? m_trainInstances. |
| 1205 | |
instance((int)m_karray[k][tempSortedAtt[k][j]][1]) |
| 1206 | |
: m_trainInstances.instance((int)m_karray[k][j][1]); |
| 1207 | |
|
| 1208 | 0 | for (int p1 = 0, p2 = 0; |
| 1209 | 0 | p1 < inst.numValues() || p2 < cmp.numValues();) { |
| 1210 | 0 | if (p1 >= inst.numValues()) { |
| 1211 | 0 | firstI = m_trainInstances.numAttributes(); |
| 1212 | |
} else { |
| 1213 | 0 | firstI = inst.index(p1); |
| 1214 | |
} |
| 1215 | 0 | if (p2 >= cmp.numValues()) { |
| 1216 | 0 | secondI = m_trainInstances.numAttributes(); |
| 1217 | |
} else { |
| 1218 | 0 | secondI = cmp.index(p2); |
| 1219 | |
} |
| 1220 | 0 | if (firstI == m_trainInstances.classIndex()) { |
| 1221 | 0 | p1++; continue; |
| 1222 | |
} |
| 1223 | 0 | if (secondI == m_trainInstances.classIndex()) { |
| 1224 | 0 | p2++; continue; |
| 1225 | |
} |
| 1226 | 0 | if (firstI == secondI) { |
| 1227 | 0 | i = firstI; |
| 1228 | 0 | temp_diff = difference(i, inst.valueSparse(p1), |
| 1229 | |
cmp.valueSparse(p2)); |
| 1230 | 0 | p1++;p2++; |
| 1231 | 0 | } else if (firstI > secondI) { |
| 1232 | 0 | i = secondI; |
| 1233 | 0 | temp_diff = difference(i, 0, cmp.valueSparse(p2)); |
| 1234 | 0 | p2++; |
| 1235 | |
} else { |
| 1236 | 0 | i = firstI; |
| 1237 | 0 | temp_diff = difference(i, inst.valueSparse(p1), 0); |
| 1238 | 0 | p1++; |
| 1239 | |
} |
| 1240 | |
|
| 1241 | 0 | if (m_weightByDistance) { |
| 1242 | 0 | temp_diff *= |
| 1243 | |
(m_weightsByRank[j]/distNormAtt[k]); |
| 1244 | |
} |
| 1245 | |
else { |
| 1246 | 0 | if (m_stored[k] > 0) { |
| 1247 | 0 | temp_diff /= (double)m_stored[k]; |
| 1248 | |
} |
| 1249 | |
} |
| 1250 | 0 | if (m_numClasses > 2) { |
| 1251 | 0 | m_weights[i] += ((m_classProbs[k]/w_norm)*temp_diff); |
| 1252 | |
} else { |
| 1253 | 0 | m_weights[i] += temp_diff; |
| 1254 | |
} |
| 1255 | |
} |
| 1256 | |
} |
| 1257 | |
} |
| 1258 | |
} |
| 1259 | 0 | } |
| 1260 | |
|
| 1261 | |
|
| 1262 | |
|
| 1263 | |
|
| 1264 | |
|
| 1265 | |
|
| 1266 | |
|
| 1267 | |
|
| 1268 | |
|
| 1269 | |
private void findKHitMiss (int instNum) { |
| 1270 | |
int i, j; |
| 1271 | |
int cl; |
| 1272 | |
double ww; |
| 1273 | 0 | double temp_diff = 0.0; |
| 1274 | 0 | Instance thisInst = m_trainInstances.instance(instNum); |
| 1275 | |
|
| 1276 | 0 | for (i = 0; i < m_numInstances; i++) { |
| 1277 | 0 | if (i != instNum) { |
| 1278 | 0 | Instance cmpInst = m_trainInstances.instance(i); |
| 1279 | 0 | temp_diff = distance(cmpInst, thisInst); |
| 1280 | |
|
| 1281 | |
|
| 1282 | 0 | if (m_numericClass) { |
| 1283 | 0 | cl = 0; |
| 1284 | |
} |
| 1285 | |
else { |
| 1286 | 0 | cl = (int)m_trainInstances.instance(i).value(m_classIndex); |
| 1287 | |
} |
| 1288 | |
|
| 1289 | |
|
| 1290 | 0 | if (m_stored[cl] < m_Knn) { |
| 1291 | 0 | m_karray[cl][m_stored[cl]][0] = temp_diff; |
| 1292 | 0 | m_karray[cl][m_stored[cl]][1] = i; |
| 1293 | 0 | m_stored[cl]++; |
| 1294 | |
|
| 1295 | |
|
| 1296 | 0 | for (j = 0, ww = -1.0; j < m_stored[cl]; j++) { |
| 1297 | 0 | if (m_karray[cl][j][0] > ww) { |
| 1298 | 0 | ww = m_karray[cl][j][0]; |
| 1299 | 0 | m_index[cl] = j; |
| 1300 | |
} |
| 1301 | |
} |
| 1302 | |
|
| 1303 | 0 | m_worst[cl] = ww; |
| 1304 | |
} |
| 1305 | |
else |
| 1306 | |
|
| 1307 | |
|
| 1308 | |
{ |
| 1309 | 0 | if (temp_diff < m_karray[cl][m_index[cl]][0]) { |
| 1310 | 0 | m_karray[cl][m_index[cl]][0] = temp_diff; |
| 1311 | 0 | m_karray[cl][m_index[cl]][1] = i; |
| 1312 | |
|
| 1313 | 0 | for (j = 0, ww = -1.0; j < m_stored[cl]; j++) { |
| 1314 | 0 | if (m_karray[cl][j][0] > ww) { |
| 1315 | 0 | ww = m_karray[cl][j][0]; |
| 1316 | 0 | m_index[cl] = j; |
| 1317 | |
} |
| 1318 | |
} |
| 1319 | |
|
| 1320 | 0 | m_worst[cl] = ww; |
| 1321 | |
} |
| 1322 | |
} |
| 1323 | |
} |
| 1324 | |
} |
| 1325 | 0 | } |
| 1326 | |
|
| 1327 | |
|
| 1328 | |
|
| 1329 | |
|
| 1330 | |
|
| 1331 | |
|
| 1332 | |
public String getRevision() { |
| 1333 | 0 | return RevisionUtils.extract("$Revision: 8034 $"); |
| 1334 | |
} |
| 1335 | |
|
| 1336 | |
|
| 1337 | |
|
| 1338 | |
|
| 1339 | |
|
| 1340 | |
|
| 1341 | |
|
| 1342 | |
|
| 1343 | |
|
| 1344 | |
public static void main (String[] args) { |
| 1345 | 0 | runEvaluator(new ReliefFAttributeEval(), args); |
| 1346 | 0 | } |
| 1347 | |
} |