source: src/main/java/weka/attributeSelection/ASEvaluation.java @ 6

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

Import di weka.

File size: 5.1 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 *    ASEvaluation.java
19 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23
24package weka.attributeSelection;
25
26import weka.core.Capabilities;
27import weka.core.CapabilitiesHandler;
28import weka.core.Instances;
29import weka.core.RevisionHandler;
30import weka.core.RevisionUtils;
31import weka.core.SerializedObject;
32import weka.core.Utils;
33
34import java.io.Serializable;
35
36/**
37 * Abstract attribute selection evaluation class
38 *
39 * @author Mark Hall (mhall@cs.waikato.ac.nz)
40 * @version $Revision: 5446 $
41 */
42public abstract class ASEvaluation
43  implements Serializable, CapabilitiesHandler, RevisionHandler {
44
45  /** for serialization */
46  private static final long serialVersionUID = 2091705669885950849L;
47 
48  // ===============
49  // Public methods.
50  // ===============
51
52  /**
53   * Generates a attribute evaluator. Has to initialize all fields of the
54   * evaluator that are not being set via options.
55   *
56   * @param data set of instances serving as training data
57   * @exception Exception if the evaluator has not been
58   * generated successfully
59   */
60  public abstract void buildEvaluator(Instances data) throws Exception;
61
62  /**
63   * Provides a chance for a attribute evaluator to do any special
64   * post processing of the selected attribute set.
65   *
66   * @param attributeSet the set of attributes found by the search
67   * @return a possibly ranked list of postprocessed attributes
68   * @exception Exception if postprocessing fails for some reason
69   */
70  public int [] postProcess(int [] attributeSet) 
71    throws Exception {
72    return attributeSet;
73  }
74
75  /**
76   * Creates a new instance of an attribute/subset evaluator
77   * given it's class name and
78   * (optional) arguments to pass to it's setOptions method. If the
79   * evaluator implements OptionHandler and the options parameter is
80   * non-null, the evaluator will have it's options set.
81   *
82   * @param evaluatorName the fully qualified class name of the evaluator
83   * @param options an array of options suitable for passing to setOptions. May
84   * be null.
85   * @return the newly created evaluator, ready for use.
86   * @exception Exception if the evaluator name is invalid, or the options
87   * supplied are not acceptable to the evaluator
88   */
89  public static ASEvaluation forName(String evaluatorName,
90                                     String [] options) throws Exception {
91    return (ASEvaluation)Utils.forName(ASEvaluation.class,
92                                       evaluatorName,
93                                       options);
94  }
95
96  /**
97   * Creates copies of the current evaluator. Note that this method
98   * now uses Serialization to perform a deep copy, so the evaluator
99   * object must be fully Serializable. Any currently built model will
100   * now be copied as well.
101   *
102   * @param model an example evaluator to copy
103   * @param num the number of evaluator copies to create.
104   * @return an array of evaluators.
105   * @exception Exception if an error occurs
106   */
107  public static ASEvaluation [] makeCopies(ASEvaluation model,
108                                         int num) throws Exception {
109
110    if (model == null) {
111      throw new Exception("No model evaluator set");
112    }
113    ASEvaluation [] evaluators = new ASEvaluation [num];
114    SerializedObject so = new SerializedObject(model);
115    for(int i = 0; i < evaluators.length; i++) {
116      evaluators[i] = (ASEvaluation) so.getObject();
117    }
118    return evaluators;
119  }
120
121  /**
122   * Returns the capabilities of this evaluator.
123   *
124   * @return            the capabilities of this evaluator
125   * @see               Capabilities
126   */
127  public Capabilities getCapabilities() {
128    Capabilities result = new Capabilities(this);
129    result.enableAll();
130   
131    return result;
132  }
133 
134  /**
135   * Returns the revision string.
136   *
137   * @return            the revision
138   */
139  public String getRevision() {
140    return RevisionUtils.extract("$Revision: 5446 $");
141  }
142 
143  /**
144   * runs the evaluator with the given commandline options
145   *
146   * @param evaluator   the evaluator to run
147   * @param options     the commandline options
148   */
149  protected static void runEvaluator(ASEvaluation evaluator, String[] options) {
150    try {
151      System.out.println(
152          AttributeSelection.SelectAttributes(evaluator, options));
153    }
154    catch (Exception e) {
155      String msg = e.toString().toLowerCase();
156      if (    (msg.indexOf("help requested") == -1)
157           && (msg.indexOf("no training file given") == -1) )
158        e.printStackTrace();
159      System.err.println(e.getMessage());
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.