source: src/main/java/weka/clusterers/AbstractClusterer.java @ 15

Last change on this file since 15 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 6.7 KB
Line 
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/*
18 *    AbstractClusterer.java
19 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.clusterers;
24
25import weka.core.Capabilities;
26import weka.core.CapabilitiesHandler;
27import weka.core.Instance;
28import weka.core.Instances;
29import weka.core.RevisionHandler;
30import weka.core.RevisionUtils;
31import weka.core.SerializedObject;
32import weka.core.Utils;
33import weka.core.Capabilities.Capability;
34
35import java.io.Serializable;
36
37/**
38 * Abstract clusterer.
39 *
40 * @author Mark Hall (mhall@cs.waikato.ac.nz)
41 * @version $Revision: 5487 $
42 */
43public abstract class AbstractClusterer
44  implements Clusterer, Cloneable, Serializable, CapabilitiesHandler, RevisionHandler {
45
46  /** for serialization */
47  private static final long serialVersionUID = -6099962589663877632L;
48
49  // ===============
50  // Public methods.
51  // ===============
52 
53  /**
54   * Generates a clusterer. Has to initialize all fields of the clusterer
55   * that are not being set via options.
56   *
57   * @param data set of instances serving as training data
58   * @exception Exception if the clusterer has not been
59   * generated successfully
60   */
61  public abstract void buildClusterer(Instances data) throws Exception;
62
63  /**
64   * Classifies a given instance. Either this or distributionForInstance()
65   * needs to be implemented by subclasses.
66   *
67   * @param instance the instance to be assigned to a cluster
68   * @return the number of the assigned cluster as an integer
69   * @exception Exception if instance could not be clustered
70   * successfully
71   */
72  public int clusterInstance(Instance instance) throws Exception {
73
74    double [] dist = distributionForInstance(instance);
75
76    if (dist == null) {
77      throw new Exception("Null distribution predicted");
78    }
79
80    if (Utils.sum(dist) <= 0) {
81      throw new Exception("Unable to cluster instance");
82    }
83    return Utils.maxIndex(dist);
84  }
85
86  /**
87   * Predicts the cluster memberships for a given instance.  Either
88   * this or clusterInstance() needs to be implemented by subclasses.
89   *
90   * @param instance the instance to be assigned a cluster.
91   * @return an array containing the estimated membership
92   * probabilities of the test instance in each cluster (this
93   * should sum to at most 1)
94   * @exception Exception if distribution could not be
95   * computed successfully
96   */
97  public double[] distributionForInstance(Instance instance) 
98    throws Exception {
99
100    double[] d = new double[numberOfClusters()];
101
102    d[clusterInstance(instance)] = 1.0;
103   
104    return d;
105  }
106
107  /**
108   * Returns the number of clusters.
109   *
110   * @return the number of clusters generated for a training dataset.
111   * @exception Exception if number of clusters could not be returned
112   * successfully
113   */
114  public abstract int numberOfClusters() throws Exception;
115
116  /**
117   * Creates a new instance of a clusterer given it's class name and
118   * (optional) arguments to pass to it's setOptions method. If the
119   * clusterer implements OptionHandler and the options parameter is
120   * non-null, the clusterer will have it's options set.
121   *
122   * @param clustererName the fully qualified class name of the clusterer
123   * @param options an array of options suitable for passing to setOptions. May
124   * be null.
125   * @return the newly created search object, ready for use.
126   * @exception Exception if the clusterer class name is invalid, or the
127   * options supplied are not acceptable to the clusterer.
128   */
129  public static Clusterer forName(String clustererName,
130                                  String [] options) throws Exception {
131    return (Clusterer)Utils.forName(Clusterer.class,
132                                    clustererName,
133                                    options);
134  }
135
136  /**
137   * Creates a deep copy of the given clusterer using serialization.
138   *
139   * @param model the clusterer to copy
140   * @return a deep copy of the clusterer
141   * @exception Exception if an error occurs
142   */
143  public static Clusterer makeCopy(Clusterer model) throws Exception {
144    return (Clusterer) new SerializedObject(model).getObject();
145  }
146
147  /**
148   * Creates copies of the current clusterer. Note that this method
149   * now uses Serialization to perform a deep copy, so the Clusterer
150   * object must be fully Serializable. Any currently built model will
151   * now be copied as well.
152   *
153   * @param model an example clusterer to copy
154   * @param num the number of clusterer copies to create.
155   * @return an array of clusterers.
156   * @exception Exception if an error occurs
157   */
158  public static Clusterer [] makeCopies(Clusterer model,
159                                        int num) throws Exception {
160     if (model == null) {
161      throw new Exception("No model clusterer set");
162    }
163    Clusterer [] clusterers = new Clusterer [num];
164    SerializedObject so = new SerializedObject(model);
165    for(int i = 0; i < clusterers.length; i++) {
166      clusterers[i] = (Clusterer) so.getObject();
167    }
168    return clusterers;
169  }
170
171  /**
172   * Returns the Capabilities of this clusterer. Derived classifiers have to
173   * override this method to enable capabilities.
174   *
175   * @return            the capabilities of this object
176   * @see               Capabilities
177   */
178  public Capabilities getCapabilities() {
179    Capabilities        result;
180   
181    result = new Capabilities(this);
182    result.enableAll();
183//    result.enable(Capability.NO_CLASS);
184   
185    return result;
186  }
187 
188  /**
189   * Returns the revision string.
190   *
191   * @return            the revision
192   */
193  public String getRevision() {
194    return RevisionUtils.extract("$Revision: 5487 $");
195  }
196 
197  /**
198   * runs the clusterer instance with the given options.
199   *
200   * @param clusterer           the clusterer to run
201   * @param options     the commandline options
202   */
203  protected static void runClusterer(Clusterer clusterer, String[] options) {
204    try {
205      System.out.println(ClusterEvaluation.evaluateClusterer(clusterer, options));
206    } 
207    catch (Exception e) {
208      if (    (e.getMessage() == null)
209           || (    (e.getMessage() != null)
210                && (e.getMessage().indexOf("General options") == -1) ) )
211        e.printStackTrace();
212      else
213        System.err.println(e.getMessage());
214    }
215  }
216}
Note: See TracBrowser for help on using the repository browser.