Source Code Cross Referenced for ZeroR.java in  » Science » weka » weka » classifiers » rules » 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.rules 
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:         *    ZeroR.java
019:         *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
020:         *
021:         */
022:
023:        package weka.classifiers.rules;
024:
025:        import weka.classifiers.Classifier;
026:        import weka.core.Attribute;
027:        import weka.core.Capabilities;
028:        import weka.core.Instance;
029:        import weka.core.Instances;
030:        import weka.core.Utils;
031:        import weka.core.WeightedInstancesHandler;
032:        import weka.core.Capabilities.Capability;
033:
034:        import java.util.Enumeration;
035:
036:        /**
037:         <!-- globalinfo-start -->
038:         * Class for building and using a 0-R classifier. Predicts the mean (for a numeric class) or the mode (for a nominal class).
039:         * <p/>
040:         <!-- globalinfo-end -->
041:         *
042:         <!-- options-start -->
043:         * Valid options are: <p/>
044:         * 
045:         * <pre> -D
046:         *  If set, classifier is run in debug mode and
047:         *  may output additional info to the console</pre>
048:         * 
049:         <!-- options-end -->
050:         *
051:         * @author Eibe Frank (eibe@cs.waikato.ac.nz)
052:         * @version $Revision: 1.15 $
053:         */
054:        public class ZeroR extends Classifier implements 
055:                WeightedInstancesHandler {
056:
057:            /** for serialization */
058:            static final long serialVersionUID = 48055541465867954L;
059:
060:            /** The class value 0R predicts. */
061:            private double m_ClassValue;
062:
063:            /** The number of instances in each class (null if class numeric). */
064:            private double[] m_Counts;
065:
066:            /** The class attribute. */
067:            private Attribute m_Class;
068:
069:            /**
070:             * Returns a string describing classifier
071:             * @return a description suitable for
072:             * displaying in the explorer/experimenter gui
073:             */
074:            public String globalInfo() {
075:                return "Class for building and using a 0-R classifier. Predicts the mean "
076:                        + "(for a numeric class) or the mode (for a nominal class).";
077:            }
078:
079:            /**
080:             * Returns default capabilities of the classifier.
081:             *
082:             * @return      the capabilities of this classifier
083:             */
084:            public Capabilities getCapabilities() {
085:                Capabilities result = super .getCapabilities();
086:
087:                // attributes
088:                result.enable(Capability.NOMINAL_ATTRIBUTES);
089:                result.enable(Capability.NUMERIC_ATTRIBUTES);
090:                result.enable(Capability.DATE_ATTRIBUTES);
091:                result.enable(Capability.STRING_ATTRIBUTES);
092:                result.enable(Capability.RELATIONAL_ATTRIBUTES);
093:                result.enable(Capability.MISSING_VALUES);
094:
095:                // class
096:                result.enable(Capability.NOMINAL_CLASS);
097:                result.enable(Capability.NUMERIC_CLASS);
098:                result.enable(Capability.DATE_CLASS);
099:                result.enable(Capability.MISSING_CLASS_VALUES);
100:
101:                // instances
102:                result.setMinimumNumberInstances(0);
103:
104:                return result;
105:            }
106:
107:            /**
108:             * Generates the classifier.
109:             *
110:             * @param instances set of instances serving as training data 
111:             * @exception Exception if the classifier has not been generated successfully
112:             */
113:            public void buildClassifier(Instances instances) throws Exception {
114:                // can classifier handle the data?
115:                getCapabilities().testWithFail(instances);
116:
117:                // remove instances with missing class
118:                instances = new Instances(instances);
119:                instances.deleteWithMissingClass();
120:
121:                double sumOfWeights = 0;
122:
123:                m_Class = instances.classAttribute();
124:                m_ClassValue = 0;
125:                switch (instances.classAttribute().type()) {
126:                case Attribute.NUMERIC:
127:                    m_Counts = null;
128:                    break;
129:                case Attribute.NOMINAL:
130:                    m_Counts = new double[instances.numClasses()];
131:                    for (int i = 0; i < m_Counts.length; i++) {
132:                        m_Counts[i] = 1;
133:                    }
134:                    sumOfWeights = instances.numClasses();
135:                    break;
136:                }
137:                Enumeration enu = instances.enumerateInstances();
138:                while (enu.hasMoreElements()) {
139:                    Instance instance = (Instance) enu.nextElement();
140:                    if (!instance.classIsMissing()) {
141:                        if (instances.classAttribute().isNominal()) {
142:                            m_Counts[(int) instance.classValue()] += instance
143:                                    .weight();
144:                        } else {
145:                            m_ClassValue += instance.weight()
146:                                    * instance.classValue();
147:                        }
148:                        sumOfWeights += instance.weight();
149:                    }
150:                }
151:                if (instances.classAttribute().isNumeric()) {
152:                    if (Utils.gr(sumOfWeights, 0)) {
153:                        m_ClassValue /= sumOfWeights;
154:                    }
155:                } else {
156:                    m_ClassValue = Utils.maxIndex(m_Counts);
157:                    Utils.normalize(m_Counts, sumOfWeights);
158:                }
159:            }
160:
161:            /**
162:             * Classifies a given instance.
163:             *
164:             * @param instance the instance to be classified
165:             * @return index of the predicted class
166:             */
167:            public double classifyInstance(Instance instance) {
168:
169:                return m_ClassValue;
170:            }
171:
172:            /**
173:             * Calculates the class membership probabilities for the given test instance.
174:             *
175:             * @param instance the instance to be classified
176:             * @return predicted class probability distribution
177:             * @exception Exception if class is numeric
178:             */
179:            public double[] distributionForInstance(Instance instance)
180:                    throws Exception {
181:
182:                if (m_Counts == null) {
183:                    double[] result = new double[1];
184:                    result[0] = m_ClassValue;
185:                    return result;
186:                } else {
187:                    return (double[]) m_Counts.clone();
188:                }
189:            }
190:
191:            /**
192:             * Returns a description of the classifier.
193:             *
194:             * @return a description of the classifier as a string.
195:             */
196:            public String toString() {
197:
198:                if (m_Class == null) {
199:                    return "ZeroR: No model built yet.";
200:                }
201:                if (m_Counts == null) {
202:                    return "ZeroR predicts class value: " + m_ClassValue;
203:                } else {
204:                    return "ZeroR predicts class value: "
205:                            + m_Class.value((int) m_ClassValue);
206:                }
207:            }
208:
209:            /**
210:             * Main method for testing this class.
211:             *
212:             * @param argv the options
213:             */
214:            public static void main(String[] argv) {
215:                runClassifier(new ZeroR(), argv);
216:            }
217:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.