source: branches/MetisMQI/src/main/java/weka/classifiers/bayes/HNB.java @ 38

Last change on this file since 38 was 29, checked in by gnappo, 14 years ago

Taggata versione per la demo e aggiunto branch.

File size: 12.4 KB
Line 
1/*
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.
6 *
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.
11 *
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.
15 */
16
17/*
18 *    HNB.java
19 *    Copyright (C) 2004 Liangxiao Jiang
20 */
21
22package weka.classifiers.bayes;
23
24import weka.classifiers.Classifier;
25import weka.classifiers.AbstractClassifier;
26import weka.core.Capabilities;
27import weka.core.Instance;
28import weka.core.Instances;
29import weka.core.RevisionUtils;
30import weka.core.TechnicalInformation;
31import weka.core.TechnicalInformationHandler;
32import weka.core.Utils;
33import weka.core.Capabilities.Capability;
34import weka.core.TechnicalInformation.Field;
35import weka.core.TechnicalInformation.Type;
36
37/**
38 <!-- globalinfo-start -->
39 * Contructs Hidden Naive Bayes classification model with high classification accuracy and AUC.<br/>
40 * <br/>
41 * For more information refer to:<br/>
42 * <br/>
43 * H. Zhang, L. Jiang, J. Su: Hidden Naive Bayes. In: Twentieth National Conference on Artificial Intelligence, 919-924, 2005.
44 * <p/>
45 <!-- globalinfo-end -->
46 *
47 <!-- technical-bibtex-start -->
48 * BibTeX:
49 * <pre>
50 * &#64;inproceedings{Zhang2005,
51 *    author = {H. Zhang and L. Jiang and J. Su},
52 *    booktitle = {Twentieth National Conference on Artificial Intelligence},
53 *    pages = {919-924},
54 *    publisher = {AAAI Press},
55 *    title = {Hidden Naive Bayes},
56 *    year = {2005}
57 * }
58 * </pre>
59 * <p/>
60 <!-- technical-bibtex-end -->
61 *
62 <!-- options-start -->
63 * Valid options are: <p/>
64 *
65 * <pre> -D
66 *  If set, classifier is run in debug mode and
67 *  may output additional info to the console</pre>
68 *
69 <!-- options-end -->
70 *
71 * @author H. Zhang (hzhang@unb.ca)
72 * @author Liangxiao Jiang (ljiang@cug.edu.cn)
73 * @version $Revision: 5928 $
74 */
75public class HNB 
76  extends AbstractClassifier
77  implements TechnicalInformationHandler {
78
79  /** for serialization */
80  static final long serialVersionUID = -4503874444306113214L;
81 
82  /** The number of each class value occurs in the dataset */
83  private double [] m_ClassCounts;
84
85  /** The number of class and two attributes values occurs in the dataset */
86  private double [][][] m_ClassAttAttCounts;
87
88  /** The number of values for each attribute in the dataset */
89  private int [] m_NumAttValues;
90
91  /** The number of values for all attributes in the dataset */
92  private int m_TotalAttValues;
93
94  /** The number of classes in the dataset */
95  private int m_NumClasses;
96
97  /** The number of attributes including class in the dataset */
98  private int m_NumAttributes;
99
100  /** The number of instances in the dataset */
101  private int m_NumInstances;
102
103  /** The index of the class attribute in the dataset */
104  private int m_ClassIndex;
105
106  /** The starting index of each attribute in the dataset */
107  private int[] m_StartAttIndex;
108
109  /** The 2D array of conditional mutual information of each pair attributes */
110  private double[][] m_condiMutualInfo;
111
112  /**
113   * Returns a string describing this classifier.
114   *
115   * @return a description of the data generator suitable for
116   * displaying in the explorer/experimenter gui
117   */
118  public String globalInfo() {
119
120    return
121      "Contructs Hidden Naive Bayes classification model with high "
122      + "classification accuracy and AUC.\n\n"
123      + "For more information refer to:\n\n"
124      + getTechnicalInformation().toString();
125  }
126
127  /**
128   * Returns an instance of a TechnicalInformation object, containing
129   * detailed information about the technical background of this class,
130   * e.g., paper reference or book this class is based on.
131   *
132   * @return the technical information about this class
133   */
134  public TechnicalInformation getTechnicalInformation() {
135    TechnicalInformation        result;
136   
137    result = new TechnicalInformation(Type.INPROCEEDINGS);
138    result.setValue(Field.AUTHOR, "H. Zhang and L. Jiang and J. Su");
139    result.setValue(Field.TITLE, "Hidden Naive Bayes");
140    result.setValue(Field.BOOKTITLE, "Twentieth National Conference on Artificial Intelligence");
141    result.setValue(Field.YEAR, "2005");
142    result.setValue(Field.PAGES, "919-924");
143    result.setValue(Field.PUBLISHER, "AAAI Press");
144   
145    return result;
146  }
147
148  /**
149   * Returns default capabilities of the classifier.
150   *
151   * @return      the capabilities of this classifier
152   */
153  public Capabilities getCapabilities() {
154    Capabilities result = super.getCapabilities();
155    result.disableAll();
156
157    // attributes
158    result.enable(Capability.NOMINAL_ATTRIBUTES);
159
160    // class
161    result.enable(Capability.NOMINAL_CLASS);
162    result.enable(Capability.MISSING_CLASS_VALUES);
163   
164    return result;
165  }
166
167  /**
168   * Generates the classifier.
169   *
170   * @param instances set of instances serving as training data
171   * @exception Exception if the classifier has not been generated successfully
172   */
173  public void buildClassifier(Instances instances) throws Exception {
174
175    // can classifier handle the data?
176    getCapabilities().testWithFail(instances);
177
178    // remove instances with missing class
179    instances = new Instances(instances);
180    instances.deleteWithMissingClass();
181   
182    // reset variable
183    m_NumClasses = instances.numClasses();
184    m_ClassIndex = instances.classIndex();
185    m_NumAttributes = instances.numAttributes();
186    m_NumInstances = instances.numInstances();
187    m_TotalAttValues = 0;
188
189    // allocate space for attribute reference arrays
190    m_StartAttIndex = new int[m_NumAttributes];
191    m_NumAttValues = new int[m_NumAttributes];
192
193    // set the starting index of each attribute and the number of values for
194    // each attribute and the total number of values for all attributes (not including class).
195    for(int i = 0; i < m_NumAttributes; i++) {
196      if(i != m_ClassIndex) {
197        m_StartAttIndex[i] = m_TotalAttValues;
198        m_NumAttValues[i] = instances.attribute(i).numValues();
199        m_TotalAttValues += m_NumAttValues[i];
200      }
201      else {
202        m_StartAttIndex[i] = -1;
203        m_NumAttValues[i] = m_NumClasses;
204      }
205    }
206
207    // allocate space for counts and frequencies
208    m_ClassCounts = new double[m_NumClasses];
209    m_ClassAttAttCounts = new double[m_NumClasses][m_TotalAttValues][m_TotalAttValues];
210
211    // Calculate the counts
212    for(int k = 0; k < m_NumInstances; k++) {
213      int classVal=(int)instances.instance(k).classValue();
214      m_ClassCounts[classVal] ++;
215      int[] attIndex = new int[m_NumAttributes];
216      for(int i = 0; i < m_NumAttributes; i++) {
217        if(i == m_ClassIndex)
218          attIndex[i] = -1;
219        else
220          attIndex[i] = m_StartAttIndex[i] + (int)instances.instance(k).value(i);
221      }
222      for(int Att1 = 0; Att1 < m_NumAttributes; Att1++) {
223        if(attIndex[Att1] == -1) continue;
224        for(int Att2 = 0; Att2 < m_NumAttributes; Att2++) {
225          if((attIndex[Att2] != -1)) {
226            m_ClassAttAttCounts[classVal][attIndex[Att1]][attIndex[Att2]] ++;
227          }
228        }
229      }
230    }
231
232    //compute conditional mutual information of each pair attributes (not including class)
233    m_condiMutualInfo=new double[m_NumAttributes][m_NumAttributes];
234    for(int son=0;son<m_NumAttributes;son++){
235      if(son == m_ClassIndex) continue;
236      for(int parent=0;parent<m_NumAttributes;parent++){
237        if(parent == m_ClassIndex || son==parent) continue;
238        m_condiMutualInfo[son][parent]=conditionalMutualInfo(son,parent);
239      }
240    }
241  }
242
243  /**
244   * Computes conditional mutual information between a pair of attributes.
245   *
246   * @param son the son attribute
247   * @param parent the parent attribute
248   * @return the conditional mutual information between son and parent given class
249   * @throws Exception if computation fails
250   */
251  private double conditionalMutualInfo(int son, int parent) throws Exception{
252
253    double CondiMutualInfo=0;
254    int sIndex=m_StartAttIndex[son];
255    int pIndex=m_StartAttIndex[parent];
256    double[] PriorsClass = new double[m_NumClasses];
257    double[][] PriorsClassSon=new double[m_NumClasses][m_NumAttValues[son]];
258    double[][] PriorsClassParent=new double[m_NumClasses][m_NumAttValues[parent]];
259    double[][][] PriorsClassParentSon=new double[m_NumClasses][m_NumAttValues[parent]][m_NumAttValues[son]];
260
261    for(int i=0;i<m_NumClasses;i++){
262      PriorsClass[i]=m_ClassCounts[i]/m_NumInstances;
263    }
264
265    for(int i=0;i<m_NumClasses;i++){
266      for(int j=0;j<m_NumAttValues[son];j++){
267        PriorsClassSon[i][j]=m_ClassAttAttCounts[i][sIndex+j][sIndex+j]/m_NumInstances;
268      }
269    }
270
271    for(int i=0;i<m_NumClasses;i++){
272      for(int j=0;j<m_NumAttValues[parent];j++){
273        PriorsClassParent[i][j]=m_ClassAttAttCounts[i][pIndex+j][pIndex+j]/m_NumInstances;
274      }
275    }
276
277    for(int i=0;i<m_NumClasses;i++){
278      for(int j=0;j<m_NumAttValues[parent];j++){
279        for(int k=0;k<m_NumAttValues[son];k++){
280          PriorsClassParentSon[i][j][k]=m_ClassAttAttCounts[i][pIndex+j][sIndex+k]/m_NumInstances;
281        }
282      }
283    }
284
285    for(int i=0;i<m_NumClasses;i++){
286      for(int j=0;j<m_NumAttValues[parent];j++){
287        for(int k=0;k<m_NumAttValues[son];k++){
288          CondiMutualInfo+=PriorsClassParentSon[i][j][k]*log2(PriorsClassParentSon[i][j][k]*PriorsClass[i],PriorsClassParent[i][j]*PriorsClassSon[i][k]);
289        }
290      }
291    }
292    return CondiMutualInfo;
293  }
294
295  /**
296   * compute the logarithm whose base is 2.
297   *
298   * @param x numerator of the fraction.
299   * @param y denominator of the fraction.
300   * @return the natual logarithm of this fraction.
301   */
302  private double log2(double x,double y){
303
304    if(x<1e-6||y<1e-6)
305      return 0.0;
306    else
307      return Math.log(x/y)/Math.log(2);
308  }
309
310  /**
311   * Calculates the class membership probabilities for the given test instance
312   *
313   * @param instance the instance to be classified
314   * @return predicted class probability distribution
315   * @exception Exception if there is a problem generating the prediction
316   */
317  public double[] distributionForInstance(Instance instance) throws Exception {
318
319    //Definition of local variables
320    double[] probs = new double[m_NumClasses];
321    int sIndex;
322    double prob;
323    double condiMutualInfoSum;
324
325    // store instance's att values in an int array
326    int[] attIndex = new int[m_NumAttributes];
327    for(int att = 0; att < m_NumAttributes; att++) {
328      if(att == m_ClassIndex)
329        attIndex[att] = -1;
330      else
331        attIndex[att] = m_StartAttIndex[att] + (int)instance.value(att);
332    }
333
334    // calculate probabilities for each possible class value
335    for(int classVal = 0; classVal < m_NumClasses; classVal++) {
336      probs[classVal]=(m_ClassCounts[classVal]+1.0/m_NumClasses)/(m_NumInstances+1.0);
337      for(int son = 0; son < m_NumAttributes; son++) {
338        if(attIndex[son]==-1) continue;
339        sIndex=attIndex[son];
340        attIndex[son]=-1;
341        prob=0;
342        condiMutualInfoSum=0;
343        for(int parent=0; parent<m_NumAttributes; parent++) {
344          if(attIndex[parent]==-1) continue;
345          condiMutualInfoSum+=m_condiMutualInfo[son][parent];
346          prob+=m_condiMutualInfo[son][parent]*(m_ClassAttAttCounts[classVal][attIndex[parent]][sIndex]+1.0/m_NumAttValues[son])/(m_ClassAttAttCounts[classVal][attIndex[parent]][attIndex[parent]] + 1.0);
347        }
348        if(condiMutualInfoSum>0){
349          prob=prob/condiMutualInfoSum;
350          probs[classVal] *= prob;
351        }
352        else{
353          prob=(m_ClassAttAttCounts[classVal][sIndex][sIndex]+1.0/m_NumAttValues[son])/(m_ClassCounts[classVal]+1.0);
354          probs[classVal]*= prob;
355        }
356        attIndex[son] = sIndex;
357      }
358    }
359    Utils.normalize(probs);
360    return probs;
361  }
362
363  /**
364   * returns a string representation of the classifier
365   *
366   * @return a representation of the classifier
367   */
368  public String toString() {
369
370    return "HNB (Hidden Naive Bayes)";
371  }
372 
373  /**
374   * Returns the revision string.
375   *
376   * @return            the revision
377   */
378  public String getRevision() {
379    return RevisionUtils.extract("$Revision: 5928 $");
380  }
381
382  /**
383   * Main method for testing this class.
384   *
385   * @param args the options
386   */
387  public static void main(String[] args) {
388    runClassifier(new HNB(), args);
389  }
390}
391
Note: See TracBrowser for help on using the repository browser.