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.
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.
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.
18
* SingleClustererEnhancer.java
19
* Copyright (C) 2006 University of Waikato, Hamilton, New Zealand
23
package weka.clusterers;
25
import weka.core.Capabilities;
26
import weka.core.Option;
27
import weka.core.OptionHandler;
28
import weka.core.Utils;
29
import weka.core.Capabilities.Capability;
31
import java.util.Enumeration;
32
import java.util.Vector;
35
* Meta-clusterer for enhancing a base clusterer.
37
* @author FracPete (fracpete at waikato dot ac dot nz)
38
* @version $Revision: 1.3 $
40
public abstract class SingleClustererEnhancer
42
implements OptionHandler {
44
/** for serialization */
45
private static final long serialVersionUID = 4893928362926428671L;
48
protected Clusterer m_Clusterer = new SimpleKMeans();
51
* String describing default clusterer.
53
* @return the default clusterer classname
55
protected String defaultClustererString() {
56
return SimpleKMeans.class.getName();
60
* Returns an enumeration describing the available options.
62
* @return an enumeration of all the available options.
64
public Enumeration listOptions() {
65
Vector result = new Vector();
67
result.addElement(new Option(
68
"\tFull name of base clusterer.\n"
69
+ "\t(default: " + defaultClustererString() +")",
72
if (m_Clusterer instanceof OptionHandler) {
73
result.addElement(new Option(
75
"", 0, "\nOptions specific to clusterer "
76
+ m_Clusterer.getClass().getName() + ":"));
77
Enumeration enu = ((OptionHandler) m_Clusterer).listOptions();
78
while (enu.hasMoreElements()) {
79
result.addElement(enu.nextElement());
83
return result.elements();
87
* Parses a given list of options.
89
* @param options the list of options as an array of strings
90
* @throws Exception if an option is not supported
92
public void setOptions(String[] options) throws Exception {
95
tmpStr = Utils.getOption('W', options);
96
if (tmpStr.length() > 0) {
97
// This is just to set the classifier in case the option
99
setClusterer(Clusterer.forName(tmpStr, null));
100
setClusterer(Clusterer.forName(tmpStr, Utils.partitionOptions(options)));
103
// This is just to set the classifier in case the option
105
setClusterer(Clusterer.forName(defaultClustererString(), null));
106
setClusterer(Clusterer.forName(defaultClustererString(), Utils.partitionOptions(options)));
111
* Gets the current settings of the clusterer.
113
* @return an array of strings suitable for passing to setOptions
115
public String[] getOptions() {
120
result = new Vector();
123
result.add(getClusterer().getClass().getName());
125
if (getClusterer() instanceof OptionHandler) {
127
options = ((OptionHandler) getClusterer()).getOptions();
128
for (i = 0; i < options.length; i++)
129
result.add(options[i]);
132
return (String[]) result.toArray(new String[result.size()]);
136
* Returns the tip text for this property
138
* @return tip text for this property suitable for
139
* displaying in the explorer/experimenter gui
141
public String clustererTipText() {
142
return "The base clusterer to be used.";
146
* Set the base clusterer.
148
* @param value the classifier to use.
150
public void setClusterer(Clusterer value) {
155
* Get the clusterer used as the base clusterer.
157
* @return the base clusterer
159
public Clusterer getClusterer() {
164
* Gets the clusterer specification string, which contains the class name of
165
* the clusterer and any options to the clusterer
167
* @return the clusterer string
169
protected String getClustererSpec() {
173
clusterer = getClusterer();
174
result = clusterer.getClass().getName();
176
if (clusterer instanceof OptionHandler)
177
result += " " + Utils.joinOptions(((OptionHandler) clusterer).getOptions());
183
* Returns default capabilities of the clusterer.
185
* @return the capabilities of this clusterer
187
public Capabilities getCapabilities() {
190
if (getClusterer() == null)
191
result = super.getCapabilities();
193
result = getClusterer().getCapabilities();
196
for (Capability cap: Capability.values())
197
result.enableDependency(cap);
203
* Returns the number of clusters.
205
* @return the number of clusters generated for a training dataset.
206
* @throws Exception if number of clusters could not be returned
209
public int numberOfClusters() throws Exception {
210
return m_Clusterer.numberOfClusters();