| 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 | * Impurity.java |
|---|
| 19 | * Copyright (C) 1999 University of Waikato, Hamilton, New Zealand |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | package weka.classifiers.trees.m5; |
|---|
| 24 | |
|---|
| 25 | import weka.core.Instances; |
|---|
| 26 | import weka.core.RevisionHandler; |
|---|
| 27 | import weka.core.RevisionUtils; |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | * Class for handling the impurity values when spliting the instances |
|---|
| 31 | * @author Yong Wang (yongwang@cs.waikato.ac.nz) |
|---|
| 32 | * @version $Revision: 1.8 $ |
|---|
| 33 | */ |
|---|
| 34 | public final class Impurity |
|---|
| 35 | implements RevisionHandler { |
|---|
| 36 | |
|---|
| 37 | double n; // number of total instances |
|---|
| 38 | int attr; // splitting attribute |
|---|
| 39 | double nl; // number of instances in the left group |
|---|
| 40 | double nr; // number of instances in the right group |
|---|
| 41 | double sl; // sum of the left group |
|---|
| 42 | double sr; // sum of the right group |
|---|
| 43 | double s2l; // squared sum of the left group |
|---|
| 44 | double s2r; // squared sum of the right group |
|---|
| 45 | double sdl; // standard deviation of the left group |
|---|
| 46 | double sdr; // standard deviation of the right group |
|---|
| 47 | double vl; // variance of the left group |
|---|
| 48 | double vr; // variance of the right group |
|---|
| 49 | double sd; // overall standard deviation |
|---|
| 50 | double va; // overall variance |
|---|
| 51 | |
|---|
| 52 | double impurity; // impurity value; |
|---|
| 53 | int order; // order = 1, variance; order = 2, standard deviation; order = 3, the cubic root of the variance; |
|---|
| 54 | // order = k, the k-th order root of the variance |
|---|
| 55 | |
|---|
| 56 | /** |
|---|
| 57 | * Constructs an Impurity object containing the impurity values of partitioning the instances using an attribute |
|---|
| 58 | * @param partition the index of the last instance in the left subset |
|---|
| 59 | * @param attribute the attribute used in partitioning |
|---|
| 60 | * @param inst instances |
|---|
| 61 | * @param k the order of the impurity; =1, the variance; =2, the stardard deviation; =k, the k-th order root of the variance |
|---|
| 62 | */ |
|---|
| 63 | public Impurity(int partition,int attribute,Instances inst,int k){ |
|---|
| 64 | |
|---|
| 65 | Values values = new Values(0,inst.numInstances()-1,inst.classIndex(),inst); |
|---|
| 66 | attr = attribute; |
|---|
| 67 | n = inst.numInstances(); |
|---|
| 68 | sd = values.sd; |
|---|
| 69 | va = values.va; |
|---|
| 70 | |
|---|
| 71 | values = new Values(0,partition,inst.classIndex(),inst); |
|---|
| 72 | nl = partition + 1; |
|---|
| 73 | sl = values.sum; |
|---|
| 74 | s2l = values.sqrSum; |
|---|
| 75 | |
|---|
| 76 | values = new Values(partition+1,inst.numInstances()-1,inst.classIndex(),inst); |
|---|
| 77 | nr = inst.numInstances() - partition -1; |
|---|
| 78 | sr = values.sum; |
|---|
| 79 | s2r = values.sqrSum; |
|---|
| 80 | |
|---|
| 81 | order = k; |
|---|
| 82 | this.incremental(0,0); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | /** |
|---|
| 86 | * Converts an Impurity object to a string |
|---|
| 87 | * @return the converted string |
|---|
| 88 | */ |
|---|
| 89 | public final String toString() { |
|---|
| 90 | |
|---|
| 91 | StringBuffer text = new StringBuffer(); |
|---|
| 92 | |
|---|
| 93 | text.append("Print impurity values:\n"); |
|---|
| 94 | text.append(" Number of total instances:\t" + n + "\n"); |
|---|
| 95 | text.append(" Splitting attribute:\t\t" + attr + "\n"); |
|---|
| 96 | text.append(" Number of the instances in the left:\t" + nl + "\n"); |
|---|
| 97 | text.append(" Number of the instances in the right:\t" + nr + "\n"); |
|---|
| 98 | text.append(" Sum of the left:\t\t\t" + sl + "\n"); |
|---|
| 99 | text.append(" Sum of the right:\t\t\t" + sr + "\n"); |
|---|
| 100 | text.append(" Squared sum of the left:\t\t" + s2l + "\n"); |
|---|
| 101 | text.append(" Squared sum of the right:\t\t" + s2r + "\n"); |
|---|
| 102 | text.append(" Standard deviation of the left:\t" + sdl + "\n"); |
|---|
| 103 | text.append(" Standard deviation of the right:\t" + sdr + "\n"); |
|---|
| 104 | text.append(" Variance of the left:\t\t" + vr + "\n"); |
|---|
| 105 | text.append(" Variance of the right:\t\t" + vr + "\n"); |
|---|
| 106 | text.append(" Overall standard deviation:\t\t" + sd + "\n"); |
|---|
| 107 | text.append(" Overall variance:\t\t\t" + va + "\n"); |
|---|
| 108 | text.append(" Impurity (order " + order + "):\t\t" + impurity + "\n"); |
|---|
| 109 | |
|---|
| 110 | return text.toString(); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | /** |
|---|
| 114 | * Incrementally computes the impurirty values |
|---|
| 115 | * @param value the incremental value |
|---|
| 116 | * @param type if type=1, value will be added to the left subset; type=-1, to the right subset; type=0, initializes |
|---|
| 117 | */ |
|---|
| 118 | public final void incremental(double value,int type){ |
|---|
| 119 | double y=0.,yl=0.,yr=0.; |
|---|
| 120 | |
|---|
| 121 | switch(type){ |
|---|
| 122 | case 1: |
|---|
| 123 | nl += 1; |
|---|
| 124 | nr -= 1; |
|---|
| 125 | sl += value; |
|---|
| 126 | sr -= value; |
|---|
| 127 | s2l += value*value; |
|---|
| 128 | s2r -= value*value; |
|---|
| 129 | break; |
|---|
| 130 | case -1: |
|---|
| 131 | nl -= 1; |
|---|
| 132 | nr += 1; |
|---|
| 133 | sl -= value; |
|---|
| 134 | sr += value; |
|---|
| 135 | s2l -= value*value; |
|---|
| 136 | s2r += value*value; |
|---|
| 137 | break; |
|---|
| 138 | case 0: |
|---|
| 139 | break; |
|---|
| 140 | default: System.err.println("wrong type in Impurity.incremental()."); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | if(nl<=0.0){ |
|---|
| 144 | vl=0.0; |
|---|
| 145 | sdl=0.0; |
|---|
| 146 | } |
|---|
| 147 | else { |
|---|
| 148 | vl = (nl*s2l-sl*sl)/((double)nl*((double)nl)); |
|---|
| 149 | vl = Math.abs(vl); |
|---|
| 150 | sdl = Math.sqrt(vl); |
|---|
| 151 | } |
|---|
| 152 | if(nr<=0.0){ |
|---|
| 153 | vr=0.0; |
|---|
| 154 | sdr=0.0; |
|---|
| 155 | } |
|---|
| 156 | else { |
|---|
| 157 | vr = (nr*s2r-sr*sr)/((double)nr*((double)nr)); |
|---|
| 158 | vr = Math.abs(vr); |
|---|
| 159 | sdr = Math.sqrt(vr); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | if(order <= 0)System.err.println("Impurity order less than zero in Impurity.incremental()"); |
|---|
| 163 | else if(order == 1) { |
|---|
| 164 | y = va; yl = vl; yr = vr; |
|---|
| 165 | } else { |
|---|
| 166 | y = Math.pow(va,1./order); |
|---|
| 167 | yl = Math.pow(vl,1./order); |
|---|
| 168 | yr = Math.pow(vr,1./order); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | if(nl<=0.0 || nr<=0.0) |
|---|
| 172 | impurity = 0.0; |
|---|
| 173 | else { |
|---|
| 174 | impurity = y - ((double)nl/(double)n)*yl - ((double)nr/(double)n)*yr; |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | /** |
|---|
| 179 | * Returns the revision string. |
|---|
| 180 | * |
|---|
| 181 | * @return the revision |
|---|
| 182 | */ |
|---|
| 183 | public String getRevision() { |
|---|
| 184 | return RevisionUtils.extract("$Revision: 1.8 $"); |
|---|
| 185 | } |
|---|
| 186 | } |
|---|