source: src/main/java/weka/classifiers/trees/j48/EntropySplitCrit.java @ 27

Last change on this file since 27 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 2.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 *    EntropySplitCrit.java
19 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.classifiers.trees.j48;
24
25import weka.core.RevisionUtils;
26import weka.core.Utils;
27
28/**
29 * Class for computing the entropy for a given distribution.
30 *
31 * @author Eibe Frank (eibe@cs.waikato.ac.nz)
32 * @version $Revision: 1.8 $
33 */
34public final class EntropySplitCrit
35  extends EntropyBasedSplitCrit {
36
37  /** for serialization */
38  private static final long serialVersionUID = 5986252682266803935L;
39
40  /**
41   * Computes entropy for given distribution.
42   */
43  public final double splitCritValue(Distribution bags) {
44   
45    return newEnt(bags);
46  }
47
48  /**
49   * Computes entropy of test distribution with respect to training distribution.
50   */
51  public final double splitCritValue(Distribution train, Distribution test) {
52
53    double result = 0;
54    int numClasses = 0;
55    int i, j;
56   
57    // Find out relevant number of classes
58    for (j = 0; j < test.numClasses(); j++)
59      if (Utils.gr(train.perClass(j), 0) || Utils.gr(test.perClass(j), 0))
60        numClasses++;
61
62    // Compute entropy of test data with respect to training data
63    for (i = 0; i < test.numBags(); i++)
64      if (Utils.gr(test.perBag(i),0)) {
65        for (j = 0; j < test.numClasses(); j++)
66          if (Utils.gr(test.perClassPerBag(i, j), 0))
67            result -= test.perClassPerBag(i, j)*
68              Math.log(train.perClassPerBag(i, j) + 1);
69        result += test.perBag(i) * Math.log(train.perBag(i) + numClasses);
70      }
71 
72    return result / log2;
73  }
74 
75  /**
76   * Returns the revision string.
77   *
78   * @return            the revision
79   */
80  public String getRevision() {
81    return RevisionUtils.extract("$Revision: 1.8 $");
82  }
83}
Note: See TracBrowser for help on using the repository browser.