weka.classifiers.neural.lvq.model.LvqModel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of wekaclassalgos Show documentation
Show all versions of wekaclassalgos Show documentation
Fork of the following defunct sourceforge.net project: https://sourceforge.net/projects/wekaclassalgos/
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package weka.classifiers.neural.lvq.model;
import weka.core.Instance;
/**
* Description: Represents a LVQ model generated using a varient of the LVQ algorithm
* for a dataset
*
*
* Copyright (c) Jason Brownlee 2004
*
*
* @author Jason Brownlee
*/
public class LvqModel extends CommonModel {
public LvqModel(int totalVectors) {
super(totalVectors);
}
public CodebookVector[] get2Bmu(Instance aInstance) {
double[] instance = aInstance.toDoubleArray();
double tmp = 0.0;
double[] distances = new double[2];
CodebookVector[] bmus = new CodebookVector[2];
// set the best as the first codebook
bmus[0] = codebookCollection[0];
distances[0] = distance(instance, codebookCollection[0].getAttributes(), Double.POSITIVE_INFINITY);
// calculate second best
if ((tmp = distance(instance, codebookCollection[1].getAttributes(), distances[0])) < distances[0]) {
// first best becomes second best
bmus[1] = bmus[0];
distances[1] = distances[0];
// second element becomes first best
bmus[0] = codebookCollection[1];
distances[0] = tmp;
}
else {
// second element is second best
bmus[1] = codebookCollection[1];
distances[1] = tmp;
}
// process all codebook vectors
for (int i = 2; i < codebookCollection.length; i++) {
double distance = distance(instance, codebookCollection[i].getAttributes(), distances[1]);
// check if better than second best
if (distance < distances[1]) {
// check if better than the best
if (distance < distances[0]) // new best and new second best
{
// best becomes second best
distances[1] = distances[0];
bmus[1] = bmus[0];
// current becomes the best
distances[0] = distance;
bmus[0] = codebookCollection[i];
}
else // new second best
{
distances[1] = distance;
bmus[1] = codebookCollection[i];
}
}
}
// store the distances in the bmus
bmus[0].setBmuHit(distances[0], aInstance);
bmus[1].setBmuHit(distances[1], aInstance);
return bmus;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy