source: src/main/java/weka/classifiers/meta/ensembleSelection/EnsembleMetricHelper.java @ 23

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

Import di weka.

File size: 3.3 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 *    EnsembleMetricHelper.java
19 *    Copyright (C) 2006 David Michael
20 *
21 */
22
23package weka.classifiers.meta.ensembleSelection;
24
25import weka.classifiers.Evaluation;
26import weka.core.RevisionHandler;
27import weka.core.RevisionUtils;
28
29/**
30 * This class is used by Ensemble Selection.  It provides the "enumeration" of the
31 * metrics that can be used by ensemble selection, as well as a helper function for
32 * computing the metric using an Evaluation class.
33 *
34 * @author  David Michael
35 * @version $Revision: 1.2 $
36 */
37public class EnsembleMetricHelper
38  implements RevisionHandler {
39 
40  /** metric: Accuracy */
41  public static final int METRIC_ACCURACY = 0;
42  /** metric: RMSE */
43  public static final int METRIC_RMSE = 1;
44  /** metric: ROC */
45  public static final int METRIC_ROC = 2;
46  /** metric: Precision */
47  public static final int METRIC_PRECISION = 3;
48  /** metric: Recall */
49  public static final int METRIC_RECALL = 4;
50  /** metric: FScore */
51  public static final int METRIC_FSCORE = 5;
52  /** metric: All */
53  public static final int METRIC_ALL = 6;
54 
55  /**
56   * Given an Evaluation object and metric, call the appropriate function to get
57   * the value for that metric and return it.  Metrics are returned so that
58   * "bigger is better".  For instance, we return 1.0 - RMSE instead of RMSE, because
59   * bigger RMSE is better.
60   *
61   * @param eval                the evaluation object to use
62   * @param metric_index        the metric to use
63   * @return                    the value for the metric
64   */
65  public static double getMetric(Evaluation eval, int metric_index) {
66    switch (metric_index) {
67      case METRIC_ACCURACY:
68        return eval.pctCorrect();
69      case METRIC_RMSE:
70        return 1.0 - eval.rootMeanSquaredError();
71      case METRIC_ROC:
72        return eval.areaUnderROC(1); //TODO - is 1 right?
73      case METRIC_PRECISION:
74        return eval.precision(1); //TODO - same question
75      case METRIC_RECALL:
76        return eval.recall(1); //TODO - same question
77      case METRIC_FSCORE:
78        return eval.fMeasure(1); //TODO - same question
79      case METRIC_ALL:
80        double average = 0;
81        int num_metrics = 0;
82        average += eval.pctCorrect();
83        ++num_metrics;
84        average += 1.0 - eval.rootMeanSquaredError();
85        ++num_metrics;
86        average += eval.areaUnderROC(1);
87        ++num_metrics;
88        average += eval.precision(1);
89        ++num_metrics;
90        average += eval.recall(1);
91        ++num_metrics;
92        average += eval.fMeasure(1);
93        ++num_metrics;
94        return average / num_metrics;
95      default:
96        return 0.0; //FIXME TODO - this should probably be an exception?
97    }
98  }
99 
100  /**
101   * Returns the revision string.
102   *
103   * @return            the revision
104   */
105  public String getRevision() {
106    return RevisionUtils.extract("$Revision: 1.2 $");
107  }
108}
Note: See TracBrowser for help on using the repository browser.