weka.clusterers.AbstractClusterer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of weka-dev Show documentation
Show all versions of weka-dev Show documentation
The Waikato Environment for Knowledge Analysis (WEKA), a machine
learning workbench. This version represents the developer version, the
"bleeding edge" of development, you could say. New functionality gets added
to this version.
/*
* 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 .
*/
/*
* AbstractClusterer.java
* Copyright (C) 1999-2012 University of Waikato, Hamilton, New Zealand
*
*/
package weka.clusterers;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.Vector;
import weka.core.Capabilities;
import weka.core.CapabilitiesHandler;
import weka.core.CapabilitiesIgnorer;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.Option;
import weka.core.OptionHandler;
import weka.core.RevisionHandler;
import weka.core.RevisionUtils;
import weka.core.SerializedObject;
import weka.core.Utils;
/**
* Abstract clusterer.
*
* @author Mark Hall ([email protected])
* @version $Revision: 11006 $
*/
public abstract class AbstractClusterer implements Clusterer, Cloneable,
Serializable, CapabilitiesHandler,
RevisionHandler, OptionHandler,
CapabilitiesIgnorer {
/** for serialization */
private static final long serialVersionUID = -6099962589663877632L;
/** Whether the clusterer is run in debug mode. */
protected boolean m_Debug = false;
/** Whether capabilities should not be checked before clusterer is built. */
protected boolean m_DoNotCheckCapabilities = false;
// ===============
// Public methods.
// ===============
/**
* Generates a clusterer. Has to initialize all fields of the clusterer that
* are not being set via options.
*
* @param data set of instances serving as training data
* @exception Exception if the clusterer has not been generated successfully
*/
@Override
public abstract void buildClusterer(Instances data) throws Exception;
/**
* Classifies a given instance. Either this or distributionForInstance() needs
* to be implemented by subclasses.
*
* @param instance the instance to be assigned to a cluster
* @return the number of the assigned cluster as an integer
* @exception Exception if instance could not be clustered successfully
*/
@Override
public int clusterInstance(Instance instance) throws Exception {
double[] dist = distributionForInstance(instance);
if (dist == null) {
throw new Exception("Null distribution predicted");
}
if (Utils.sum(dist) <= 0) {
throw new Exception("Unable to cluster instance");
}
return Utils.maxIndex(dist);
}
/**
* Predicts the cluster memberships for a given instance. Either this or
* clusterInstance() needs to be implemented by subclasses.
*
* @param instance the instance to be assigned a cluster.
* @return an array containing the estimated membership probabilities of the
* test instance in each cluster (this should sum to at most 1)
* @exception Exception if distribution could not be computed successfully
*/
@Override
public double[] distributionForInstance(Instance instance) throws Exception {
double[] d = new double[numberOfClusters()];
d[clusterInstance(instance)] = 1.0;
return d;
}
/**
* Returns the number of clusters.
*
* @return the number of clusters generated for a training dataset.
* @exception Exception if number of clusters could not be returned
* successfully
*/
@Override
public abstract int numberOfClusters() throws Exception;
/**
* Returns an enumeration describing the available options.
*
* @return an enumeration of all the available options.
*/
@Override
public Enumeration
© 2015 - 2024 Weber Informatics LLC | Privacy Policy