source: branches/MetisMQI/src/main/java/weka/classifiers/trees/m5/YongSplitInfo.java

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

Taggata versione per la demo e aggiunto branch.

File size: 5.6 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 *    YongSplitInfo.java
19 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.classifiers.trees.m5;
24
25import weka.core.Instances;
26import weka.core.RevisionHandler;
27import weka.core.RevisionUtils;
28import weka.core.Utils;
29
30import java.io.Serializable;
31
32/**
33 * Stores split information.
34 *
35 * @author Yong Wang (yongwang@cs.waikato.ac.nz)
36 * @author Mark Hall (mhall@cs.waikato.ac.nz)
37 * @version $Revision: 1.4 $
38 */
39public final class YongSplitInfo
40  implements Cloneable, Serializable, SplitEvaluate, RevisionHandler {
41
42  /** for serialization */
43  private static final long serialVersionUID = 1864267581079767881L;
44
45  private int  number;         // number of total instances
46  private int  first;          // first instance index
47  private int  last;           // last instance index
48  private int  position;       // position of maximum impurity reduction
49  private double  maxImpurity; // maximum impurity reduction
50  private double  leftAve;     // left average class value
51  private double rightAve;     // right average class value
52  private int  splitAttr;      // spliting attribute
53  private double  splitValue;  // splitting value
54
55  /**
56   * Constructs an object which contains the split information
57   * @param low the index of the first instance
58   * @param high the index of the last instance
59   * @param attr an attribute
60   */
61  public YongSplitInfo(int low,int high,int attr) {
62    number = high-low+1;
63    first = low;
64    last = high;
65    position = -1;
66    maxImpurity = -1.e20;
67    splitAttr = attr;      // attr < 0 is an empty object
68    splitValue = 0.0;
69    Utils.SMALL = 1e-10;
70  }
71
72  /**
73   * Makes a copy of this SplitInfo object
74   */ 
75  public final SplitEvaluate copy () throws Exception {
76
77    YongSplitInfo s = (YongSplitInfo)this.clone();
78   
79    return s;
80  }
81
82  /**
83   * Resets the object of split information
84   * @param low the index of the first instance
85   * @param high the index of the last instance
86   * @param attr the attribute
87   */
88  public final void  initialize(int low,int high,int attr){
89     
90    number = high-low+1;
91    first = low;
92    last = high;
93    position = -1;
94    maxImpurity = -1.e20;
95    splitAttr = attr;
96    splitValue = 0.0;
97  }
98
99  /**
100   * Converts the spliting information to string
101   * @param inst the instances
102   */
103  public final String  toString(Instances inst){
104
105    StringBuffer text =  new StringBuffer();
106
107    text.append("Print SplitInfo:\n");
108    text.append("    Instances:\t\t" + number + " (" + first + "-" + 
109                position + "," + (position+1) + "-" + last + ")\n");
110    text.append("    Maximum Impurity Reduction:\t" + 
111                Utils.doubleToString(maxImpurity,1,4) + "\n");
112    text.append("    Left average:\t" + leftAve + "\n");
113    text.append("    Right average:\t" + rightAve + "\n");
114    if(maxImpurity>0.0)
115      text.append("    Splitting function:\t" + 
116                  inst.attribute(splitAttr).name() + " = " 
117                  + splitValue + "\n");
118    else text.append("    Splitting function:\tnull\n");
119   
120    return text.toString();
121  }
122 
123  /**
124   * Finds the best splitting point for an attribute in the instances
125   * @param attr the splitting attribute
126   * @param inst the instances
127   * @exception Exception if something goes wrong
128   */
129  public final void  attrSplit(int attr,Instances inst) throws Exception {
130    int i,len,count,part;
131    Impurity imp;
132   
133    int low = 0;
134    int high = inst.numInstances()-1;
135    this.initialize(low,high,attr);
136    if(number < 4) {
137      return;
138    }
139   
140    len = ((high-low+1)<5) ? 1 : (high-low+1) / 5; 
141   
142    position = low;
143   
144    part = low + len - 1;
145    imp = new Impurity(part,attr,inst,5);
146   
147    count=0;
148    for(i=low+len;i<=high-len-1;i++) {
149     
150      imp.incremental(inst.instance(i).classValue(),1);
151     
152      if(Utils.eq(inst.instance(i+1).value(attr),
153                  inst.instance(i).value(attr)) == false) {
154        count = i;
155        if(imp.impurity > maxImpurity){
156          maxImpurity = imp.impurity;
157          splitValue = (inst.instance(i).value(attr) +
158                        inst.instance(i+1).value(attr)) * 0.5;
159          leftAve = imp.sl / imp.nl; 
160          rightAve = imp.sr / imp.nr; 
161          position=i;
162        }
163      } 
164    }
165  }
166
167  /**
168   * Returns the impurity of this split
169   *
170   * @return the impurity of this split
171   */
172  public double maxImpurity () {
173    return maxImpurity;
174  }
175
176  /**
177   * Returns the attribute used in this split
178   *
179   * @return the attribute used in this split
180   */
181  public int splitAttr () {
182    return splitAttr;
183  }
184
185  /**
186   * Returns the position of the split in the sorted values. -1 indicates that
187   * a split could not be found.
188   *
189   * @return an <code>int</code> value
190   */
191  public int position () {
192    return position;
193  }
194
195  /**
196   * Returns the split value
197   *
198   * @return the split value
199   */
200  public double splitValue () {
201    return splitValue;
202  }
203 
204  /**
205   * Returns the revision string.
206   *
207   * @return            the revision
208   */
209  public String getRevision() {
210    return RevisionUtils.extract("$Revision: 1.4 $");
211  }
212}
Note: See TracBrowser for help on using the repository browser.