All Downloads are FREE. Search and download functionalities are using the official Maven repository.

weka.classifiers.functions.NearestCentroidClassifier Maven / Gradle / Ivy

There is a newer version: 4.0.3
Show newest version
/**
 * 
 */
package weka.classifiers.functions;

import java.util.Collections;
import java.util.Enumeration;
import java.util.Vector;

import weka.classifiers.AbstractClassifier;
import weka.core.Attribute;
import weka.core.Capabilities;
import weka.core.Capabilities.Capability;
import weka.core.DenseInstance;
import weka.core.DistanceFunction;
import weka.core.EuclideanDistance;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.NormalizableDistance;
import weka.core.Option;
import weka.core.RevisionUtils;
import weka.core.Utils;
import weka.core.WeightedInstancesHandler;

/**
 * @author Pawel Trajdos
 *
 */
public class NearestCentroidClassifier extends AbstractClassifier implements WeightedInstancesHandler {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8462836067571523903L;
	
	protected DistanceFunction distFun = null;
	protected Instance[] centroids = null;
	protected boolean[] activeCentroids = null;
	

	/**
	 * 
	 */
	public NearestCentroidClassifier() {
		
		EuclideanDistance tmpDist = new EuclideanDistance();
		tmpDist.setDontNormalize(true);
		this.distFun = tmpDist;

	}

	/**
	 * Builds the classifier.
	 * @param data -- training set
	 */
	@Override
	public void buildClassifier(Instances data) throws Exception {
		this.getCapabilities().testWithFail(data);
		this.distFun.setInstances(data);
		
		int numClasses = data.numClasses();
		int numAttrs = data.numAttributes();
		this.centroids = new Instance[numClasses];
		this.activeCentroids = new boolean[numClasses];
		
		int classIdx = data.classIndex();
		double[][] centroidsDoubles = new double[numClasses][numAttrs];
		double[] classObjCounts = new double[numClasses];
		
		//Initialise centroids
		for(int i=0;i max){
				max = tmp;
				maxIdx = c;
			}
			tmp = Math.exp(-tmp);
			if(this.activeCentroids[c]) {
				distribution[c] = tmp;
				distSum+=tmp;
			}
		}
		boolean err =false;
		
		err = Utils.eq(distSum, 0)? true:false;
		
		if(!err)
		for(int c =0;c listOptions() {
		Vector




© 2015 - 2025 Weber Informatics LLC | Privacy Policy