| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
package weka.classifiers.lazy; |
| 23 | |
|
| 24 | |
import java.util.Enumeration; |
| 25 | |
import java.util.Vector; |
| 26 | |
|
| 27 | |
import weka.classifiers.Classifier; |
| 28 | |
import weka.classifiers.SingleClassifierEnhancer; |
| 29 | |
import weka.classifiers.UpdateableClassifier; |
| 30 | |
import weka.core.Capabilities; |
| 31 | |
import weka.core.Capabilities.Capability; |
| 32 | |
import weka.core.Instance; |
| 33 | |
import weka.core.Instances; |
| 34 | |
import weka.core.Option; |
| 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 | |
import weka.core.WeightedInstancesHandler; |
| 42 | |
import weka.core.neighboursearch.LinearNNSearch; |
| 43 | |
import weka.core.neighboursearch.NearestNeighbourSearch; |
| 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 | |
public class LWL |
| 120 | |
extends SingleClassifierEnhancer |
| 121 | |
implements UpdateableClassifier, WeightedInstancesHandler, |
| 122 | |
TechnicalInformationHandler { |
| 123 | |
|
| 124 | |
|
| 125 | |
static final long serialVersionUID = 1979797405383665815L; |
| 126 | |
|
| 127 | |
|
| 128 | |
protected Instances m_Train; |
| 129 | |
|
| 130 | |
|
| 131 | 0 | protected int m_kNN = -1; |
| 132 | |
|
| 133 | |
|
| 134 | 0 | protected int m_WeightKernel = LINEAR; |
| 135 | |
|
| 136 | |
|
| 137 | 0 | protected boolean m_UseAllK = true; |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | 0 | protected NearestNeighbourSearch m_NNSearch = new LinearNNSearch(); |
| 143 | |
|
| 144 | |
|
| 145 | |
public static final int LINEAR = 0; |
| 146 | |
public static final int EPANECHNIKOV = 1; |
| 147 | |
public static final int TRICUBE = 2; |
| 148 | |
public static final int INVERSE = 3; |
| 149 | |
public static final int GAUSS = 4; |
| 150 | |
public static final int CONSTANT = 5; |
| 151 | |
|
| 152 | |
|
| 153 | |
protected Classifier m_ZeroR; |
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
public String globalInfo() { |
| 161 | 0 | return |
| 162 | |
"Locally weighted learning. Uses an instance-based algorithm to " |
| 163 | |
+ "assign instance weights which are then used by a specified " |
| 164 | |
+ "WeightedInstancesHandler.\n" |
| 165 | |
+ "Can do classification (e.g. using naive Bayes) or regression " |
| 166 | |
+ "(e.g. using linear regression).\n\n" |
| 167 | |
+ "For more info, see\n\n" |
| 168 | |
+ getTechnicalInformation().toString(); |
| 169 | |
} |
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
public TechnicalInformation getTechnicalInformation() { |
| 179 | |
TechnicalInformation result; |
| 180 | |
TechnicalInformation additional; |
| 181 | |
|
| 182 | 0 | result = new TechnicalInformation(Type.INPROCEEDINGS); |
| 183 | 0 | result.setValue(Field.AUTHOR, "Eibe Frank and Mark Hall and Bernhard Pfahringer"); |
| 184 | 0 | result.setValue(Field.YEAR, "2003"); |
| 185 | 0 | result.setValue(Field.TITLE, "Locally Weighted Naive Bayes"); |
| 186 | 0 | result.setValue(Field.BOOKTITLE, "19th Conference in Uncertainty in Artificial Intelligence"); |
| 187 | 0 | result.setValue(Field.PAGES, "249-256"); |
| 188 | 0 | result.setValue(Field.PUBLISHER, "Morgan Kaufmann"); |
| 189 | |
|
| 190 | 0 | additional = result.add(Type.ARTICLE); |
| 191 | 0 | additional.setValue(Field.AUTHOR, "C. Atkeson and A. Moore and S. Schaal"); |
| 192 | 0 | additional.setValue(Field.YEAR, "1996"); |
| 193 | 0 | additional.setValue(Field.TITLE, "Locally weighted learning"); |
| 194 | 0 | additional.setValue(Field.JOURNAL, "AI Review"); |
| 195 | |
|
| 196 | 0 | return result; |
| 197 | |
} |
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | 0 | public LWL() { |
| 203 | 0 | m_Classifier = new weka.classifiers.trees.DecisionStump(); |
| 204 | 0 | } |
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
protected String defaultClassifierString() { |
| 212 | |
|
| 213 | 0 | return "weka.classifiers.trees.DecisionStump"; |
| 214 | |
} |
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
public Enumeration enumerateMeasures() { |
| 222 | 0 | return m_NNSearch.enumerateMeasures(); |
| 223 | |
} |
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
public double getMeasure(String additionalMeasureName) { |
| 233 | 0 | return m_NNSearch.getMeasure(additionalMeasureName); |
| 234 | |
} |
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
public Enumeration listOptions() { |
| 242 | |
|
| 243 | 0 | Vector newVector = new Vector(3); |
| 244 | 0 | newVector.addElement(new Option("\tThe nearest neighbour search " + |
| 245 | |
"algorithm to use " + |
| 246 | |
"(default: weka.core.neighboursearch.LinearNNSearch).\n", |
| 247 | |
"A", 0, "-A")); |
| 248 | 0 | newVector.addElement(new Option("\tSet the number of neighbours used to set" |
| 249 | |
+" the kernel bandwidth.\n" |
| 250 | |
+"\t(default all)", |
| 251 | |
"K", 1, "-K <number of neighbours>")); |
| 252 | 0 | newVector.addElement(new Option("\tSet the weighting kernel shape to use." |
| 253 | |
+" 0=Linear, 1=Epanechnikov,\n" |
| 254 | |
+"\t2=Tricube, 3=Inverse, 4=Gaussian.\n" |
| 255 | |
+"\t(default 0 = Linear)", |
| 256 | |
"U", 1,"-U <number of weighting method>")); |
| 257 | |
|
| 258 | 0 | Enumeration enu = super.listOptions(); |
| 259 | 0 | while (enu.hasMoreElements()) { |
| 260 | 0 | newVector.addElement(enu.nextElement()); |
| 261 | |
} |
| 262 | |
|
| 263 | 0 | return newVector.elements(); |
| 264 | |
} |
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
public void setOptions(String[] options) throws Exception { |
| 307 | |
|
| 308 | 0 | String knnString = Utils.getOption('K', options); |
| 309 | 0 | if (knnString.length() != 0) { |
| 310 | 0 | setKNN(Integer.parseInt(knnString)); |
| 311 | |
} else { |
| 312 | 0 | setKNN(-1); |
| 313 | |
} |
| 314 | |
|
| 315 | 0 | String weightString = Utils.getOption('U', options); |
| 316 | 0 | if (weightString.length() != 0) { |
| 317 | 0 | setWeightingKernel(Integer.parseInt(weightString)); |
| 318 | |
} else { |
| 319 | 0 | setWeightingKernel(LINEAR); |
| 320 | |
} |
| 321 | |
|
| 322 | 0 | String nnSearchClass = Utils.getOption('A', options); |
| 323 | 0 | if(nnSearchClass.length() != 0) { |
| 324 | 0 | String nnSearchClassSpec[] = Utils.splitOptions(nnSearchClass); |
| 325 | 0 | if(nnSearchClassSpec.length == 0) { |
| 326 | 0 | throw new Exception("Invalid NearestNeighbourSearch algorithm " + |
| 327 | |
"specification string."); |
| 328 | |
} |
| 329 | 0 | String className = nnSearchClassSpec[0]; |
| 330 | 0 | nnSearchClassSpec[0] = ""; |
| 331 | |
|
| 332 | 0 | setNearestNeighbourSearchAlgorithm( (NearestNeighbourSearch) |
| 333 | |
Utils.forName( NearestNeighbourSearch.class, |
| 334 | |
className, |
| 335 | |
nnSearchClassSpec) |
| 336 | |
); |
| 337 | 0 | } |
| 338 | |
else |
| 339 | 0 | this.setNearestNeighbourSearchAlgorithm(new LinearNNSearch()); |
| 340 | |
|
| 341 | 0 | super.setOptions(options); |
| 342 | 0 | } |
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
public String [] getOptions() { |
| 350 | |
|
| 351 | 0 | String [] superOptions = super.getOptions(); |
| 352 | 0 | String [] options = new String [superOptions.length + 6]; |
| 353 | |
|
| 354 | 0 | int current = 0; |
| 355 | |
|
| 356 | 0 | options[current++] = "-U"; options[current++] = "" + getWeightingKernel(); |
| 357 | 0 | if ( (getKNN() == 0) && m_UseAllK) { |
| 358 | 0 | options[current++] = "-K"; options[current++] = "-1"; |
| 359 | |
} |
| 360 | |
else { |
| 361 | 0 | options[current++] = "-K"; options[current++] = "" + getKNN(); |
| 362 | |
} |
| 363 | 0 | options[current++] = "-A"; |
| 364 | 0 | options[current++] = m_NNSearch.getClass().getName()+" "+Utils.joinOptions(m_NNSearch.getOptions()); |
| 365 | |
|
| 366 | 0 | System.arraycopy(superOptions, 0, options, current, |
| 367 | |
superOptions.length); |
| 368 | |
|
| 369 | 0 | return options; |
| 370 | |
} |
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | |
|
| 376 | |
|
| 377 | |
public String KNNTipText() { |
| 378 | 0 | return "How many neighbours are used to determine the width of the " |
| 379 | |
+ "weighting function (<= 0 means all neighbours)."; |
| 380 | |
} |
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
public void setKNN(int knn) { |
| 390 | |
|
| 391 | 0 | m_kNN = knn; |
| 392 | 0 | if (knn <= 0) { |
| 393 | 0 | m_kNN = 0; |
| 394 | 0 | m_UseAllK = true; |
| 395 | |
} else { |
| 396 | 0 | m_UseAllK = false; |
| 397 | |
} |
| 398 | 0 | } |
| 399 | |
|
| 400 | |
|
| 401 | |
|
| 402 | |
|
| 403 | |
|
| 404 | |
|
| 405 | |
|
| 406 | |
|
| 407 | |
public int getKNN() { |
| 408 | |
|
| 409 | 0 | return m_kNN; |
| 410 | |
} |
| 411 | |
|
| 412 | |
|
| 413 | |
|
| 414 | |
|
| 415 | |
|
| 416 | |
|
| 417 | |
public String weightingKernelTipText() { |
| 418 | 0 | return "Determines weighting function. [0 = Linear, 1 = Epnechnikov,"+ |
| 419 | |
"2 = Tricube, 3 = Inverse, 4 = Gaussian and 5 = Constant. "+ |
| 420 | |
"(default 0 = Linear)]."; |
| 421 | |
} |
| 422 | |
|
| 423 | |
|
| 424 | |
|
| 425 | |
|
| 426 | |
|
| 427 | |
|
| 428 | |
|
| 429 | |
|
| 430 | |
|
| 431 | |
public void setWeightingKernel(int kernel) { |
| 432 | |
|
| 433 | 0 | if ((kernel != LINEAR) |
| 434 | |
&& (kernel != EPANECHNIKOV) |
| 435 | |
&& (kernel != TRICUBE) |
| 436 | |
&& (kernel != INVERSE) |
| 437 | |
&& (kernel != GAUSS) |
| 438 | |
&& (kernel != CONSTANT)) { |
| 439 | 0 | return; |
| 440 | |
} |
| 441 | 0 | m_WeightKernel = kernel; |
| 442 | 0 | } |
| 443 | |
|
| 444 | |
|
| 445 | |
|
| 446 | |
|
| 447 | |
|
| 448 | |
|
| 449 | |
|
| 450 | |
public int getWeightingKernel() { |
| 451 | |
|
| 452 | 0 | return m_WeightKernel; |
| 453 | |
} |
| 454 | |
|
| 455 | |
|
| 456 | |
|
| 457 | |
|
| 458 | |
|
| 459 | |
|
| 460 | |
public String nearestNeighbourSearchAlgorithmTipText() { |
| 461 | 0 | return "The nearest neighbour search algorithm to use (Default: LinearNN)."; |
| 462 | |
} |
| 463 | |
|
| 464 | |
|
| 465 | |
|
| 466 | |
|
| 467 | |
|
| 468 | |
public NearestNeighbourSearch getNearestNeighbourSearchAlgorithm() { |
| 469 | 0 | return m_NNSearch; |
| 470 | |
} |
| 471 | |
|
| 472 | |
|
| 473 | |
|
| 474 | |
|
| 475 | |
|
| 476 | |
|
| 477 | |
public void setNearestNeighbourSearchAlgorithm(NearestNeighbourSearch nearestNeighbourSearchAlgorithm) { |
| 478 | 0 | m_NNSearch = nearestNeighbourSearchAlgorithm; |
| 479 | 0 | } |
| 480 | |
|
| 481 | |
|
| 482 | |
|
| 483 | |
|
| 484 | |
|
| 485 | |
|
| 486 | |
public Capabilities getCapabilities() { |
| 487 | |
Capabilities result; |
| 488 | |
|
| 489 | 0 | if (m_Classifier != null) { |
| 490 | 0 | result = m_Classifier.getCapabilities(); |
| 491 | |
} else { |
| 492 | 0 | result = super.getCapabilities(); |
| 493 | |
} |
| 494 | |
|
| 495 | 0 | result.setMinimumNumberInstances(0); |
| 496 | |
|
| 497 | |
|
| 498 | 0 | for (Capability cap: Capability.values()) |
| 499 | 0 | result.enableDependency(cap); |
| 500 | |
|
| 501 | 0 | return result; |
| 502 | |
} |
| 503 | |
|
| 504 | |
|
| 505 | |
|
| 506 | |
|
| 507 | |
|
| 508 | |
|
| 509 | |
|
| 510 | |
public void buildClassifier(Instances instances) throws Exception { |
| 511 | |
|
| 512 | 0 | if (!(m_Classifier instanceof WeightedInstancesHandler)) { |
| 513 | 0 | throw new IllegalArgumentException("Classifier must be a " |
| 514 | |
+ "WeightedInstancesHandler!"); |
| 515 | |
} |
| 516 | |
|
| 517 | |
|
| 518 | 0 | getCapabilities().testWithFail(instances); |
| 519 | |
|
| 520 | |
|
| 521 | 0 | instances = new Instances(instances); |
| 522 | 0 | instances.deleteWithMissingClass(); |
| 523 | |
|
| 524 | |
|
| 525 | 0 | if (instances.numAttributes() == 1) { |
| 526 | 0 | System.err.println( |
| 527 | |
"Cannot build model (only class attribute present in data!), " |
| 528 | |
+ "using ZeroR model instead!"); |
| 529 | 0 | m_ZeroR = new weka.classifiers.rules.ZeroR(); |
| 530 | 0 | m_ZeroR.buildClassifier(instances); |
| 531 | 0 | return; |
| 532 | |
} |
| 533 | |
else { |
| 534 | 0 | m_ZeroR = null; |
| 535 | |
} |
| 536 | |
|
| 537 | 0 | m_Train = new Instances(instances, 0, instances.numInstances()); |
| 538 | |
|
| 539 | 0 | m_NNSearch.setInstances(m_Train); |
| 540 | 0 | } |
| 541 | |
|
| 542 | |
|
| 543 | |
|
| 544 | |
|
| 545 | |
|
| 546 | |
|
| 547 | |
|
| 548 | |
|
| 549 | |
public void updateClassifier(Instance instance) throws Exception { |
| 550 | |
|
| 551 | 0 | if (m_Train == null) { |
| 552 | 0 | throw new Exception("No training instance structure set!"); |
| 553 | |
} |
| 554 | 0 | else if (m_Train.equalHeaders(instance.dataset()) == false) { |
| 555 | 0 | throw new Exception("Incompatible instance types\n" + m_Train.equalHeadersMsg(instance.dataset())); |
| 556 | |
} |
| 557 | 0 | if (!instance.classIsMissing()) { |
| 558 | 0 | m_NNSearch.update(instance); |
| 559 | 0 | m_Train.add(instance); |
| 560 | |
} |
| 561 | 0 | } |
| 562 | |
|
| 563 | |
|
| 564 | |
|
| 565 | |
|
| 566 | |
|
| 567 | |
|
| 568 | |
|
| 569 | |
|
| 570 | |
public double[] distributionForInstance(Instance instance) throws Exception { |
| 571 | |
|
| 572 | |
|
| 573 | 0 | if (m_ZeroR != null) { |
| 574 | 0 | return m_ZeroR.distributionForInstance(instance); |
| 575 | |
} |
| 576 | |
|
| 577 | 0 | if (m_Train.numInstances() == 0) { |
| 578 | 0 | throw new Exception("No training instances!"); |
| 579 | |
} |
| 580 | |
|
| 581 | 0 | m_NNSearch.addInstanceInfo(instance); |
| 582 | |
|
| 583 | 0 | int k = m_Train.numInstances(); |
| 584 | 0 | if( (!m_UseAllK && (m_kNN < k)) |
| 585 | |
|
| 586 | |
) { |
| 587 | 0 | k = m_kNN; |
| 588 | |
} |
| 589 | |
|
| 590 | 0 | Instances neighbours = m_NNSearch.kNearestNeighbours(instance, k); |
| 591 | 0 | double distances[] = m_NNSearch.getDistances(); |
| 592 | |
|
| 593 | 0 | if (m_Debug) { |
| 594 | 0 | System.out.println("Test Instance: "+instance); |
| 595 | 0 | System.out.println("For "+k+" kept " + neighbours.numInstances() + " out of " + |
| 596 | |
m_Train.numInstances() + " instances."); |
| 597 | |
} |
| 598 | |
|
| 599 | |
|
| 600 | 0 | if(k>distances.length) |
| 601 | 0 | k = distances.length; |
| 602 | |
|
| 603 | 0 | if (m_Debug) { |
| 604 | 0 | System.out.println("Instance Distances"); |
| 605 | 0 | for (int i = 0; i < distances.length; i++) { |
| 606 | 0 | System.out.println("" + distances[i]); |
| 607 | |
} |
| 608 | |
} |
| 609 | |
|
| 610 | |
|
| 611 | 0 | double bandwidth = distances[k-1]; |
| 612 | |
|
| 613 | |
|
| 614 | 0 | if (bandwidth <= 0) { |
| 615 | |
|
| 616 | 0 | for(int i=0; i < distances.length; i++) |
| 617 | 0 | distances[i] = 1; |
| 618 | |
} else { |
| 619 | |
|
| 620 | 0 | for (int i = 0; i < distances.length; i++) |
| 621 | 0 | distances[i] = distances[i] / bandwidth; |
| 622 | |
} |
| 623 | |
|
| 624 | |
|
| 625 | 0 | for (int i = 0; i < distances.length; i++) { |
| 626 | 0 | switch (m_WeightKernel) { |
| 627 | |
case LINEAR: |
| 628 | 0 | distances[i] = 1.0001 - distances[i]; |
| 629 | 0 | break; |
| 630 | |
case EPANECHNIKOV: |
| 631 | 0 | distances[i] = 3/4D*(1.0001 - distances[i]*distances[i]); |
| 632 | 0 | break; |
| 633 | |
case TRICUBE: |
| 634 | 0 | distances[i] = Math.pow( (1.0001 - Math.pow(distances[i], 3)), 3 ); |
| 635 | 0 | break; |
| 636 | |
case CONSTANT: |
| 637 | |
|
| 638 | 0 | distances[i] = 1; |
| 639 | 0 | break; |
| 640 | |
case INVERSE: |
| 641 | 0 | distances[i] = 1.0 / (1.0 + distances[i]); |
| 642 | 0 | break; |
| 643 | |
case GAUSS: |
| 644 | 0 | distances[i] = Math.exp(-distances[i] * distances[i]); |
| 645 | |
break; |
| 646 | |
} |
| 647 | |
} |
| 648 | |
|
| 649 | 0 | if (m_Debug) { |
| 650 | 0 | System.out.println("Instance Weights"); |
| 651 | 0 | for (int i = 0; i < distances.length; i++) { |
| 652 | 0 | System.out.println("" + distances[i]); |
| 653 | |
} |
| 654 | |
} |
| 655 | |
|
| 656 | |
|
| 657 | 0 | double sumOfWeights = 0, newSumOfWeights = 0; |
| 658 | 0 | for (int i = 0; i < distances.length; i++) { |
| 659 | 0 | double weight = distances[i]; |
| 660 | 0 | Instance inst = (Instance) neighbours.instance(i); |
| 661 | 0 | sumOfWeights += inst.weight(); |
| 662 | 0 | newSumOfWeights += inst.weight() * weight; |
| 663 | 0 | inst.setWeight(inst.weight() * weight); |
| 664 | |
|
| 665 | |
} |
| 666 | |
|
| 667 | |
|
| 668 | 0 | for (int i = 0; i < neighbours.numInstances(); i++) { |
| 669 | 0 | Instance inst = neighbours.instance(i); |
| 670 | 0 | inst.setWeight(inst.weight() * sumOfWeights / newSumOfWeights); |
| 671 | |
} |
| 672 | |
|
| 673 | |
|
| 674 | 0 | m_Classifier.buildClassifier(neighbours); |
| 675 | |
|
| 676 | 0 | if (m_Debug) { |
| 677 | 0 | System.out.println("Classifying test instance: " + instance); |
| 678 | 0 | System.out.println("Built base classifier:\n" |
| 679 | |
+ m_Classifier.toString()); |
| 680 | |
} |
| 681 | |
|
| 682 | |
|
| 683 | 0 | return m_Classifier.distributionForInstance(instance); |
| 684 | |
} |
| 685 | |
|
| 686 | |
|
| 687 | |
|
| 688 | |
|
| 689 | |
|
| 690 | |
|
| 691 | |
public String toString() { |
| 692 | |
|
| 693 | |
|
| 694 | 0 | if (m_ZeroR != null) { |
| 695 | 0 | StringBuffer buf = new StringBuffer(); |
| 696 | 0 | buf.append(this.getClass().getName().replaceAll(".*\\.", "") + "\n"); |
| 697 | 0 | buf.append(this.getClass().getName().replaceAll(".*\\.", "").replaceAll(".", "=") + "\n\n"); |
| 698 | 0 | buf.append("Warning: No model could be built, hence ZeroR model is used:\n\n"); |
| 699 | 0 | buf.append(m_ZeroR.toString()); |
| 700 | 0 | return buf.toString(); |
| 701 | |
} |
| 702 | |
|
| 703 | 0 | if (m_Train == null) { |
| 704 | 0 | return "Locally weighted learning: No model built yet."; |
| 705 | |
} |
| 706 | 0 | String result = "Locally weighted learning\n" |
| 707 | |
+ "===========================\n"; |
| 708 | |
|
| 709 | 0 | result += "Using classifier: " + m_Classifier.getClass().getName() + "\n"; |
| 710 | |
|
| 711 | 0 | switch (m_WeightKernel) { |
| 712 | |
case LINEAR: |
| 713 | 0 | result += "Using linear weighting kernels\n"; |
| 714 | 0 | break; |
| 715 | |
case EPANECHNIKOV: |
| 716 | 0 | result += "Using epanechnikov weighting kernels\n"; |
| 717 | 0 | break; |
| 718 | |
case TRICUBE: |
| 719 | 0 | result += "Using tricube weighting kernels\n"; |
| 720 | 0 | break; |
| 721 | |
case INVERSE: |
| 722 | 0 | result += "Using inverse-distance weighting kernels\n"; |
| 723 | 0 | break; |
| 724 | |
case GAUSS: |
| 725 | 0 | result += "Using gaussian weighting kernels\n"; |
| 726 | 0 | break; |
| 727 | |
case CONSTANT: |
| 728 | 0 | result += "Using constant weighting kernels\n"; |
| 729 | |
break; |
| 730 | |
} |
| 731 | 0 | result += "Using " + (m_UseAllK ? "all" : "" + m_kNN) + " neighbours"; |
| 732 | 0 | return result; |
| 733 | |
} |
| 734 | |
|
| 735 | |
|
| 736 | |
|
| 737 | |
|
| 738 | |
|
| 739 | |
|
| 740 | |
public String getRevision() { |
| 741 | 0 | return RevisionUtils.extract("$Revision: 8034 $"); |
| 742 | |
} |
| 743 | |
|
| 744 | |
|
| 745 | |
|
| 746 | |
|
| 747 | |
|
| 748 | |
|
| 749 | |
public static void main(String [] argv) { |
| 750 | 0 | runClassifier(new LWL(), argv); |
| 751 | 0 | } |
| 752 | |
} |