~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/filters/unsupervised/attribute/PKIDiscretize.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 *    PKIDiscretize.java
 
19
 *    Copyright (C) 2003 University of Waikato, Hamilton, New Zealand
 
20
 *
 
21
 */
 
22
 
 
23
package weka.filters.unsupervised.attribute;
 
24
 
 
25
import weka.core.Instances;
 
26
import weka.core.Option;
 
27
import weka.core.TechnicalInformation;
 
28
import weka.core.TechnicalInformationHandler;
 
29
import weka.core.Utils;
 
30
import weka.core.TechnicalInformation.Field;
 
31
import weka.core.TechnicalInformation.Type;
 
32
 
 
33
import java.util.Enumeration;
 
34
import java.util.Vector;
 
35
 
 
36
/**
 
37
 <!-- globalinfo-start -->
 
38
 * Discretizes numeric attributes using equal frequency binning, where the number of bins is equal to the square root of the number of non-missing values.<br/>
 
39
 * <br/>
 
40
 * For more information, see:<br/>
 
41
 * <br/>
 
42
 * Ying Yang, Geoffrey I. Webb: Proportional k-Interval Discretization for Naive-Bayes Classifiers. In: 12th European Conference on Machine Learning, 564-575, 2001.
 
43
 * <p/>
 
44
 <!-- globalinfo-end -->
 
45
 * 
 
46
 <!-- technical-bibtex-start -->
 
47
 * BibTeX:
 
48
 * <pre>
 
49
 * &#64;inproceedings{Yang2001,
 
50
 *    author = {Ying Yang and Geoffrey I. Webb},
 
51
 *    booktitle = {12th European Conference on Machine Learning},
 
52
 *    pages = {564-575},
 
53
 *    publisher = {Springer},
 
54
 *    series = {LNCS},
 
55
 *    title = {Proportional k-Interval Discretization for Naive-Bayes Classifiers},
 
56
 *    volume = {2167},
 
57
 *    year = {2001}
 
58
 * }
 
59
 * </pre>
 
60
 * <p/>
 
61
 <!-- technical-bibtex-end -->
 
62
 *
 
63
 <!-- options-start -->
 
64
 * Valid options are: <p/>
 
65
 * 
 
66
 * <pre> -unset-class-temporarily
 
67
 *  Unsets the class index temporarily before the filter is
 
68
 *  applied to the data.
 
69
 *  (default: no)</pre>
 
70
 * 
 
71
 * <pre> -R &lt;col1,col2-col4,...&gt;
 
72
 *  Specifies list of columns to Discretize. First and last are valid indexes.
 
73
 *  (default: first-last)</pre>
 
74
 * 
 
75
 * <pre> -V
 
76
 *  Invert matching sense of column indexes.</pre>
 
77
 * 
 
78
 * <pre> -D
 
79
 *  Output binary attributes for discretized attributes.</pre>
 
80
 * 
 
81
 <!-- options-end -->
 
82
 *
 
83
 * @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
 
84
 * @version $Revision: 1.8 $
 
85
 */
 
86
public class PKIDiscretize 
 
87
  extends Discretize
 
