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: }
|