001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
015: */
016:
017: /*
018: * MarginCurve.java
019: * Copyright (C) 2002 University of Waikato, Hamilton, New Zealand
020: *
021: */
022:
023: package weka.classifiers.evaluation;
024:
025: import weka.classifiers.meta.LogitBoost;
026: import weka.core.Utils;
027: import weka.core.Attribute;
028: import weka.core.FastVector;
029: import weka.core.Instance;
030: import weka.core.Instances;
031:
032: /**
033: * Generates points illustrating the prediction margin. The margin is defined
034: * as the difference between the probability predicted for the actual class and
035: * the highest probability predicted for the other classes. One hypothesis
036: * as to the good performance of boosting algorithms is that they increaes the
037: * margins on the training data and this gives better performance on test data.
038: *
039: * @author Len Trigg (len@reeltwo.com)
040: * @version $Revision: 1.10 $
041: */
042: public class MarginCurve {
043:
044: /**
045: * Calculates the cumulative margin distribution for the set of
046: * predictions, returning the result as a set of Instances. The
047: * structure of these Instances is as follows:<p> <ul>
048: * <li> <b>Margin</b> contains the margin value (which should be plotted
049: * as an x-coordinate)
050: * <li> <b>Current</b> contains the count of instances with the current
051: * margin (plot as y axis)
052: * <li> <b>Cumulative</b> contains the count of instances with margin
053: * less than or equal to the current margin (plot as y axis)
054: * </ul> <p>
055: *
056: * @return datapoints as a set of instances, null if no predictions
057: * have been made.
058: */
059: public Instances getCurve(FastVector predictions) {
060:
061: if (predictions.size() == 0) {
062: return null;
063: }
064:
065: Instances insts = makeHeader();
066: double[] margins = getMargins(predictions);
067: int[] sorted = Utils.sort(margins);
068: int binMargin = 0;
069: int totalMargin = 0;
070: insts.add(makeInstance(-1, binMargin, totalMargin));
071: for (int i = 0; i < sorted.length; i++) {
072: double current = margins[sorted[i]];
073: double weight = ((NominalPrediction) predictions
074: .elementAt(sorted[i])).weight();
075: totalMargin += weight;
076: binMargin += weight;
077: if (true) {
078: insts
079: .add(makeInstance(current, binMargin,
080: totalMargin));
081: binMargin = 0;
082: }
083: }
084: return insts;
085: }
086:
087: /**
088: * Pulls all the margin values out of a vector of NominalPredictions.
089: *
090: * @param predictions a FastVector containing NominalPredictions
091: * @return an array of margin values.
092: */
093: private double[] getMargins(FastVector predictions) {
094:
095: // sort by predicted probability of the desired class.
096: double[] margins = new double[predictions.size()];
097: for (int i = 0; i < margins.length; i++) {
098: NominalPrediction pred = (NominalPrediction) predictions
099: .elementAt(i);
100: margins[i] = pred.margin();
101: }
102: return margins;
103: }
104:
105: /**
106: * Creates an Instances object with the attributes we will be calculating.
107: *
108: * @return the Instances structure.
109: */
110: private Instances makeHeader() {
111:
112: FastVector fv = new FastVector();
113: fv.addElement(new Attribute("Margin"));
114: fv.addElement(new Attribute("Current"));
115: fv.addElement(new Attribute("Cumulative"));
116: return new Instances("MarginCurve", fv, 100);
117: }
118:
119: /**
120: * Creates an Instance object with the attributes calculated.
121: *
122: * @param margin the margin for this data point.
123: * @param current the number of instances with this margin.
124: * @param cumulative the number of instances with margin less than or equal
125: * to this margin.
126: * @return the Instance object.
127: */
128: private Instance makeInstance(double margin, int current,
129: int cumulative) {
130:
131: int count = 0;
132: double[] vals = new double[3];
133: vals[count++] = margin;
134: vals[count++] = current;
135: vals[count++] = cumulative;
136: return new Instance(1.0, vals);
137: }
138:
139: /**
140: * Tests the MarginCurve generation from the command line.
141: * The classifier is currently hardcoded. Pipe in an arff file.
142: *
143: * @param args currently ignored
144: */
145: public static void main(String[] args) {
146:
147: try {
148: Utils.SMALL = 0;
149: Instances inst = new Instances(
150: new java.io.InputStreamReader(System.in));
151: inst.setClassIndex(inst.numAttributes() - 1);
152: MarginCurve tc = new MarginCurve();
153: EvaluationUtils eu = new EvaluationUtils();
154: weka.classifiers.meta.LogitBoost classifier = new weka.classifiers.meta.LogitBoost();
155: classifier.setNumIterations(20);
156: FastVector predictions = eu.getTrainTestPredictions(
157: classifier, inst, inst);
158: Instances result = tc.getCurve(predictions);
159: System.out.println(result);
160: } catch (Exception ex) {
161: ex.printStackTrace();
162: }
163: }
164: }
|