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 (C) 2007 Geoff Webb & Janice Boughton
20
* a split object for nodes added to a tree during grafting.
21
* (used in classifier J48g).
24
package weka.classifiers.trees.j48;
29
* Class implementing a split for nodes added to a tree during grafting.
31
* @author Janice Boughton (jrbought@infotech.monash.edu.au)
32
* @version $Revision 1.0 $
34
public class GraftSplit extends ClassifierSplitModel implements Comparable {
36
/** the distribution for graft values, from cases in atbop */
37
private Distribution m_graftdistro;
39
/** the attribute we are splitting on */
40
private int m_attIndex;
42
/** value of split point (if numeric attribute) */
43
private double m_splitPoint;
45
/** dominant class of the subset specified by m_testType */
46
private int m_maxClass;
48
/** dominant class of the subset not specified by m_testType */
49
private int m_otherLeafMaxClass;
51
/** laplace value of the subset specified by m_testType for m_maxClass */
52
private double m_laplace;
54
/** leaf for the subset specified by m_testType */
55
private Distribution m_leafdistro;
64
private int m_testType;
70
* @param a the attribute to split on
71
* @param v the value of a where split occurs
72
* @param t the test type (0 is <=, 1 is >, 2 is =, 3 is !)
73
* @param c the class to label the leaf node pointed to by test as.
74
* @param l the laplace value (needed when sorting GraftSplits)
76
public GraftSplit(int a, double v, int t, double c, double l) {
89
* @param a the attribute to split on
90
* @param v the value of a where split occurs
91
* @param t the test type (0 is <=, 1 is >, 2 is =, 3 is !=)
92
* @param oC the class to label the leaf node not pointed to by test as.
93
* @param counts the distribution for this split
95
public GraftSplit(int a, double v, int t, double oC, double [][] counts)
100
m_otherLeafMaxClass = (int)oC;
102
// only deal with binary cuts (<= and >; = and !=)
105
// which subset are we looking at for the graft?
106
int subset = subsetOfInterest(); // this is the subset for m_leaf
108
// create graft distribution, based on counts
109
m_distribution = new Distribution(counts);
111
// create a distribution object for m_leaf
112
double [][] lcounts = new double[1][m_distribution.numClasses()];
113
for(int c = 0; c < lcounts[0].length; c++) {
114
lcounts[0][c] = counts[subset][c];
116
m_leafdistro = new Distribution(lcounts);
119
m_maxClass = m_distribution.maxClass(subset);
121
// set the laplace value (assumes binary class) for subset of interest
122
m_laplace = (m_distribution.perClassPerBag(subset, m_maxClass) + 1.0)
123
/ (m_distribution.perBag(subset) + 2.0);
128
* deletes the cases in data that belong to leaf pointed to by
129
* the test (i.e. the subset of interest). this is useful so
130
* the instances belonging to that leaf aren't passed down the
133
* @param data the instances to delete from
135
public void deleteGraftedCases(Instances data) {
137
int subOfInterest = subsetOfInterest();
138
for(int x = 0; x < data.numInstances(); x++) {
139
if(whichSubset(data.instance(x)) == subOfInterest) {
147
* builds m_graftdistro using the passed data
149
* @param data the instances to use when creating the distribution
151
public void buildClassifier(Instances data) throws Exception {
153
// distribution for the graft, not counting cases in atbop, only orig leaf
154
m_graftdistro = new Distribution(2, data.numClasses());
156
// which subset are we looking at for the graft?
157
int subset = subsetOfInterest(); // this is the subset for m_leaf
159
double thisNodeCount = 0;
160
double knownCases = 0;
161
boolean allKnown = true;
162
// populate distribution
163
for(int x = 0; x < data.numInstances(); x++) {
164
Instance instance = data.instance(x);
165
if(instance.isMissing(m_attIndex)) {
169
knownCases += instance.weight();
170
int subst = whichSubset(instance);
173
m_graftdistro.add(subst, instance);
174
if(subst == subset) { // instance belongs at m_leaf
175
thisNodeCount += instance.weight();
178
double factor = (knownCases == 0) ? (1.0 / (double)2.0)
179
: (thisNodeCount / knownCases);
181
for(int x = 0; x < data.numInstances(); x++) {
182
if(data.instance(x).isMissing(m_attIndex)) {
183
Instance instance = data.instance(x);
184
int subst = whichSubset(instance);
187
instance.setWeight(instance.weight() * factor);
188
m_graftdistro.add(subst, instance);
193
// if there are no cases at the leaf, make sure the desired
194
// class is chosen, by setting counts to 0.01
195
if(m_graftdistro.perBag(subset) == 0) {
196
double [] counts = new double[data.numClasses()];
197
counts[m_maxClass] = 0.01;
198
m_graftdistro.add(subset, counts);
200
if(m_graftdistro.perBag((subset == 0) ? 1 : 0) == 0) {
201
double [] counts = new double[data.numClasses()];
202
counts[(int)m_otherLeafMaxClass] = 0.01;
203
m_graftdistro.add((subset == 0) ? 1 : 0, counts);
209
* @return the NoSplit object for the leaf pointed to by m_testType branch
211
public NoSplit getLeaf() {
212
return new NoSplit(m_leafdistro);
217
* @return the NoSplit object for the leaf not pointed to by m_testType branch
219
public NoSplit getOtherLeaf() {
221
// the bag (subset) that isn't pointed to by m_testType branch
222
int bag = (subsetOfInterest() == 0) ? 1 : 0;
224
double [][] counts = new double[1][m_graftdistro.numClasses()];
226
for(int c = 0; c < counts[0].length; c++) {
227
counts[0][c] = m_graftdistro.perClassPerBag(bag, c);
228
totals += counts[0][c];
230
// if empty, make sure proper class gets chosen
232
counts[0][m_otherLeafMaxClass] += 0.01;
234
return new NoSplit(new Distribution(counts));
239
* Prints label for subset index of instances (eg class).
241
* @param index the bag to dump label for
242
* @param data to get attribute names and such
243
* @return the label as a string
244
* @exception Exception if something goes wrong
246
public final String dumpLabelG(int index, Instances data) throws Exception {
250
text = new StringBuffer();
251
text.append(((Instances)data).classAttribute().
252
value((index==subsetOfInterest()) ? m_maxClass : m_otherLeafMaxClass));
253
text.append(" ("+Utils.roundDouble(m_graftdistro.perBag(index),1));
254
if(Utils.gr(m_graftdistro.numIncorrect(index),0))
256
+Utils.roundDouble(m_graftdistro.numIncorrect(index),2));
258
// show the graft values, only if this is subsetOfInterest()
259
if(index == subsetOfInterest()) {
260
text.append("|"+Utils.roundDouble(m_distribution.perBag(index),2));
261
if(Utils.gr(m_distribution.numIncorrect(index),0))
263
+Utils.roundDouble(m_distribution.numIncorrect(index),2));
266
return text.toString();
271
* @return the subset that is specified by the test type
273
public int subsetOfInterest() {
283
* @return the number of positive cases in the subset of interest
285
public double positivesForSubsetOfInterest() {
286
return (m_distribution.perClassPerBag(subsetOfInterest(), m_maxClass));
291
* @param subset the subset to get the positives for
292
* @return the number of positive cases in the specified subset
294
public double positives(int subset) {
295
return (m_distribution.perClassPerBag(subset,
296
m_distribution.maxClass(subset)));
301
* @return the number of instances in the subset of interest
303
public double totalForSubsetOfInterest() {
304
return (m_distribution.perBag(subsetOfInterest()));
309
* @param subset the index of the bag to get the total for
310
* @return the number of instances in the subset
312
public double totalForSubset(int subset) {
313
return (m_distribution.perBag(subset));
318
* Prints left side of condition satisfied by instances.
320
* @param data the data.
322
public String leftSide(Instances data) {
323
return data.attribute(m_attIndex).name();
328
* @return the index of the attribute to split on
330
public int attribute() {
336
* Prints condition satisfied by instances in subset index.
338
public final String rightSide(int index, Instances data) {
342
text = new StringBuffer();
343
if(data.attribute(m_attIndex).isNominal())
346
data.attribute(m_attIndex).value((int)m_splitPoint));
349
data.attribute(m_attIndex).value((int)m_splitPoint));
353
Utils.doubleToString(m_splitPoint,6));
356
Utils.doubleToString(m_splitPoint,6));
357
return text.toString();
362
* Returns a string containing java source code equivalent to the test
363
* made at this node. The instance being tested is called "i".
365
* @param index index of the nominal value tested
366
* @param data the data containing instance structure info
367
* @return a value of type 'String'
369
public final String sourceExpression(int index, Instances data) {
371
StringBuffer expr = null;
373
return "i[" + m_attIndex + "] == null";
375
if(data.attribute(m_attIndex).isNominal()) {
377
expr = new StringBuffer("i[");
379
expr = new StringBuffer("!i[");
380
expr.append(m_attIndex).append("]");
381
expr.append(".equals(\"").append(data.attribute(m_attIndex)
382
.value((int)m_splitPoint)).append("\")");
384
expr = new StringBuffer("((Double) i[");
385
expr.append(m_attIndex).append("])");
387
expr.append(".doubleValue() <= ").append(m_splitPoint);
389
expr.append(".doubleValue() > ").append(m_splitPoint);
392
return expr.toString();
397
* @param instance the instance to produce the weights for
398
* @return a double array of weights, null if only belongs to one subset
400
public double [] weights(Instance instance) {
405
if(instance.isMissing(m_attIndex)) {
406
weights = new double [m_numSubsets];
407
for(i=0;i<m_numSubsets;i++) {
408
weights [i] = m_graftdistro.perBag(i)/m_graftdistro.total();
418
* @param instance the instance for which to determine the subset
419
* @return an int indicating the subset this instance belongs to
421
public int whichSubset(Instance instance) {
423
if(instance.isMissing(m_attIndex))
426
if(instance.attribute(m_attIndex).isNominal()) {
427
// in the case of nominal, m_splitPoint is the = value, all else is !=
428
if(instance.value(m_attIndex) == m_splitPoint)
433
if(Utils.smOrEq(instance.value(m_attIndex), m_splitPoint))
442
* @return the value of the split point
444
public double splitPoint() {
449
* @return the dominate class for the subset of interest
451
public int maxClassForSubsetOfInterest() {
456
* @return the laplace value for maxClass of subset of interest
458
public double laplaceForSubsetOfInterest() {
463
* returns the test type
464
* @return value of testtype
466
public int testType() {
471
* method needed for sorting a collection of GraftSplits by laplace value
472
* @param g the graft split to compare to this one
473
* @return -1, 0, or 1 if this GraftSplit laplace is <, = or > than that of g
475
public int compareTo(Object g) {
477
if(m_laplace > ((GraftSplit)g).laplaceForSubsetOfInterest())
479
if(m_laplace < ((GraftSplit)g).laplaceForSubsetOfInterest())
485
* returns the probability for instance for the specified class
486
* @param classIndex the index of the class
487
* @param instance the instance to get the probability for
488
* @param theSubset the subset
490
public final double classProb(int classIndex, Instance instance,
491
int theSubset) throws Exception {
493
if (theSubset <= -1) {
494
double [] weights = weights(instance);
495
if (weights == null) {
496
return m_distribution.prob(classIndex);
499
for (int i = 0; i < weights.length; i++) {
500
prob += weights[i] * m_distribution.prob(classIndex, i);
505
if (Utils.gr(m_distribution.perBag(theSubset), 0)) {
506
return m_distribution.prob(classIndex, theSubset);
508
return m_distribution.prob(classIndex);
515
* method for returning information about this GraftSplit
516
* @param data instances for determining names of attributes and values
517
* @return a string showing this GraftSplit's information
519
public String toString(Instances data) {
524
else if(m_testType == 1)
526
else if(m_testType == 2)
531
if(data.attribute(m_attIndex).isNominal())
532
theTest += data.attribute(m_attIndex).value((int)m_splitPoint);
534
theTest += Double.toString(m_splitPoint);
536
return data.attribute(m_attIndex).name() + theTest
537
+ " (" + Double.toString(m_laplace) + ") --> "
538
+ data.attribute(data.classIndex()).value(m_maxClass);