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.
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.
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.
19
* Copyright 2006 Liangxiao Jiang
22
package weka.classifiers.bayes;
24
import weka.classifiers.Classifier;
25
import weka.core.Capabilities;
26
import weka.core.Instance;
27
import weka.core.Instances;
28
import weka.core.Option;
29
import weka.core.TechnicalInformation;
30
import weka.core.TechnicalInformationHandler;
31
import weka.core.Utils;
32
import weka.core.Capabilities.Capability;
33
import weka.core.TechnicalInformation.Field;
34
import weka.core.TechnicalInformation.Type;
36
import java.util.Enumeration;
37
import java.util.Vector;
40
<!-- globalinfo-start -->
41
* WAODE contructs the model called Weightily Averaged One-Dependence Estimators.<br/>
43
* For more information, see<br/>
45
* L. Jiang, H. Zhang: Weightily Averaged One-Dependence Estimators. In: Proceedings of the 9th Biennial Pacific Rim International Conference on Artificial Intelligence, PRICAI 2006, 970-974, 2006.
47
<!-- globalinfo-end -->
49
<!-- technical-bibtex-start -->
52
* @inproceedings{Jiang2006,
53
* author = {L. Jiang and H. Zhang},
54
* booktitle = {Proceedings of the 9th Biennial Pacific Rim International Conference on Artificial Intelligence, PRICAI 2006},
57
* title = {Weightily Averaged One-Dependence Estimators},
63
<!-- technical-bibtex-end -->
65
<!-- options-start -->
66
* Valid options are: <p/>
69
* If set, classifier is run in debug mode and
70
* may output additional info to the console</pre>
73
* Whether to print some more internals.
78
* @author Liangxiao Jiang (ljiang@cug.edu.cn)
79
* @author H. Zhang (hzhang@unb.ca)
80
* @version $Revision: 1.2 $
84
implements TechnicalInformationHandler {
86
/** for serialization */
87
private static final long serialVersionUID = 2170978824284697882L;
89
/** The number of each class value occurs in the dataset */
90
private double[] m_ClassCounts;
92
/** The number of each attribute value occurs in the dataset */
93
private double[] m_AttCounts;
95
/** The number of two attributes values occurs in the dataset */
96
private double[][] m_AttAttCounts;
98
/** The number of class and two attributes values occurs in the dataset */
99
private double[][][] m_ClassAttAttCounts;
101
/** The number of values for each attribute in the dataset */
102
private int[] m_NumAttValues;
104
/** The number of values for all attributes in the dataset */
105
private int m_TotalAttValues;
107
/** The number of classes in the dataset */
108
private int m_NumClasses;
110
/** The number of attributes including class in the dataset */
111
private int m_NumAttributes;
113
/** The number of instances in the dataset */
114
private int m_NumInstances;
116
/** The index of the class attribute in the dataset */
117
private int m_ClassIndex;
119
/** The starting index of each attribute in the dataset */
120
private int[] m_StartAttIndex;
122
/** The array of mutual information between each attribute and class */
123
private double[] m_mutualInformation;
125
/** the header information of the training data */
126
private Instances m_Header = null;
128
/** whether to print more internals in the toString method
129
* @see #toString() */
130
private boolean m_Internals = false;
132
/** a ZeroR model in case no model can be built from the data */
133
private Classifier m_ZeroR;
136
* Returns a string describing this classifier
138
* @return a description of the classifier suitable for
139
* displaying in the explorer/experimenter gui
141
public String globalInfo() {
143
"WAODE contructs the model called Weightily Averaged One-Dependence "
145
+ "For more information, see\n\n"
146
+ getTechnicalInformation().toString();
150
* Gets an enumeration describing the available options.
152
* @return an enumeration of all the available options.
154
public Enumeration listOptions() {
155
Vector result = new Vector();
156
Enumeration enm = super.listOptions();
157
while (enm.hasMoreElements())
158
result.add(enm.nextElement());
160
result.addElement(new Option(
161
"\tWhether to print some more internals.\n"
165
return result.elements();
170
* Parses a given list of options. <p/>
172
<!-- options-start -->
173
* Valid options are: <p/>
176
* If set, classifier is run in debug mode and
177
* may output additional info to the console</pre>
180
* Whether to print some more internals.
181
* (default: no)</pre>
185
* @param options the list of options as an array of strings
186
* @throws Exception if an option is not supported
188
public void setOptions(String[] options) throws Exception {
189
super.setOptions(options);
191
setInternals(Utils.getFlag('I', options));
195
* Gets the current settings of the filter.
197
* @return an array of strings suitable for passing to setOptions
199
public String[] getOptions() {
204
result = new Vector();
206
options = super.getOptions();
207
for (i = 0; i < options.length; i++)
208
result.add(options[i]);
213
return (String[]) result.toArray(new String[result.size()]);
217
* Returns the tip text for this property
219
* @return tip text for this property suitable for
220
* displaying in the explorer/experimenter gui
222
public String internalsTipText() {
223
return "Prints more internals of the classifier.";
227
* Sets whether internals about classifier are printed via toString().
229
* @param value if internals should be printed
232
public void setInternals(boolean value) {
237
* Gets whether more internals of the classifier are printed.
239
* @return true if more internals are printed
241
public boolean getInternals() {
246
* Returns an instance of a TechnicalInformation object, containing
247
* detailed information about the technical background of this class,
248
* e.g., paper reference or book this class is based on.
250
* @return the technical information about this class
252
public TechnicalInformation getTechnicalInformation() {
253
TechnicalInformation result;
255
result = new TechnicalInformation(Type.INPROCEEDINGS);
256
result.setValue(Field.AUTHOR, "L. Jiang and H. Zhang");
257
result.setValue(Field.TITLE, "Weightily Averaged One-Dependence Estimators");
258
result.setValue(Field.BOOKTITLE, "Proceedings of the 9th Biennial Pacific Rim International Conference on Artificial Intelligence, PRICAI 2006");
259
result.setValue(Field.YEAR, "2006");
260
result.setValue(Field.PAGES, "970-974");
261
result.setValue(Field.SERIES, "LNAI");
262
result.setValue(Field.VOLUME, "4099");
268
* Returns default capabilities of the classifier.
270
* @return the capabilities of this classifier
272
public Capabilities getCapabilities() {
273
Capabilities result = super.getCapabilities();
276
result.enable(Capability.NOMINAL_ATTRIBUTES);
279
result.enable(Capability.NOMINAL_CLASS);
285
* Generates the classifier.
287
* @param instances set of instances serving as training data
288
* @throws Exception if the classifier has not been generated successfully
290
public void buildClassifier(Instances instances) throws Exception {
292
// can classifier handle the data?
293
getCapabilities().testWithFail(instances);
295
// only class? -> build ZeroR model
296
if (instances.numAttributes() == 1) {
298
"Cannot build model (only class attribute present in data!), "
299
+ "using ZeroR model instead!");
300
m_ZeroR = new weka.classifiers.rules.ZeroR();
301
m_ZeroR.buildClassifier(instances);
309
m_NumClasses = instances.numClasses();
310
m_ClassIndex = instances.classIndex();
311
m_NumAttributes = instances.numAttributes();
312
m_NumInstances = instances.numInstances();
313
m_TotalAttValues = 0;
315
// allocate space for attribute reference arrays
316
m_StartAttIndex = new int[m_NumAttributes];
317
m_NumAttValues = new int[m_NumAttributes];
319
// set the starting index of each attribute and the number of values for
320
// each attribute and the total number of values for all attributes (not including class).
321
for (int i = 0; i < m_NumAttributes; i++) {
322
if (i != m_ClassIndex) {
323
m_StartAttIndex[i] = m_TotalAttValues;
324
m_NumAttValues[i] = instances.attribute(i).numValues();
325
m_TotalAttValues += m_NumAttValues[i];
328
m_StartAttIndex[i] = -1;
329
m_NumAttValues[i] = m_NumClasses;
333
// allocate space for counts and frequencies
334
m_ClassCounts = new double[m_NumClasses];
335
m_AttCounts = new double[m_TotalAttValues];
336
m_AttAttCounts = new double[m_TotalAttValues][m_TotalAttValues];
337
m_ClassAttAttCounts = new double[m_NumClasses][m_TotalAttValues][m_TotalAttValues];
338
m_Header = new Instances(instances, 0);
340
// Calculate the counts
341
for (int k = 0; k < m_NumInstances; k++) {
342
int classVal=(int)instances.instance(k).classValue();
343
m_ClassCounts[classVal] ++;
344
int[] attIndex = new int[m_NumAttributes];
345
for (int i = 0; i < m_NumAttributes; i++) {
346
if (i == m_ClassIndex){
350
attIndex[i] = m_StartAttIndex[i] + (int)instances.instance(k).value(i);
351
m_AttCounts[attIndex[i]]++;
354
for (int Att1 = 0; Att1 < m_NumAttributes; Att1++) {
355
if (attIndex[Att1] == -1) continue;
356
for (int Att2 = 0; Att2 < m_NumAttributes; Att2++) {
357
if ((attIndex[Att2] != -1)) {
358
m_AttAttCounts[attIndex[Att1]][attIndex[Att2]] ++;
359
m_ClassAttAttCounts[classVal][attIndex[Att1]][attIndex[Att2]] ++;
365
//compute mutual information between each attribute and class
366
m_mutualInformation=new double[m_NumAttributes];
367
for (int att=0;att<m_NumAttributes;att++){
368
if (att == m_ClassIndex) continue;
369
m_mutualInformation[att]=mutualInfo(att);
374
* Computes mutual information between each attribute and class attribute.
376
* @param att is the attribute
377
* @return the conditional mutual information between son and parent given class
379
private double mutualInfo(int att) {
382
int attIndex=m_StartAttIndex[att];
383
double[] PriorsClass = new double[m_NumClasses];
384
double[] PriorsAttribute = new double[m_NumAttValues[att]];
385
double[][] PriorsClassAttribute=new double[m_NumClasses][m_NumAttValues[att]];
387
for (int i=0;i<m_NumClasses;i++){
388
PriorsClass[i]=m_ClassCounts[i]/m_NumInstances;
391
for (int j=0;j<m_NumAttValues[att];j++){
392
PriorsAttribute[j]=m_AttCounts[attIndex+j]/m_NumInstances;
395
for (int i=0;i<m_NumClasses;i++){
396
for (int j=0;j<m_NumAttValues[att];j++){
397
PriorsClassAttribute[i][j]=m_ClassAttAttCounts[i][attIndex+j][attIndex+j]/m_NumInstances;
401
for (int i=0;i<m_NumClasses;i++){
402
for (int j=0;j<m_NumAttValues[att];j++){
403
mutualInfo+=PriorsClassAttribute[i][j]*log2(PriorsClassAttribute[i][j],PriorsClass[i]*PriorsAttribute[j]);
410
* compute the logarithm whose base is 2.
412
* @param x numerator of the fraction.
413
* @param y denominator of the fraction.
414
* @return the natual logarithm of this fraction.
416
private double log2(double x,double y){
418
if (x < Utils.SMALL || y < Utils.SMALL)
421
return Math.log(x/y)/Math.log(2);
425
* Calculates the class membership probabilities for the given test instance
427
* @param instance the instance to be classified
428
* @return predicted class probability distribution
429
* @throws Exception if there is a problem generating the prediction
431
public double[] distributionForInstance(Instance instance) throws Exception {
434
if (m_ZeroR != null) {
435
return m_ZeroR.distributionForInstance(instance);
438
//Definition of local variables
439
double[] probs = new double[m_NumClasses];
441
double mutualInfoSum;
443
// store instance's att values in an int array
444
int[] attIndex = new int[m_NumAttributes];
445
for (int att = 0; att < m_NumAttributes; att++) {
446
if (att == m_ClassIndex)
449
attIndex[att] = m_StartAttIndex[att] + (int)instance.value(att);
452
// calculate probabilities for each possible class value
453
for (int classVal = 0; classVal < m_NumClasses; classVal++) {
457
for (int parent = 0; parent < m_NumAttributes; parent++) {
458
if (attIndex[parent]==-1) continue;
459
prob=(m_ClassAttAttCounts[classVal][attIndex[parent]][attIndex[parent]] + 1.0/(m_NumClasses*m_NumAttValues[parent]))/(m_NumInstances + 1.0);
460
for (int son = 0; son < m_NumAttributes; son++) {
461
if (attIndex[son]==-1 || son == parent) continue;
462
prob*=(m_ClassAttAttCounts[classVal][attIndex[parent]][attIndex[son]] + 1.0/m_NumAttValues[son])/(m_ClassAttAttCounts[classVal][attIndex[parent]][attIndex[parent]] + 1.0);
464
mutualInfoSum+=m_mutualInformation[parent];
465
probs[classVal]+=m_mutualInformation[parent]*prob;
467
probs[classVal]/=mutualInfoSum;
469
if (!Double.isNaN(Utils.sum(probs)))
470
Utils.normalize(probs);
475
* returns a string representation of the classifier
477
* @return string representation of the classifier
479
public String toString() {
485
if (m_ZeroR != null) {
486
result = new StringBuffer();
487
result.append(this.getClass().getName().replaceAll(".*\\.", "") + "\n");
488
result.append(this.getClass().getName().replaceAll(".*\\.", "").replaceAll(".", "=") + "\n\n");
489
result.append("Warning: No model could be built, hence ZeroR model is used:\n\n");
490
result.append(m_ZeroR.toString());
493
classname = this.getClass().getName().replaceAll(".*\\.", "");
494
result = new StringBuffer();
495
result.append(classname + "\n");
496
result.append(classname.replaceAll(".", "=") + "\n\n");
498
if (m_Header == null) {
499
result.append("No Model built yet.\n");
502
if (getInternals()) {
503
result.append("Mutual information of attributes with class attribute:\n");
504
for (i = 0; i < m_Header.numAttributes(); i++) {
506
if (i == m_Header.classIndex())
510
(i+1) + ". " + m_Header.attribute(i).name() + ": "
511
+ Utils.doubleToString(m_mutualInformation[i], 6) + "\n");
515
result.append("Model built successfully.\n");
520
return result.toString();
524
* Main method for testing this class.
526
* @param argv the commandline options, use -h to list all options
528
public static void main(String[] argv) {
529
runClassifier(new WAODE(), argv);