88
  implements TechnicalInformationHandler {
 
89
  
 
90
  /** for serialization */
 
91
  static final long serialVersionUID = 6153101248977702675L;
 
92
 
 
93
  /**
 
94
   * Sets the format of the input instances.
 
95
   *
 
96
   * @param instanceInfo an Instances object containing the input instance
 
97
   * structure (any instances contained in the object are ignored - only the
 
98
   * structure is required).
 
99
   * @return true if the outputFormat may be collected immediately
 
100
   * @throws Exception if the input format can't be set successfully
 
101
   */
 
102
  public boolean setInputFormat(Instances instanceInfo) throws Exception {
 
103
 
 
104
    // alter child behaviour to do what we want
 
105
    m_FindNumBins = true;
 
106
    return super.setInputFormat(instanceInfo);
 
107
  }
 
108
 
 
109
  /**
 
110
   * Finds the number of bins to use and creates the cut points.
 
111
   *
 
112
   * @param index the attribute index
 
113
   */
 
114
  protected void findNumBins(int index) {
 
115
 
 
116
    Instances toFilter = getInputFormat();
 
117
 
 
118
    // Find number of instances for attribute where not missing
 
119
    int numOfInstances = toFilter.numInstances();
 
120
    for (int i = 0; i < toFilter.numInstances(); i++) {
 
121
      if (toFilter.instance(i).isMissing(index))
 
122
        numOfInstances--;
 
123
    }
 
124
 
 
125
    m_NumBins = (int)(Math.sqrt(numOfInstances));
 
126
 
 
127
    if (m_NumBins > 0) {
 
128
      calculateCutPointsByEqualFrequencyBinning(index);
 
129
    }
 
130
  }
 
131
 
 
132
  /**
 
133
   * Gets an enumeration describing the available options.
 
134
   *
 
135
   * @return an enumeration of all the available options.
 
136
   */
 
137
  public Enumeration listOptions() {
 
138
    Vector result = new Vector();
 
139
    
 
140
    result.addElement(new Option(
 
141
        "\tUnsets the class index temporarily before the filter is\n"
 
142
        + "\tapplied to the data.\n"
 
143
        + "\t(default: no)",
 
144
        "unset-class-temporarily", 1, "-unset-class-temporarily"));
 
145
    
 
146
    result.addElement(new Option(
 
147
        "\tSpecifies list of columns to Discretize. First"
 
148
        + " and last are valid indexes.\n"
 
149
        + "\t(default: first-last)",
 
150
        "R", 1, "-R <col1,col2-col4,...>"));
 
151
    
 
152
    result.addElement(new Option(
 
153
        "\tInvert matching sense of column indexes.",
 
154
        "V", 0, "-V"));
 
155
    
 
156
    result.addElement(new Option(
 
157
        "\tOutput binary attributes for discretized attributes.",
 
158
        "D", 0, "-D"));
 
159
    
 
160
    return result.elements();
 
161
  }
 
162
 
 
163
 
 
164
  /**
 
165
   * Parses a given list of options. <p/>
 
166
   * 
 
167
   <!-- options-start -->
 
168
   * Valid options are: <p/>
 
169
   * 
 
170
   * <pre> -unset-class-temporarily
 
171
   *  Unsets the class index temporarily before the filter is
 
172
   *  applied to the data.
 
173
   *  (default: no)</pre>
 
174
   * 
 
175
   * <pre> -R &lt;col1,col2-col4,...&gt;
 
176
   *  Specifies list of columns to Discretize. First and last are valid indexes.
 
177
   *  (default: first-last)</pre>
 
178
   * 
 
179
   * <pre> -V
 
180
   *  Invert matching sense of column indexes.</pre>
 
181
   * 
 
182
   * <pre> -D
 
183
   *  Output binary attributes for discretized attributes.</pre>
 
184
   * 
 
185
   <!-- options-end -->
 
186
   *
 
187
   * @param options the list of options as an array of strings
 
188
   * @throws Exception if an option is not supported
 
189
   */
 
190
  public void setOptions(String[] options) throws Exception {
 
191
 
 
192
    setIgnoreClass(Utils.getFlag("unset-class-temporarily", options));
 
193
    setMakeBinary(Utils.getFlag('D', options));
 
194
    setInvertSelection(Utils.getFlag('V', options));
 
195
    
 
196
    String convertList = Utils.getOption('R', options);
 
197
    if (convertList.length() != 0) {
 
198
      setAttributeIndices(convertList);
 
199
    } else {
 
200
      setAttributeIndices("first-last");
 
201
    }
 
202
 
 
203
    if (getInputFormat() != null) {
 
204
      setInputFormat(getInputFormat());
 
205
    }
 
206
  }
 
207
  /**
 
208
   * Gets the current settings of the filter.
 
209
   *
 
210
   * @return an array of strings suitable for passing to setOptions
 
211
   */
 
212
  public String[] getOptions() {
 
213
    Vector        result;
 
214
 
 
215
    result = new Vector();
 
216
 
 
217
    if (getMakeBinary())
 
218
      result.add("-D");
 
219
    
 
220
    if (getInvertSelection())
 
221
      result.add("-V");
 
222
    
 
223
    if (!getAttributeIndices().equals("")) {
 
224
      result.add("-R");
 
225
      result.add(getAttributeIndices());
 
226
    }
 
227
 
 
228
    return (String[]) result.toArray(new String[result.size()]);
 
229
  }
 
230
 
 
231
  /**
 
232
   * Returns a string describing this filter
 
233
   *
 
234
   * @return a description of the filter suitable for
 
235
   * displaying in the explorer/experimenter gui
 
236
   */
 
237
  public String globalInfo() {
 
238
 
 
239
    return "Discretizes numeric attributes using equal frequency binning,"
 
240
      + " where the number of bins is equal to the square root of the"
 
241
      + " number of non-missing values.\n\n"
 
242
      + "For more information, see:\n\n"
 
243
      + getTechnicalInformation().toString();
 
244
  }
 
245
 
 
246
  /**
 
247
   * Returns an instance of a TechnicalInformation object, containing 
 
248
   * detailed information about the technical background of this class,
 
249
   * e.g., paper reference or book this class is based on.
 
250
   * 
 
251
   * @return the technical information about this class
 
252
   */
 
253
  public TechnicalInformation getTechnicalInformation() {
 
254
    TechnicalInformation        result;
 
255
    
 
256
    result = new TechnicalInformation(Type.INPROCEEDINGS);
 
257
    result.setValue(Field.AUTHOR, "Ying Yang and Geoffrey I. Webb");
 
258
    result.setValue(Field.TITLE, "Proportional k-Interval Discretization for Naive-Bayes Classifiers");
 
259
    result.setValue(Field.BOOKTITLE, "12th European Conference on Machine Learning");
 
260
    result.setValue(Field.YEAR, "2001");
 
261
    result.setValue(Field.PAGES, "564-575");
 
262
    result.setValue(Field.PUBLISHER, "Springer");
 
263
    result.setValue(Field.SERIES, "LNCS");
 
264
    result.setValue(Field.VOLUME, "2167");
 
265
    
 
266
    return result;
 
267
  }
 
268
  
 
269
  /**
 
270
   * Returns the tip text for this property
 
271
   *
 
272
   * @return tip text for this property suitable for
 
273
   * displaying in the explorer/experimenter gui
 
274
   */
 
275
  public String findNumBinsTipText() {
 
276
 
 
277
    return "Ignored.";
 
278
  }
 
279
 
 
280
  /**
 
281
   * Get the value of FindNumBins.
 
282
   *
 
283
   * @return Value of FindNumBins.
 
284
   */
 
285
  public boolean getFindNumBins() {
 
286
    
 
287
    return false;
 
288
  }
 
289
  
 
290
  /**
 
291
   * Set the value of FindNumBins.
 
292
   *
 
293
   * @param newFindNumBins Value to assign to FindNumBins.
 
294
   */
 
295
  public void setFindNumBins(boolean newFindNumBins) {
 
296
    
 
297
  }
 
298
  
 
299
  /**
 
300
   * Returns the tip text for this property
 
301
   *
 
302
   * @return tip text for this property suitable for
 
303
   * displaying in the explorer/experimenter gui
 
304
   */
 
305
  public String useEqualFrequencyTipText() {
 
306
 
 
307
    return "Always true.";
 
308
  }
 
309
 
 
310
  /**
 
311
   * Get the value of UseEqualFrequency.
 
312
   *
 
313
   * @return Value of UseEqualFrequency.
 
314
   */
 
315
  public boolean getUseEqualFrequency() {
 
316
    
 
317
    return true;
 
318
  }
 
319
  
 
320
  /**
 
321
   * Set the value of UseEqualFrequency.
 
322
   *
 
323
   * @param newUseEqualFrequency Value to assign to UseEqualFrequency.
 
324
   */
 
325
  public void setUseEqualFrequency(boolean newUseEqualFrequency) {
 
326
    
 
327
  }
 
328
 
 
329
  /**
 
330
   * Returns the tip text for this property
 
331
   *
 
332
   * @return tip text for this property suitable for
 
333
   * displaying in the explorer/experimenter gui
 
334
   */
 
335
  public String binsTipText() {
 
336
 
 
337
    return "Ignored.";
 
338
  }
 
339
 
 
340
  /**
 
341
   * Ignored
 
342
   *
 
343
   * @return the number of bins.
 
344
   */
 
345
  public int getBins() {
 
346
 
 
347
    return 0;
 
348
  }
 
349
 
 
350
  /**
 
351
   * Ignored
 
352
   *
 
353
   * @param numBins the number of bins
 
354
   */
 
355
  public void setBins(int numBins) {
 
356
 
 
357
  }
 
358
 
 
359
  /**
 
360
   * Main method for testing this class.
 
361
   *
 
362
   * @param argv should contain arguments to the filter: use -h for help
 
363
   */
 
364
  public static void main(String [] argv) {
 
365
    runFilter(new PKIDiscretize(), argv);
 
366
  }
 
367
}