annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@tree_CPD/learn_params.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function CPD = learn_params(CPD, fam, data, ns, cnodes, varargin)
Daniel@0 2 % LEARN_PARAMS Construct classification/regression tree given complete data
Daniel@0 3 % CPD = learn_params(CPD, fam, data, ns, cnodes)
Daniel@0 4 %
Daniel@0 5 % fam(i) is the node id of the i-th node in the family of nodes, self node is the last one
Daniel@0 6 % data(i,m) is the value of node i in case m (can be cell array).
Daniel@0 7 % ns(i) is the node size for the i-th node in the whold bnet
Daniel@0 8 % cnodes(i) is the node id for the i-th continuous node in the whole bnet
Daniel@0 9 %
Daniel@0 10 % The following optional arguments can be specified in the form of name/value pairs:
Daniel@0 11 % stop_cases: for early stop (pruning). A node is not split if it has less than k cases. default is 0.
Daniel@0 12 % min_gain: for early stop (pruning).
Daniel@0 13 % For discrete output: A node is not split when the gain of best split is less than min_gain. default is 0.
Daniel@0 14 % For continuous (cts) outpt: A node is not split when the gain of best split is less than min_gain*score(root)
Daniel@0 15 % (we denote it cts_min_gain). default is 0.006
Daniel@0 16 % %%%%%%%%%%%%%%%%%%%Struction definition of dtree_CPD.tree%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 17 % tree.num_node the last position in tree.nodes array for adding new nodes,
Daniel@0 18 % it is not always same to number of nodes in a tree, because some position in the
Daniel@0 19 % tree.nodes array can be set to unused (e.g. in tree pruning)
Daniel@0 20 % tree.nodes is the array of nodes in the tree plus some unused nodes.
Daniel@0 21 % tree.nodes(1) is the root for the tree.
Daniel@0 22 %
Daniel@0 23 % Below is the attributes for each node
Daniel@0 24 % tree.nodes(i).used; % flag this node is used (0 means node not used, it can be removed from tree to save memory)
Daniel@0 25 % tree.nodes(i).is_leaf; % if 1 means this node is a leaf, if 0 not a leaf.
Daniel@0 26 % tree.nodes(i).children; % children(i) is the node number in tree.nodes array for the i-th child node
Daniel@0 27 % tree.nodes(i).split_id; % the attribute id used to split this node
Daniel@0 28 % tree.nodes(i).split_threshhold; % the threshhold for continuous attribute to split this node
Daniel@0 29 % %%%%%attributes specially for classification tree (discrete output)
Daniel@0 30 % tree.nodes(i).probs % probs(i) is the prob for i-th value of class node
Daniel@0 31 % % For three output class, the probs = [0.9 0.1 0.0] means the probability of
Daniel@0 32 % % class 1 is 0.9, for class 2 is 0.1, for class 3 is 0.0.
Daniel@0 33 % %%%%%attributes specially for regression tree (continuous output)
Daniel@0 34 % tree.nodes(i).mean % mean output value for this node
Daniel@0 35 % tree.nodes(i).std % standard deviation for output values in this node
Daniel@0 36 %
Daniel@0 37 % Author: yimin.zhang@intel.com
Daniel@0 38 % Last updated: Jan. 19, 2002
Daniel@0 39
Daniel@0 40 % Want list:
Daniel@0 41 % (1) more efficient for cts attributes: get the values of cts attributes at first (the begining of build_tree function), then doing bi_search in finding threshhold
Daniel@0 42 % (2) pruning classification tree using Pessimistic Error Pruning
Daniel@0 43 % (3) bi_search for strings (used for transform data to BNT format)
Daniel@0 44
Daniel@0 45 global tree %tree must be global so that it can be accessed in recursive slitting function
Daniel@0 46 global cts_min_gain
Daniel@0 47 tree=[]; % clear the tree
Daniel@0 48 tree.num_node=0;
Daniel@0 49 cts_min_gain=0;
Daniel@0 50
Daniel@0 51 stop_cases=0;
Daniel@0 52 min_gain=0;
Daniel@0 53
Daniel@0 54 args = varargin;
Daniel@0 55 nargs = length(args);
Daniel@0 56 if (nargs>0)
Daniel@0 57 if isstr(args{1})
Daniel@0 58 for i=1:2:nargs
Daniel@0 59 switch args{i},
Daniel@0 60 case 'stop_cases', stop_cases = args{i+1};
Daniel@0 61 case 'min_gain', min_gain = args{i+1};
Daniel@0 62 end
Daniel@0 63 end
Daniel@0 64 else
Daniel@0 65 error(['error in input parameters']);
Daniel@0 66 end
Daniel@0 67 end
Daniel@0 68
Daniel@0 69 if iscell(data)
Daniel@0 70 local_data = cell2num(data(fam,:));
Daniel@0 71 else
Daniel@0 72 local_data = data(fam, :);
Daniel@0 73 end
Daniel@0 74 %counts = compute_counts(local_data, CPD.sizes);
Daniel@0 75 %CPD.CPT = mk_stochastic(counts + CPD.prior); % bug fix 11/5/01
Daniel@0 76 node_types = zeros(1,size(ns,2)); %all nodes are disrete
Daniel@0 77 node_types(cnodes)=1;
Daniel@0 78 %make the data be BNT compliant (values for discrete nodes are from 1-n, here n is the node size)
Daniel@0 79 %trans_data=transform_data(local_data,'tmp.dat',[]); %here no cts nodes
Daniel@0 80
Daniel@0 81 build_dtree (CPD, local_data, ns(fam), node_types(fam),stop_cases,min_gain);
Daniel@0 82 %CPD.tree=copy_tree(tree);
Daniel@0 83 CPD.tree=tree; %copy the tree constructed to CPD
Daniel@0 84
Daniel@0 85
Daniel@0 86 function new_tree = copy_tree(tree)
Daniel@0 87 % copy the tree to new_tree
Daniel@0 88 new_tree.num_node=tree.num_node;
Daniel@0 89 new_tree.root = tree.root;
Daniel@0 90 for i=1:tree.num_node
Daniel@0 91 new_tree.nodes(i)=tree.nodes(i);
Daniel@0 92 end
Daniel@0 93
Daniel@0 94
Daniel@0 95 function build_dtree (CPD, fam_ev, node_sizes, node_types,stop_cases,min_gain)
Daniel@0 96 global tree
Daniel@0 97 global cts_min_gain
Daniel@0 98
Daniel@0 99 tree.num_node=0; %the current number of nodes in the tree
Daniel@0 100 tree.root=1;
Daniel@0 101
Daniel@0 102 T = 1:size(fam_ev,2) ; %all cases
Daniel@0 103 candidate_attrs = 1:(size(node_sizes,2)-1); %all attributes
Daniel@0 104 node_id=1; %the root node
Daniel@0 105 lastnode=size(node_sizes,2); %the last element in all nodes is the dependent variable (category node)
Daniel@0 106 num_cat=node_sizes(lastnode);
Daniel@0 107
Daniel@0 108 % get minimum gain for cts output (used in stop splitting)
Daniel@0 109 if (node_types(size(fam_ev,1))==1) %cts output
Daniel@0 110 N = size(fam_ev,2);
Daniel@0 111 output_id = size(fam_ev,1);
Daniel@0 112 cases_T = fam_ev(output_id,:); %get all the output value for cases T
Daniel@0 113 std_T = std(cases_T);
Daniel@0 114 avg_y_T = mean(cases_T);
Daniel@0 115 sqr_T = cases_T - avg_y_T;
Daniel@0 116 cts_min_gain = min_gain*(sum(sqr_T.*sqr_T)/N); % min_gain * (R(root) = 1/N * SUM(y-avg_y)^2)
Daniel@0 117 end
Daniel@0 118
Daniel@0 119 split_dtree (CPD, fam_ev, node_sizes, node_types, stop_cases,min_gain, T, candidate_attrs, num_cat);
Daniel@0 120
Daniel@0 121
Daniel@0 122
Daniel@0 123 % pruning method
Daniel@0 124 % (1) Restrictions on minimum node size: A node is not split if it has smaller than k cases.
Daniel@0 125 % (2) Threshholds on impurity: a threshhold is imposed on the splitting test score. Threshhold can be
Daniel@0 126 % imposed on local goodness measure (the gain_ratio of a node) or global goodness.
Daniel@0 127 % (3) Mininum Error Pruning (MEP), (no need pruning set)
Daniel@0 128 % Prune if static error<=backed-up error
Daniel@0 129 % Static error at node v: e(v) = (Nc + 1)/(N+k) (laplace estimate, prior for each class equal)
Daniel@0 130 % here N is # of all examples, Nc is # of majority class examples, k is number of classes
Daniel@0 131 % Backed-up error at node v: (Ti is the i-th subtree root)
Daniel@0 132 % E(T) = Sum_1_to_n(pi*e(Ti))
Daniel@0 133 % (4) Pessimistic Error Pruning (PEP), used in Quilan C4.5 (no need pruning set, efficient because of pruning top-down)
Daniel@0 134 % Probability of error (apparent error rate)
Daniel@0 135 % q = (N-Nc+0.5)/N
Daniel@0 136 % where N=#examples, Nc=#examples in majority class
Daniel@0 137 % Error of a node v (if pruned) q(v)= (Nv- Nc,v + 0.5)/Nv
Daniel@0 138 % Error of a subtree q(T)= Sum_of_l_leaves(Nl - Nc,l + 0.5)/Sum_of_l_leaves(Nl)
Daniel@0 139 % Prune if q(v)<=q(T)
Daniel@0 140 %
Daniel@0 141 % Implementation statuts:
Daniel@0 142 % (1)(2) has been implemented as the input parameters of learn_params.
Daniel@0 143 % (4) is implemented in this function
Daniel@0 144 function pruning(fam_ev,node_sizes,node_types)
Daniel@0 145 % PRUNING prune the constructed tree using PEP
Daniel@0 146 % pruning(fam_ev,node_sizes,node_types)
Daniel@0 147 %
Daniel@0 148 % fam_ev(i,j) is the value of attribute i in j-th training cases (for whole tree), the last row is for the class label (self_ev)
Daniel@0 149 % node_sizes(i) is the node size for the i-th node in the family
Daniel@0 150 % node_types(i) is the node type for the i-th node in the family, 0 for disrete node, 1 for continous node
Daniel@0 151 % the global parameter 'tree' is for storing the input tree and the pruned tree
Daniel@0 152
Daniel@0 153
Daniel@0 154 function split_T = split_cases(fam_ev,node_sizes,node_types,T,node_i, threshhold)
Daniel@0 155 % SPLIT_CASES split the cases T according to values of node_i in the family
Daniel@0 156 % split_T = split_cases(fam_ev,node_sizes,node_types,T,node_i)
Daniel@0 157 %
Daniel@0 158 % fam_ev(i,j) is the value of attribute i in j-th training cases (for whole tree), the last row is for the class label (self_ev)
Daniel@0 159 % node_sizes(i) is the node size for the i-th node in the family
Daniel@0 160 % node_types(i) is the node type for the i-th node in the family, 0 for disrete node, 1 for continous node
Daniel@0 161 % node_i is the attribute we need to split
Daniel@0 162
Daniel@0 163 if (node_types(node_i)==0) %discrete attribute
Daniel@0 164 %init the subsets of T
Daniel@0 165 split_T = cell(1,node_sizes(node_i)); %T will be separated into |node_size of i| subsets according to different values of node i
Daniel@0 166 for i=1:node_sizes(node_i) % here we assume that the value of an attribute is 1:node_size
Daniel@0 167 split_T{i}=zeros(1,0);
Daniel@0 168 end
Daniel@0 169
Daniel@0 170 size_t = size(T,2);
Daniel@0 171 for i=1:size_t
Daniel@0 172 case_id = T(i);
Daniel@0 173 %put this case into one subset of split_T according to its value for node_i
Daniel@0 174 value = fam_ev(node_i,case_id);
Daniel@0 175 pos = size(split_T{value},2)+1;
Daniel@0 176 split_T{value}(pos)=case_id; % here assumes the value of an attribute is 1:node_size
Daniel@0 177 end
Daniel@0 178 else %continuous attribute
Daniel@0 179 %init the subsets of T
Daniel@0 180 split_T = cell(1,2); %T will be separated into 2 subsets (<=threshhold) (>threshhold)
Daniel@0 181 for i=1:2
Daniel@0 182 split_T{i}=zeros(1,0);
Daniel@0 183 end
Daniel@0 184
Daniel@0 185 size_t = size(T,2);
Daniel@0 186 for i=1:size_t
Daniel@0 187 case_id = T(i);
Daniel@0 188 %put this case into one subset of split_T according to its value for node_i
Daniel@0 189 value = fam_ev(node_i,case_id);
Daniel@0 190 subset_num=1;
Daniel@0 191 if (value>threshhold)
Daniel@0 192 subset_num=2;
Daniel@0 193 end
Daniel@0 194 pos = size(split_T{subset_num},2)+1;
Daniel@0 195 split_T{subset_num}(pos)=case_id;
Daniel@0 196 end
Daniel@0 197 end
Daniel@0 198
Daniel@0 199
Daniel@0 200
Daniel@0 201 function new_node = split_dtree (CPD, fam_ev, node_sizes, node_types, stop_cases, min_gain, T, candidate_attrs, num_cat)
Daniel@0 202 % SPLIT_TREE Split the tree at node node_id with cases T (actually it is just indexes to family evidences).
Daniel@0 203 % new_node = split_dtree (fam_ev, node_sizes, node_types, T, node_id, num_cat, method)
Daniel@0 204 %
Daniel@0 205 % fam_ev(i,j) is the value of attribute i in j-th training cases (for whole tree), the last row is for the class label (self_ev)
Daniel@0 206 % node_sizes{i} is the node size for the i-th node in the family
Daniel@0 207 % node_types{i} is the node type for the i-th node in the family, 0 for disrete node, 1 for continous node
Daniel@0 208 % stop_cases is the threshold of number of cases to stop slitting
Daniel@0 209 % min_gain is the minimum gain need to split a node
Daniel@0 210 % T(i) is the index of i-th cases in current decision tree node, we need split it further
Daniel@0 211 % candidate_attrs(i) the node id for the i-th attribute that still need to be considered as split attribute
Daniel@0 212 %%%%% node_id is the index of current node considered for a split
Daniel@0 213 % num_cat is the number of output categories for the decision tree
Daniel@0 214 % output:
Daniel@0 215 % new_node is the new node created
Daniel@0 216 global tree
Daniel@0 217 global cts_min_gain
Daniel@0 218
Daniel@0 219 size_fam = size(fam_ev,1); %number of family size
Daniel@0 220 output_type = node_types(size_fam); %the type of output for the tree (0 is discrete, 1 is continuous)
Daniel@0 221 size_attrs = size(candidate_attrs,2); %number of candidate attributes
Daniel@0 222 size_t = size(T,2); %number of training cases in this tree node
Daniel@0 223
Daniel@0 224 %(1)computeFrequenceyForEachClass(T)
Daniel@0 225 if (output_type==0) %discrete output
Daniel@0 226 class_freqs = zeros(1,num_cat);
Daniel@0 227 for i=1:size_t
Daniel@0 228 case_id = T(i);
Daniel@0 229 case_class = fam_ev(size_fam,case_id); %get the class label for this case
Daniel@0 230 class_freqs(case_class)=class_freqs(case_class)+1;
Daniel@0 231 end
Daniel@0 232 else %cts output
Daniel@0 233 N = size(fam_ev,2);
Daniel@0 234 cases_T = fam_ev(size(fam_ev,1),T); %get the output value for cases T
Daniel@0 235 std_T = std(cases_T);
Daniel@0 236 end
Daniel@0 237
Daniel@0 238 %(2) if OneClass (for discrete output) or same output value (for cts output) or Class With #examples < stop_cases
Daniel@0 239 % return a leaf;
Daniel@0 240 % create a decision node N;
Daniel@0 241
Daniel@0 242 % get majority class in this node
Daniel@0 243 if (output_type == 0)
Daniel@0 244 top1_class = 0; %the class with the largest number of cases
Daniel@0 245 top1_class_cases = 0; %the number of cases in top1_class
Daniel@0 246 [top1_class_cases,top1_class]=max(class_freqs);
Daniel@0 247 end
Daniel@0 248
Daniel@0 249 if (size_t==0) %impossble
Daniel@0 250 new_node=-1;
Daniel@0 251 fprintf('Fatal error: please contact the author. \n');
Daniel@0 252 return;
Daniel@0 253 end
Daniel@0 254
Daniel@0 255 % stop splitting if needed
Daniel@0 256 %for discrete output: one class
Daniel@0 257 %for cts output, all output value in cases are same
Daniel@0 258 %cases too little
Daniel@0 259 if ( (output_type==0 & top1_class_cases == size_t) | (output_type==1 & std_T == 0) | (size_t < stop_cases))
Daniel@0 260 %create one new leaf node
Daniel@0 261 tree.num_node=tree.num_node+1;
Daniel@0 262 tree.nodes(tree.num_node).used=1; %flag this node is used (0 means node not used, it will be removed from tree at last to save memory)
Daniel@0 263 tree.nodes(tree.num_node).is_leaf=1;
Daniel@0 264 tree.nodes(tree.num_node).children=[];
Daniel@0 265 tree.nodes(tree.num_node).split_id=0; %the attribute(parent) id to split this tree node
Daniel@0 266 tree.nodes(tree.num_node).split_threshhold=0;
Daniel@0 267 if (output_type==0)
Daniel@0 268 tree.nodes(tree.num_node).probs=class_freqs/size_t; %the prob for each value of class node
Daniel@0 269
Daniel@0 270 % tree.nodes(tree.num_node).probs=zeros(1,num_cat); %the prob for each value of class node
Daniel@0 271 % tree.nodes(tree.num_node).probs(top1_class)=1; %use the majority class of parent node, like for binary class,
Daniel@0 272 %and majority is class 2, then the CPT is [0 1]
Daniel@0 273 %we may need to use prior to do smoothing, to get [0.001 0.999]
Daniel@0 274 tree.nodes(tree.num_node).error.self_error=1-top1_class_cases/size_t; %the classfication error in this tree node when use default class
Daniel@0 275 tree.nodes(tree.num_node).error.all_error=1-top1_class_cases/size_t; %no total classfication error in this tree node and its subtree
Daniel@0 276 tree.nodes(tree.num_node).error.all_error_num=size_t - top1_class_cases;
Daniel@0 277 fprintf('Create leaf node(onecla) %d. Class %d Cases %d Error %d \n',tree.num_node, top1_class, size_t, size_t - top1_class_cases );
Daniel@0 278 else
Daniel@0 279 avg_y_T = mean(cases_T);
Daniel@0 280 tree.nodes(tree.num_node).mean = avg_y_T;
Daniel@0 281 tree.nodes(tree.num_node).std = std_T;
Daniel@0 282 fprintf('Create leaf node(samevalue) %d. Mean %8.4f Std %8.4f Cases %d \n',tree.num_node, avg_y_T, std_T, size_t);
Daniel@0 283 end
Daniel@0 284 new_node = tree.num_node;
Daniel@0 285 return;
Daniel@0 286 end
Daniel@0 287
Daniel@0 288 %create one new node
Daniel@0 289 tree.num_node=tree.num_node+1;
Daniel@0 290 tree.nodes(tree.num_node).used=1; %flag this node is used (0 means node not used, it will be removed from tree at last to save memory)
Daniel@0 291 tree.nodes(tree.num_node).is_leaf=1;
Daniel@0 292 tree.nodes(tree.num_node).children=[];
Daniel@0 293 tree.nodes(tree.num_node).split_id=0;
Daniel@0 294 tree.nodes(tree.num_node).split_threshhold=0;
Daniel@0 295 if (output_type==0)
Daniel@0 296 tree.nodes(tree.num_node).error.self_error=1-top1_class_cases/size_t;
Daniel@0 297 tree.nodes(tree.num_node).error.all_error=0;
Daniel@0 298 tree.nodes(tree.num_node).error.all_error_num=0;
Daniel@0 299 else
Daniel@0 300 avg_y_T = mean(cases_T);
Daniel@0 301 tree.nodes(tree.num_node).mean = avg_y_T;
Daniel@0 302 tree.nodes(tree.num_node).std = std_T;
Daniel@0 303 end
Daniel@0 304 new_node = tree.num_node;
Daniel@0 305
Daniel@0 306 %Stop splitting if no attributes left in this node
Daniel@0 307 if (size_attrs==0)
Daniel@0 308 if (output_type==0)
Daniel@0 309 tree.nodes(tree.num_node).probs=class_freqs/size_t; %the prob for each value of class node
Daniel@0 310 tree.nodes(tree.num_node).error.all_error=1-top1_class_cases/size_t;
Daniel@0 311 tree.nodes(tree.num_node).error.all_error_num=size_t - top1_class_cases;
Daniel@0 312 fprintf('Create leaf node(noattr) %d. Class %d Cases %d Error %d \n',tree.num_node, top1_class, size_t, size_t - top1_class_cases );
Daniel@0 313 else
Daniel@0 314 fprintf('Create leaf node(noattr) %d. Mean %8.4f Std %8.4f Cases %d \n',tree.num_node, avg_y_T, std_T, size_t);
Daniel@0 315 end
Daniel@0 316 return;
Daniel@0 317 end
Daniel@0 318
Daniel@0 319
Daniel@0 320 %(3) for each attribute A
Daniel@0 321 % ComputeGain(A);
Daniel@0 322 max_gain=0; %the max gain score (for discrete information gain or gain ration, for cts node the R(T))
Daniel@0 323 best_attr=0; %the attribute with the max_gain
Daniel@0 324 best_split = []; %the split of T according to the value of best_attr
Daniel@0 325 cur_best_threshhold = 0; %the threshhold for split continuous attribute
Daniel@0 326 best_threshhold=0;
Daniel@0 327
Daniel@0 328 % compute Info(T) (for discrete output)
Daniel@0 329 if (output_type == 0)
Daniel@0 330 class_split_T = split_cases(fam_ev,node_sizes,node_types,T,size(fam_ev,1),0); %split cases according to class
Daniel@0 331 info_T = compute_info (fam_ev, T, class_split_T);
Daniel@0 332 else % compute R(T) (for cts output)
Daniel@0 333 % N = size(fam_ev,2);
Daniel@0 334 % cases_T = fam_ev(size(fam_ev,1),T); %get the output value for cases T
Daniel@0 335 % std_T = std(cases_T);
Daniel@0 336 % avg_y_T = mean(cases_T);
Daniel@0 337 sqr_T = cases_T - avg_y_T;
Daniel@0 338 R_T = sum(sqr_T.*sqr_T)/N; % get R(T) = 1/N * SUM(y-avg_y)^2
Daniel@0 339 info_T = R_T;
Daniel@0 340 end
Daniel@0 341
Daniel@0 342 for i=1:(size_fam-1)
Daniel@0 343 if (myismember(i,candidate_attrs)) %if this attribute still in the candidate attribute set
Daniel@0 344 if (node_types(i)==0) %discrete attibute
Daniel@0 345 split_T = split_cases(fam_ev,node_sizes,node_types,T,i,0); %split cases according to value of attribute i
Daniel@0 346 % For cts output, we compute the least square gain.
Daniel@0 347 % For discrete output, we compute gain ratio
Daniel@0 348 cur_gain = compute_gain(fam_ev,node_sizes,node_types,T,info_T,i,split_T,0,output_type); %gain ratio
Daniel@0 349 else %cts attribute
Daniel@0 350 %get the values of this attribute
Daniel@0 351 ev = fam_ev(:,T);
Daniel@0 352 values = ev(i,:);
Daniel@0 353 sort_v = sort(values);
Daniel@0 354 %remove the duplicate values in sort_v
Daniel@0 355 v_set = unique(sort_v);
Daniel@0 356 best_gain = 0;
Daniel@0 357 best_threshhold = 0;
Daniel@0 358 best_split1 = [];
Daniel@0 359
Daniel@0 360 %find the best split for this cts attribute
Daniel@0 361 % see "Quilan 96: Improved Use of Continuous Attributes in C4.5"
Daniel@0 362 for j=1:(size(v_set,2)-1)
Daniel@0 363 mid_v = (v_set(j)+v_set(j+1))/2;
Daniel@0 364 split_T = split_cases(fam_ev,node_sizes,node_types,T,i,mid_v); %split cases according to value of attribute i (<=mid_v)
Daniel@0 365 % For cts output, we compute the least square gain.
Daniel@0 366 % For discrete output, we use Quilan 96: use information gain instead of gain ratio to select threshhold
Daniel@0 367 cur_gain = compute_gain(fam_ev,node_sizes,node_types,T,info_T,i,split_T,1,output_type);
Daniel@0 368 %if (i==6)
Daniel@0 369 % fprintf('gain %8.5f threshhold %6.3f spliting %d\n', cur_gain, mid_v, size(split_T{1},2));
Daniel@0 370 %end
Daniel@0 371
Daniel@0 372 if (best_gain < cur_gain)
Daniel@0 373 best_gain = cur_gain;
Daniel@0 374 best_threshhold = mid_v;
Daniel@0 375 %best_split1 = split_T; %here we need to copy array, not good!!! (maybe we can compute after we get best_attr
Daniel@0 376 end
Daniel@0 377 end
Daniel@0 378 %recalculate the gain_ratio of the best_threshhold
Daniel@0 379 split_T = split_cases(fam_ev,node_sizes,node_types,T,i,best_threshhold);
Daniel@0 380 best_gain = compute_gain(fam_ev,node_sizes,node_types,T,info_T,i,split_T,0,output_type); %gain_ratio
Daniel@0 381 if (output_type==0) %for discrete output
Daniel@0 382 cur_gain = best_gain-log2(size(v_set,2)-1)/size_t; % Quilan 96: use the gain_ratio-log2(N-1)/|D| as the gain of this attr
Daniel@0 383 else %for cts output
Daniel@0 384 cur_gain = best_gain;
Daniel@0 385 end
Daniel@0 386 end
Daniel@0 387
Daniel@0 388 if (max_gain < cur_gain)
Daniel@0 389 max_gain = cur_gain;
Daniel@0 390 best_attr = i;
Daniel@0 391 cur_best_threshhold=best_threshhold; %save the threshhold
Daniel@0 392 %best_split = split_T; %here we need to copy array, not good!!! So we will recalculate in below line 313
Daniel@0 393 end
Daniel@0 394 end
Daniel@0 395 end
Daniel@0 396
Daniel@0 397 % stop splitting if gain is too small
Daniel@0 398 if (max_gain==0 | (output_type==0 & max_gain < min_gain) | (output_type==1 & max_gain < cts_min_gain))
Daniel@0 399 if (output_type==0)
Daniel@0 400 tree.nodes(tree.num_node).probs=class_freqs/size_t; %the prob for each value of class node
Daniel@0 401 tree.nodes(tree.num_node).error.all_error=1-top1_class_cases/size_t;
Daniel@0 402 tree.nodes(tree.num_node).error.all_error_num=size_t - top1_class_cases;
Daniel@0 403 fprintf('Create leaf node(nogain) %d. Class %d Cases %d Error %d \n',tree.num_node, top1_class, size_t, size_t - top1_class_cases );
Daniel@0 404 else
Daniel@0 405 fprintf('Create leaf node(nogain) %d. Mean %8.4f Std %8.4f Cases %d \n',tree.num_node, avg_y_T, std_T, size_t);
Daniel@0 406 end
Daniel@0 407 return;
Daniel@0 408 end
Daniel@0 409
Daniel@0 410 %get the split of cases according to the best split attribute
Daniel@0 411 if (node_types(best_attr)==0) %discrete attibute
Daniel@0 412 best_split = split_cases(fam_ev,node_sizes,node_types,T,best_attr,0);
Daniel@0 413 else
Daniel@0 414 best_split = split_cases(fam_ev,node_sizes,node_types,T,best_attr,cur_best_threshhold);
Daniel@0 415 end
Daniel@0 416
Daniel@0 417 %(4) best_attr = AttributeWithBestGain;
Daniel@0 418 %(5) if best_attr is continuous ???? why need this? maybe the value in the decision tree must appeared in data
Daniel@0 419 % find threshhold in all cases that <= max_V
Daniel@0 420 % change the split of T
Daniel@0 421 tree.nodes(tree.num_node).split_id=best_attr;
Daniel@0 422 tree.nodes(tree.num_node).split_threshhold=cur_best_threshhold; %for cts attribute only
Daniel@0 423
Daniel@0 424 %note: below threshhold rejust is linera search, so it is slow. A better method is described in paper "Efficient C4.5"
Daniel@0 425 %if (output_type==0)
Daniel@0 426 if (node_types(best_attr)==1) %is a continuous attribute
Daniel@0 427 %find the value that approximate best_threshhold from below (the largest that <= best_threshhold)
Daniel@0 428 best_value=0;
Daniel@0 429 for i=1:size(fam_ev,2) %note: need to search in all cases for all tree, not just in cases for this node
Daniel@0 430 val = fam_ev(best_attr,i);
Daniel@0 431 if (val <= cur_best_threshhold & val > best_value) %val is more clear to best_threshhold
Daniel@0 432 best_value=val;
Daniel@0 433 end
Daniel@0 434 end
Daniel@0 435 tree.nodes(tree.num_node).split_threshhold=best_value; %for cts attribute only
Daniel@0 436 end
Daniel@0 437 %end
Daniel@0 438
Daniel@0 439 if (output_type == 0)
Daniel@0 440 fprintf('Create node %d split at %d gain %8.4f Th %d. Class %d Cases %d Error %d \n',tree.num_node, best_attr, max_gain, tree.nodes(tree.num_node).split_threshhold, top1_class, size_t, size_t - top1_class_cases );
Daniel@0 441 else
Daniel@0 442 fprintf('Create node %d split at %d gain %8.4f Th %d. Mean %8.4f Cases %d\n',tree.num_node, best_attr, max_gain, tree.nodes(tree.num_node).split_threshhold, avg_y_T, size_t );
Daniel@0 443 end
Daniel@0 444
Daniel@0 445 %(6) Foreach T' in the split_T
Daniel@0 446 % if T' is Empty
Daniel@0 447 % Child of node_id is a leaf
Daniel@0 448 % else
Daniel@0 449 % Child of node_id = split_tree (T')
Daniel@0 450 tree.nodes(new_node).is_leaf=0; %because this node will be split, it is not leaf now
Daniel@0 451 for i=1:size(best_split,2)
Daniel@0 452 if (size(best_split{i},2)==0) %T(i) is empty
Daniel@0 453 %create one new leaf node
Daniel@0 454 tree.num_node=tree.num_node+1;
Daniel@0 455 tree.nodes(tree.num_node).used=1; %flag this node is used (0 means node not used, it will be removed from tree at last to save memory)
Daniel@0 456 tree.nodes(tree.num_node).is_leaf=1;
Daniel@0 457 tree.nodes(tree.num_node).children=[];
Daniel@0 458 tree.nodes(tree.num_node).split_id=0;
Daniel@0 459 tree.nodes(tree.num_node).split_threshhold=0;
Daniel@0 460 if (output_type == 0)
Daniel@0 461 tree.nodes(tree.num_node).probs=zeros(1,num_cat); %the prob for each value of class node
Daniel@0 462 tree.nodes(tree.num_node).probs(top1_class)=1; %use the majority class of parent node, like for binary class,
Daniel@0 463 %and majority is class 2, then the CPT is [0 1]
Daniel@0 464 %we may need to use prior to do smoothing, to get [0.001 0.999]
Daniel@0 465 tree.nodes(tree.num_node).error.self_error=0;
Daniel@0 466 tree.nodes(tree.num_node).error.all_error=0;
Daniel@0 467 tree.nodes(tree.num_node).error.all_error_num=0;
Daniel@0 468 else
Daniel@0 469 tree.nodes(tree.num_node).mean = avg_y_T; %just use parent node's mean value
Daniel@0 470 tree.nodes(tree.num_node).std = std_T;
Daniel@0 471 end
Daniel@0 472 %add the new leaf node to parents
Daniel@0 473 num_children=size(tree.nodes(new_node).children,2);
Daniel@0 474 tree.nodes(new_node).children(num_children+1)=tree.num_node;
Daniel@0 475 if (output_type==0)
Daniel@0 476 fprintf('Create leaf node(nullset) %d. %d-th child of Father %d Class %d\n',tree.num_node, i, new_node, top1_class );
Daniel@0 477 else
Daniel@0 478 fprintf('Create leaf node(nullset) %d. %d-th child of Father %d \n',tree.num_node, i, new_node );
Daniel@0 479 end
Daniel@0 480
Daniel@0 481 else
Daniel@0 482 if (node_types(best_attr)==0) % if attr is discrete, it should be removed from the candidate set
Daniel@0 483 new_candidate_attrs = mysetdiff(candidate_attrs,[best_attr]);
Daniel@0 484 else
Daniel@0 485 new_candidate_attrs = candidate_attrs;
Daniel@0 486 end
Daniel@0 487 new_sub_node = split_dtree (CPD, fam_ev, node_sizes, node_types, stop_cases, min_gain, best_split{i}, new_candidate_attrs, num_cat);
Daniel@0 488 %tree.nodes(parent_id).error.all_error += tree.nodes(new_sub_node).error.all_error;
Daniel@0 489 fprintf('Add subtree node %d to %d. #nodes %d\n',new_sub_node,new_node, tree.num_node );
Daniel@0 490
Daniel@0 491 % tree.nodes(new_node).error.all_error_num = tree.nodes(new_node).error.all_error_num + tree.nodes(new_sub_node).error.all_error_num;
Daniel@0 492 %add the new leaf node to parents
Daniel@0 493 num_children=size(tree.nodes(new_node).children,2);
Daniel@0 494 tree.nodes(new_node).children(num_children+1)=new_sub_node;
Daniel@0 495 end
Daniel@0 496 end
Daniel@0 497
Daniel@0 498 %(7) Compute errors of N; for doing pruning
Daniel@0 499 % get the total error for the subtree
Daniel@0 500 if (output_type==0)
Daniel@0 501 tree.nodes(new_node).error.all_error=tree.nodes(new_node).error.all_error_num/size_t;
Daniel@0 502 end
Daniel@0 503 %doing pruning, but doing here is not so efficient, because it is bottom up.
Daniel@0 504 %if tree.nodes()
Daniel@0 505 %after doing pruning, need to update the all_error to self_error
Daniel@0 506
Daniel@0 507 %(8) Return N
Daniel@0 508
Daniel@0 509
Daniel@0 510
Daniel@0 511
Daniel@0 512 %(1) For discrete output, we use GainRatio defined as below
Daniel@0 513 % Gain(X,T)
Daniel@0 514 % GainRatio(X,T) = ----------
Daniel@0 515 % SplitInfo(X,T)
Daniel@0 516 % where
Daniel@0 517 % Gain(X,T) = Info(T) - Info(X,T)
Daniel@0 518 % |Ti|
Daniel@0 519 % Info(X,T) = Sum for i from 1 to n of ( ---- * Info(Ti))
Daniel@0 520 % |T|
Daniel@0 521
Daniel@0 522 % SplitInfo(D,T) is the information due to the split of T on the basis
Daniel@0 523 % of the value of the categorical attribute D. Thus SplitInfo(D,T) is
Daniel@0 524 % I(|T1|/|T|, |T2|/|T|, .., |Tm|/|T|)
Daniel@0 525 % where {T1, T2, .. Tm} is the partition of T induced by the value of D.
Daniel@0 526
Daniel@0 527 % Definition of Info(Ti)
Daniel@0 528 % If a set T of records is partitioned into disjoint exhaustive classes C1, C2, .., Ck on the basis of the
Daniel@0 529 % value of the categorical attribute, then the information needed to identify the class of an element of T
Daniel@0 530 % is Info(T) = I(P), where P is the probability distribution of the partition (C1, C2, .., Ck):
Daniel@0 531 % P = (|C1|/|T|, |C2|/|T|, ..., |Ck|/|T|)
Daniel@0 532 % Here I(P) is defined as
Daniel@0 533 % I(P) = -(p1*log(p1) + p2*log(p2) + .. + pn*log(pn))
Daniel@0 534 %
Daniel@0 535 %(2) For continuous output (regression tree), we use least squares score (adapted from Leo Breiman's book "Classification and regression trees", page 231
Daniel@0 536 % The original support only binary split, we further extend it to permit multiple-child split
Daniel@0 537 %
Daniel@0 538 % Delta_R = R(T) - Sum for all childe nodes Ti (R(Ti))
Daniel@0 539 % Where R(Ti)= 1/N * Sum for all cases i in node Ti ((yi - avg_y(Ti))^2)
Daniel@0 540 % here N is the number of all training cases for construct the regression tree
Daniel@0 541 % avg_y(Ti) is the average value for output variable for the cases in node Ti
Daniel@0 542
Daniel@0 543 function gain_score = compute_gain (fam_ev, node_sizes, node_types, T, info_T, attr_id, split_T, score_type, output_type)
Daniel@0 544 % COMPUTE_GAIN Compute the score for the split of cases T using attribute attr_id
Daniel@0 545 % gain_score = compute_gain (fam_ev, T, attr_id, node_size, method)
Daniel@0 546 %
Daniel@0 547 % fam_ev(i,j) is the value of attribute i in j-th training cases, the last row is for the class label (self_ev)
Daniel@0 548 % T(i) is the index of i-th cases in current decision tree node, we need split it further
Daniel@0 549 % attr_id is the index of current node considered for a split
Daniel@0 550 % split_T{i} is the i_th subset in partition of cases T according to the value of attribute attr_id
Daniel@0 551 % score_type if 0, is gain ratio, 1 is information gain (only apply to discrete output)
Daniel@0 552 % node_size(i) the node size of i-th node in the family
Daniel@0 553 % output_type: 0 means discrete output, 1 means continuous output.
Daniel@0 554 gain_score=0;
Daniel@0 555 % ***********for DISCRETE output*******************************************************
Daniel@0 556 if (output_type == 0)
Daniel@0 557 % compute Info(T)
Daniel@0 558 total_cnt = size(T,2);
Daniel@0 559 if (total_cnt==0)
Daniel@0 560 return;
Daniel@0 561 end;
Daniel@0 562 %class_split_T = split_cases(fam_ev,node_sizes,node_types,T,size(fam_ev,1),0); %split cases according to class
Daniel@0 563 %info_T = compute_info (fam_ev, T, class_split_T);
Daniel@0 564
Daniel@0 565 % compute Info(X,T)
Daniel@0 566 num_class = size(split_T,2);
Daniel@0 567 subset_sizes = zeros(1,num_class);
Daniel@0 568 info_ti = zeros(1,num_class);
Daniel@0 569 for i=1:num_class
Daniel@0 570 subset_sizes(i)=size(split_T{i},2);
Daniel@0 571 if (subset_sizes(i)~=0)
Daniel@0 572 class_split_Ti = split_cases(fam_ev,node_sizes,node_types,split_T{i},size(fam_ev,1),0); %split cases according to class
Daniel@0 573 info_ti(i) = compute_info(fam_ev, split_T{i}, class_split_Ti);
Daniel@0 574 end
Daniel@0 575 end
Daniel@0 576 ti_ratios = subset_sizes/total_cnt; %get the |Ti|/|T|
Daniel@0 577 info_X_T = sum(ti_ratios.*info_ti);
Daniel@0 578
Daniel@0 579 %get Gain(X,T)
Daniel@0 580 gain_X_T = info_T - info_X_T;
Daniel@0 581
Daniel@0 582 if (score_type == 1) %information gain
Daniel@0 583 gain_score=gain_X_T;
Daniel@0 584 return;
Daniel@0 585 end
Daniel@0 586 %compute the SplitInfo(X,T) //is this also for cts attr, only split into two subsets
Daniel@0 587 splitinfo_T = compute_info (fam_ev, T, split_T);
Daniel@0 588 if (splitinfo_T~=0)
Daniel@0 589 gain_score = gain_X_T/splitinfo_T;
Daniel@0 590 end
Daniel@0 591
Daniel@0 592 % ************for continuous output**************************************************
Daniel@0 593 else
Daniel@0 594 N = size(fam_ev,2);
Daniel@0 595
Daniel@0 596 % compute R(Ti)
Daniel@0 597 num_class = size(split_T,2);
Daniel@0 598 R_Ti = zeros(1,num_class);
Daniel@0 599 for i=1:num_class
Daniel@0 600 if (size(split_T{i},2)~=0)
Daniel@0 601 cases_T = fam_ev(size(fam_ev,1),split_T{i});
Daniel@0 602 avg_y_T = mean(cases_T);
Daniel@0 603 sqr_T = cases_T - avg_y_T;
Daniel@0 604 R_Ti(i) = sum(sqr_T.*sqr_T)/N; % get R(Ti) = 1/N * SUM(y-avg_y)^2
Daniel@0 605 end
Daniel@0 606 end
Daniel@0 607 %delta_R = R(T) - SUM(R(Ti))
Daniel@0 608 gain_score = info_T - sum(R_Ti);
Daniel@0 609
Daniel@0 610 end
Daniel@0 611
Daniel@0 612
Daniel@0 613 % Definition of Info(Ti)
Daniel@0 614 % If a set T of records is partitioned into disjoint exhaustive classes C1, C2, .., Ck on the basis of the
Daniel@0 615 % value of the categorical attribute, then the information needed to identify the class of an element of T
Daniel@0 616 % is Info(T) = I(P), where P is the probability distribution of the partition (C1, C2, .., Ck):
Daniel@0 617 % P = (|C1|/|T|, |C2|/|T|, ..., |Ck|/|T|)
Daniel@0 618 % Here I(P) is defined as
Daniel@0 619 % I(P) = -(p1*log(p1) + p2*log(p2) + .. + pn*log(pn))
Daniel@0 620 function info = compute_info (fam_ev, T, split_T)
Daniel@0 621 % COMPUTE_INFO compute the information for the split of T into split_T
Daniel@0 622 % info = compute_info (fam_ev, T, split_T)
Daniel@0 623
Daniel@0 624 total_cnt = size(T,2);
Daniel@0 625 num_class = size(split_T,2);
Daniel@0 626 subset_sizes = zeros(1,num_class);
Daniel@0 627 probs = zeros(1,num_class);
Daniel@0 628 log_probs = zeros(1,num_class);
Daniel@0 629 for i=1:num_class
Daniel@0 630 subset_sizes(i)=size(split_T{i},2);
Daniel@0 631 end
Daniel@0 632
Daniel@0 633 probs = subset_sizes/total_cnt;
Daniel@0 634 %log_probs = log2(probs); % if probs(i)=0, the log2(probs(i)) will be Inf
Daniel@0 635 for i=1:size(probs,2)
Daniel@0 636 if (probs(i)~=0)
Daniel@0 637 log_probs(i)=log2(probs(i));
Daniel@0 638 end
Daniel@0 639 end
Daniel@0 640
Daniel@0 641 info = sum(-(probs.*log_probs));
Daniel@0 642