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 | * ClustererAssignmentsPlotInstances.java |
---|
19 | * Copyright (C) 2009 University of Waikato, Hamilton, New Zealand |
---|
20 | */ |
---|
21 | |
---|
22 | package weka.gui.explorer; |
---|
23 | |
---|
24 | import weka.clusterers.ClusterEvaluation; |
---|
25 | import weka.clusterers.Clusterer; |
---|
26 | import weka.core.Attribute; |
---|
27 | import weka.core.FastVector; |
---|
28 | import weka.core.Instance; |
---|
29 | import weka.core.DenseInstance; |
---|
30 | import weka.core.Instances; |
---|
31 | import weka.core.Utils; |
---|
32 | import weka.gui.visualize.Plot2D; |
---|
33 | import weka.gui.visualize.PlotData2D; |
---|
34 | |
---|
35 | /** |
---|
36 | * A class for generating plottable cluster assignments. |
---|
37 | * <p/> |
---|
38 | * Example usage: |
---|
39 | * <pre> |
---|
40 | * Instances train = ... // from somewhere |
---|
41 | * Instances test = ... // from somewhere |
---|
42 | * Clusterer cls = ... // from somewhere |
---|
43 | * // build and evaluate clusterer |
---|
44 | * cls.buildClusterer(train); |
---|
45 | * ClusterEvaluation eval = new ClusterEvaluation(); |
---|
46 | * eval.setClusterer(cls); |
---|
47 | * eval.evaluateClusterer(test); |
---|
48 | * // generate plot instances |
---|
49 | * ClustererPlotInstances plotInstances = new ClustererPlotInstances(); |
---|
50 | * plotInstances.setClusterer(cls); |
---|
51 | * plotInstances.setInstances(test); |
---|
52 | * plotInstances.setClusterer(cls); |
---|
53 | * plotInstances.setClusterEvaluation(eval); |
---|
54 | * plotInstances.setUp(); |
---|
55 | * // generate visualization |
---|
56 | * VisualizePanel visPanel = new VisualizePanel(); |
---|
57 | * visPanel.addPlot(plotInstances.getPlotData("plot name")); |
---|
58 | * // clean up |
---|
59 | * plotInstances.cleanUp(); |
---|
60 | * </pre> |
---|
61 | * |
---|
62 | * @author fracpete (fracpete at waikato dot ac dot nz) |
---|
63 | * @version $Revision: 6021 $ |
---|
64 | */ |
---|
65 | public class ClustererAssignmentsPlotInstances |
---|
66 | extends AbstractPlotInstances { |
---|
67 | |
---|
68 | /** for serialization. */ |
---|
69 | private static final long serialVersionUID = -4748134272046520423L; |
---|
70 | |
---|
71 | /** for storing the plot shapes. */ |
---|
72 | protected int[] m_PlotShapes; |
---|
73 | |
---|
74 | /** the clusterer being used. */ |
---|
75 | protected Clusterer m_Clusterer; |
---|
76 | |
---|
77 | /** the cluster evaluation to use. */ |
---|
78 | protected ClusterEvaluation m_Evaluation; |
---|
79 | |
---|
80 | /** |
---|
81 | * Initializes the members. |
---|
82 | */ |
---|
83 | protected void initialize() { |
---|
84 | super.initialize(); |
---|
85 | |
---|
86 | m_PlotShapes = null; |
---|
87 | m_Clusterer = null; |
---|
88 | m_Evaluation = null; |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * Sets the classifier used for making the predictions. |
---|
93 | * |
---|
94 | * @param value the clusterer to use |
---|
95 | */ |
---|
96 | public void setClusterer(Clusterer value) { |
---|
97 | m_Clusterer = value; |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * Returns the currently set clusterer. |
---|
102 | * |
---|
103 | * @return the clusterer in use |
---|
104 | */ |
---|
105 | public Clusterer getClusterer() { |
---|
106 | return m_Clusterer; |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * Sets the cluster evaluation object to use. |
---|
111 | * |
---|
112 | * @param value the evaluation object |
---|
113 | */ |
---|
114 | public void setClusterEvaluation(ClusterEvaluation value) { |
---|
115 | m_Evaluation = value; |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * Returns the cluster evaluation object in use. |
---|
120 | * |
---|
121 | * @return the evaluation object |
---|
122 | */ |
---|
123 | public ClusterEvaluation getClusterEvaluation() { |
---|
124 | return m_Evaluation; |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * Checks whether clusterer and evaluation are provided. |
---|
129 | */ |
---|
130 | protected void check() { |
---|
131 | super.check(); |
---|
132 | |
---|
133 | if (m_Clusterer == null) |
---|
134 | throw new IllegalStateException("No clusterer set!"); |
---|
135 | |
---|
136 | if (m_Evaluation == null) |
---|
137 | throw new IllegalStateException("No cluster evaluation set!"); |
---|
138 | } |
---|
139 | |
---|
140 | /** |
---|
141 | * Sets up the structure for the plot instances. |
---|
142 | */ |
---|
143 | protected void determineFormat() { |
---|
144 | int numClusters; |
---|
145 | FastVector hv; |
---|
146 | Attribute predictedCluster; |
---|
147 | FastVector clustVals; |
---|
148 | int i; |
---|
149 | |
---|
150 | numClusters = m_Evaluation.getNumClusters(); |
---|
151 | hv = new FastVector(); |
---|
152 | clustVals = new FastVector(); |
---|
153 | |
---|
154 | for (i = 0; i < numClusters; i++) |
---|
155 | clustVals.addElement("cluster" + (i+1)); |
---|
156 | predictedCluster = new Attribute("Cluster", clustVals); |
---|
157 | for (i = 0; i < m_Instances.numAttributes(); i++) |
---|
158 | hv.addElement(m_Instances.attribute(i).copy()); |
---|
159 | hv.addElement(predictedCluster); |
---|
160 | |
---|
161 | m_PlotInstances = new Instances( |
---|
162 | m_Instances.relationName() + "_clustered", hv, m_Instances.numInstances()); |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * Generates the cluster assignments. |
---|
167 | * |
---|
168 | * @see #m_PlotShapes |
---|
169 | * @see #m_PlotSizes |
---|
170 | * @see #m_PlotInstances |
---|
171 | */ |
---|
172 | protected void process() { |
---|
173 | double[] clusterAssignments; |
---|
174 | int i; |
---|
175 | double[] values; |
---|
176 | int j; |
---|
177 | int[] classAssignments; |
---|
178 | |
---|
179 | clusterAssignments = m_Evaluation.getClusterAssignments(); |
---|
180 | |
---|
181 | classAssignments = null; |
---|
182 | if (m_Instances.classIndex() >= 0) { |
---|
183 | classAssignments = m_Evaluation.getClassesToClusters(); |
---|
184 | m_PlotShapes = new int[m_Instances.numInstances()]; |
---|
185 | for (i = 0; i < m_Instances.numInstances(); i++) |
---|
186 | m_PlotShapes[i] = Plot2D.CONST_AUTOMATIC_SHAPE; |
---|
187 | } |
---|
188 | |
---|
189 | for (i = 0; i < m_Instances.numInstances(); i++) { |
---|
190 | values = new double[m_PlotInstances.numAttributes()]; |
---|
191 | for (j = 0; j < m_Instances.numAttributes(); j++) |
---|
192 | values[j] = m_Instances.instance(i).value(j); |
---|
193 | if (clusterAssignments[i] < 0) { |
---|
194 | values[j] = Utils.missingValue(); |
---|
195 | } else { |
---|
196 | values[j] = clusterAssignments[i]; |
---|
197 | } |
---|
198 | m_PlotInstances.add(new DenseInstance(1.0, values)); |
---|
199 | if (m_PlotShapes != null) { |
---|
200 | if (clusterAssignments[i] >= 0) { |
---|
201 | if ((int) m_Instances.instance(i).classValue() != classAssignments[(int) clusterAssignments[i]]) |
---|
202 | m_PlotShapes[i] = Plot2D.ERROR_SHAPE; |
---|
203 | } else { |
---|
204 | m_PlotShapes[i] = Plot2D.MISSING_SHAPE; |
---|
205 | } |
---|
206 | } |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | /** |
---|
211 | * Performs optional post-processing. |
---|
212 | */ |
---|
213 | protected void finishUp() { |
---|
214 | super.finishUp(); |
---|
215 | |
---|
216 | process(); |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * Assembles and returns the plot. The relation name of the dataset gets |
---|
221 | * added automatically. |
---|
222 | * |
---|
223 | * @param name the name of the plot |
---|
224 | * @return the plot |
---|
225 | * @throws Exception if plot generation fails |
---|
226 | */ |
---|
227 | protected PlotData2D createPlotData(String name) throws Exception { |
---|
228 | PlotData2D result; |
---|
229 | |
---|
230 | result = new PlotData2D(m_PlotInstances); |
---|
231 | if (m_PlotShapes != null) |
---|
232 | result.setShapeType(m_PlotShapes); |
---|
233 | result.addInstanceNumberAttribute(); |
---|
234 | result.setPlotName(name + " (" + m_Instances.relationName() + ")"); |
---|
235 | |
---|
236 | return result; |
---|
237 | } |
---|
238 | |
---|
239 | /** |
---|
240 | * For freeing up memory. Plot data cannot be generated after this call! |
---|
241 | */ |
---|
242 | public void cleanUp() { |
---|
243 | super.cleanUp(); |
---|
244 | |
---|
245 | m_Clusterer = null; |
---|
246 | m_Evaluation = null; |
---|
247 | m_PlotShapes = null; |
---|
248 | } |
---|
249 | } |
---|