DistributionClusterer.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 3k
Category:

Windows Develop

Development Platform:

Java

  1. /*
  2.  *    This program is free software; you can redistribute it and/or modify
  3.  *    it under the terms of the GNU General Public License as published by
  4.  *    the Free Software Foundation; either version 2 of the License, or
  5.  *    (at your option) any later version.
  6.  *
  7.  *    This program is distributed in the hope that it will be useful,
  8.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  *    GNU General Public License for more details.
  11.  *
  12.  *    You should have received a copy of the GNU General Public License
  13.  *    along with this program; if not, write to the Free Software
  14.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  */
  16. /*
  17.  *    DistributionClusterer.java
  18.  *    Copyright (C) 1999 Mark Hall
  19.  *
  20.  */
  21. package weka.clusterers;
  22. import weka.core.*;
  23. /** 
  24.  * Abstract clustering model that produces (for each test instance)
  25.  * an estimate of the membership in each cluster 
  26.  * (ie. a probability distribution).
  27.  *
  28.  * @author   Mark Hall (mhall@cs.waikato.ac.nz)
  29.  * @version  $Revision: 1.7 $
  30.  */
  31. public abstract class DistributionClusterer extends Clusterer {
  32.   // ===============
  33.   // Public methods.
  34.   // ===============
  35.   /**
  36.    * Computes the density for a given instance.
  37.    * 
  38.    * @param instance the instance to compute the density for
  39.    * @return the density.
  40.    * @exception Exception if the density could not be computed
  41.    * successfully
  42.    */
  43.   public abstract double densityForInstance(Instance instance) 
  44.     throws Exception;
  45.   /**
  46.    * Predicts the cluster memberships for a given instance.
  47.    *
  48.    * @param instance the instance to be assigned a cluster.
  49.    * @return an array containing the estimated membership 
  50.    * probabilities of the test instance in each cluster (this 
  51.    * should sum to at most 1)
  52.    * @exception Exception if distribution could not be 
  53.    * computed successfully
  54.    */
  55.   public abstract double[] distributionForInstance(Instance instance) 
  56.        throws Exception;
  57.   /**
  58.    * Assigns an instance to a Cluster.
  59.    *
  60.    * @param instance the instance to be classified
  61.    * @return the predicted most likely cluster for the instance. 
  62.    * @exception Exception if an error occurred during the prediction
  63.    */
  64.   public int clusterInstance(Instance instance) throws Exception {
  65.     double [] dist = distributionForInstance(instance);
  66.     if (dist == null) {
  67.       throw new Exception("Null distribution predicted");
  68.     }
  69.     if (Utils.sum(dist) <= 0) {
  70.       throw new Exception("Unable to cluster instance");
  71.     }
  72.     return Utils.maxIndex(dist);
  73.   }
  74. }