Source Code Cross Referenced for MarginCurve.java in  » Science » weka » weka » classifiers » evaluation » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » weka » weka.classifiers.evaluation 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.