wolffd@0: function CPD = learn_params(CPD, fam, data, ns, cnodes, varargin) wolffd@0: % LEARN_PARAMS Construct classification/regression tree given complete data wolffd@0: % CPD = learn_params(CPD, fam, data, ns, cnodes) wolffd@0: % wolffd@0: % fam(i) is the node id of the i-th node in the family of nodes, self node is the last one wolffd@0: % data(i,m) is the value of node i in case m (can be cell array). wolffd@0: % ns(i) is the node size for the i-th node in the whold bnet wolffd@0: % cnodes(i) is the node id for the i-th continuous node in the whole bnet wolffd@0: % wolffd@0: % The following optional arguments can be specified in the form of name/value pairs: wolffd@0: % stop_cases: for early stop (pruning). A node is not split if it has less than k cases. default is 0. wolffd@0: % min_gain: for early stop (pruning). wolffd@0: % For discrete output: A node is not split when the gain of best split is less than min_gain. default is 0. wolffd@0: % For continuous (cts) outpt: A node is not split when the gain of best split is less than min_gain*score(root) wolffd@0: % (we denote it cts_min_gain). default is 0.006 wolffd@0: % %%%%%%%%%%%%%%%%%%%Struction definition of dtree_CPD.tree%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: % tree.num_node the last position in tree.nodes array for adding new nodes, wolffd@0: % it is not always same to number of nodes in a tree, because some position in the wolffd@0: % tree.nodes array can be set to unused (e.g. in tree pruning) wolffd@0: % tree.nodes is the array of nodes in the tree plus some unused nodes. wolffd@0: % tree.nodes(1) is the root for the tree. wolffd@0: % wolffd@0: % Below is the attributes for each node wolffd@0: % tree.nodes(i).used; % flag this node is used (0 means node not used, it can be removed from tree to save memory) wolffd@0: % tree.nodes(i).is_leaf; % if 1 means this node is a leaf, if 0 not a leaf. wolffd@0: % tree.nodes(i).children; % children(i) is the node number in tree.nodes array for the i-th child node wolffd@0: % tree.nodes(i).split_id; % the attribute id used to split this node wolffd@0: % tree.nodes(i).split_threshhold; % the threshhold for continuous attribute to split this node wolffd@0: % %%%%%attributes specially for classification tree (discrete output) wolffd@0: % tree.nodes(i).probs % probs(i) is the prob for i-th value of class node wolffd@0: % % For three output class, the probs = [0.9 0.1 0.0] means the probability of wolffd@0: % % class 1 is 0.9, for class 2 is 0.1, for class 3 is 0.0. wolffd@0: % %%%%%attributes specially for regression tree (continuous output) wolffd@0: % tree.nodes(i).mean % mean output value for this node wolffd@0: % tree.nodes(i).std % standard deviation for output values in this node wolffd@0: % wolffd@0: % Author: yimin.zhang@intel.com wolffd@0: % Last updated: Jan. 19, 2002 wolffd@0: wolffd@0: % Want list: wolffd@0: % (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 wolffd@0: % (2) pruning classification tree using Pessimistic Error Pruning wolffd@0: % (3) bi_search for strings (used for transform data to BNT format) wolffd@0: wolffd@0: global tree %tree must be global so that it can be accessed in recursive slitting function wolffd@0: global cts_min_gain wolffd@0: tree=[]; % clear the tree wolffd@0: tree.num_node=0; wolffd@0: cts_min_gain=0; wolffd@0: wolffd@0: stop_cases=0; wolffd@0: min_gain=0; wolffd@0: wolffd@0: args = varargin; wolffd@0: nargs = length(args); wolffd@0: if (nargs>0) wolffd@0: if isstr(args{1}) wolffd@0: for i=1:2:nargs wolffd@0: switch args{i}, wolffd@0: case 'stop_cases', stop_cases = args{i+1}; wolffd@0: case 'min_gain', min_gain = args{i+1}; wolffd@0: end wolffd@0: end wolffd@0: else wolffd@0: error(['error in input parameters']); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: if iscell(data) wolffd@0: local_data = cell2num(data(fam,:)); wolffd@0: else wolffd@0: local_data = data(fam, :); wolffd@0: end wolffd@0: %counts = compute_counts(local_data, CPD.sizes); wolffd@0: %CPD.CPT = mk_stochastic(counts + CPD.prior); % bug fix 11/5/01 wolffd@0: node_types = zeros(1,size(ns,2)); %all nodes are disrete wolffd@0: node_types(cnodes)=1; wolffd@0: %make the data be BNT compliant (values for discrete nodes are from 1-n, here n is the node size) wolffd@0: %trans_data=transform_data(local_data,'tmp.dat',[]); %here no cts nodes wolffd@0: wolffd@0: build_dtree (CPD, local_data, ns(fam), node_types(fam),stop_cases,min_gain); wolffd@0: %CPD.tree=copy_tree(tree); wolffd@0: CPD.tree=tree; %copy the tree constructed to CPD wolffd@0: wolffd@0: wolffd@0: function new_tree = copy_tree(tree) wolffd@0: % copy the tree to new_tree wolffd@0: new_tree.num_node=tree.num_node; wolffd@0: new_tree.root = tree.root; wolffd@0: for i=1:tree.num_node wolffd@0: new_tree.nodes(i)=tree.nodes(i); wolffd@0: end wolffd@0: wolffd@0: wolffd@0: function build_dtree (CPD, fam_ev, node_sizes, node_types,stop_cases,min_gain) wolffd@0: global tree wolffd@0: global cts_min_gain wolffd@0: wolffd@0: tree.num_node=0; %the current number of nodes in the tree wolffd@0: tree.root=1; wolffd@0: wolffd@0: T = 1:size(fam_ev,2) ; %all cases wolffd@0: candidate_attrs = 1:(size(node_sizes,2)-1); %all attributes wolffd@0: node_id=1; %the root node wolffd@0: lastnode=size(node_sizes,2); %the last element in all nodes is the dependent variable (category node) wolffd@0: num_cat=node_sizes(lastnode); wolffd@0: wolffd@0: % get minimum gain for cts output (used in stop splitting) wolffd@0: if (node_types(size(fam_ev,1))==1) %cts output wolffd@0: N = size(fam_ev,2); wolffd@0: output_id = size(fam_ev,1); wolffd@0: cases_T = fam_ev(output_id,:); %get all the output value for cases T wolffd@0: std_T = std(cases_T); wolffd@0: avg_y_T = mean(cases_T); wolffd@0: sqr_T = cases_T - avg_y_T; wolffd@0: cts_min_gain = min_gain*(sum(sqr_T.*sqr_T)/N); % min_gain * (R(root) = 1/N * SUM(y-avg_y)^2) wolffd@0: end wolffd@0: wolffd@0: split_dtree (CPD, fam_ev, node_sizes, node_types, stop_cases,min_gain, T, candidate_attrs, num_cat); wolffd@0: wolffd@0: wolffd@0: wolffd@0: % pruning method wolffd@0: % (1) Restrictions on minimum node size: A node is not split if it has smaller than k cases. wolffd@0: % (2) Threshholds on impurity: a threshhold is imposed on the splitting test score. Threshhold can be wolffd@0: % imposed on local goodness measure (the gain_ratio of a node) or global goodness. wolffd@0: % (3) Mininum Error Pruning (MEP), (no need pruning set) wolffd@0: % Prune if static error<=backed-up error wolffd@0: % Static error at node v: e(v) = (Nc + 1)/(N+k) (laplace estimate, prior for each class equal) wolffd@0: % here N is # of all examples, Nc is # of majority class examples, k is number of classes wolffd@0: % Backed-up error at node v: (Ti is the i-th subtree root) wolffd@0: % E(T) = Sum_1_to_n(pi*e(Ti)) wolffd@0: % (4) Pessimistic Error Pruning (PEP), used in Quilan C4.5 (no need pruning set, efficient because of pruning top-down) wolffd@0: % Probability of error (apparent error rate) wolffd@0: % q = (N-Nc+0.5)/N wolffd@0: % where N=#examples, Nc=#examples in majority class wolffd@0: % Error of a node v (if pruned) q(v)= (Nv- Nc,v + 0.5)/Nv wolffd@0: % Error of a subtree q(T)= Sum_of_l_leaves(Nl - Nc,l + 0.5)/Sum_of_l_leaves(Nl) wolffd@0: % Prune if q(v)<=q(T) wolffd@0: % wolffd@0: % Implementation statuts: wolffd@0: % (1)(2) has been implemented as the input parameters of learn_params. wolffd@0: % (4) is implemented in this function wolffd@0: function pruning(fam_ev,node_sizes,node_types) wolffd@0: % PRUNING prune the constructed tree using PEP wolffd@0: % pruning(fam_ev,node_sizes,node_types) wolffd@0: % wolffd@0: % 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) wolffd@0: % node_sizes(i) is the node size for the i-th node in the family wolffd@0: % node_types(i) is the node type for the i-th node in the family, 0 for disrete node, 1 for continous node wolffd@0: % the global parameter 'tree' is for storing the input tree and the pruned tree wolffd@0: wolffd@0: wolffd@0: function split_T = split_cases(fam_ev,node_sizes,node_types,T,node_i, threshhold) wolffd@0: % SPLIT_CASES split the cases T according to values of node_i in the family wolffd@0: % split_T = split_cases(fam_ev,node_sizes,node_types,T,node_i) wolffd@0: % wolffd@0: % 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) wolffd@0: % node_sizes(i) is the node size for the i-th node in the family wolffd@0: % node_types(i) is the node type for the i-th node in the family, 0 for disrete node, 1 for continous node wolffd@0: % node_i is the attribute we need to split wolffd@0: wolffd@0: if (node_types(node_i)==0) %discrete attribute wolffd@0: %init the subsets of T wolffd@0: 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 wolffd@0: for i=1:node_sizes(node_i) % here we assume that the value of an attribute is 1:node_size wolffd@0: split_T{i}=zeros(1,0); wolffd@0: end wolffd@0: wolffd@0: size_t = size(T,2); wolffd@0: for i=1:size_t wolffd@0: case_id = T(i); wolffd@0: %put this case into one subset of split_T according to its value for node_i wolffd@0: value = fam_ev(node_i,case_id); wolffd@0: pos = size(split_T{value},2)+1; wolffd@0: split_T{value}(pos)=case_id; % here assumes the value of an attribute is 1:node_size wolffd@0: end wolffd@0: else %continuous attribute wolffd@0: %init the subsets of T wolffd@0: split_T = cell(1,2); %T will be separated into 2 subsets (<=threshhold) (>threshhold) wolffd@0: for i=1:2 wolffd@0: split_T{i}=zeros(1,0); wolffd@0: end wolffd@0: wolffd@0: size_t = size(T,2); wolffd@0: for i=1:size_t wolffd@0: case_id = T(i); wolffd@0: %put this case into one subset of split_T according to its value for node_i wolffd@0: value = fam_ev(node_i,case_id); wolffd@0: subset_num=1; wolffd@0: if (value>threshhold) wolffd@0: subset_num=2; wolffd@0: end wolffd@0: pos = size(split_T{subset_num},2)+1; wolffd@0: split_T{subset_num}(pos)=case_id; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: wolffd@0: wolffd@0: function new_node = split_dtree (CPD, fam_ev, node_sizes, node_types, stop_cases, min_gain, T, candidate_attrs, num_cat) wolffd@0: % SPLIT_TREE Split the tree at node node_id with cases T (actually it is just indexes to family evidences). wolffd@0: % new_node = split_dtree (fam_ev, node_sizes, node_types, T, node_id, num_cat, method) wolffd@0: % wolffd@0: % 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) wolffd@0: % node_sizes{i} is the node size for the i-th node in the family wolffd@0: % node_types{i} is the node type for the i-th node in the family, 0 for disrete node, 1 for continous node wolffd@0: % stop_cases is the threshold of number of cases to stop slitting wolffd@0: % min_gain is the minimum gain need to split a node wolffd@0: % T(i) is the index of i-th cases in current decision tree node, we need split it further wolffd@0: % candidate_attrs(i) the node id for the i-th attribute that still need to be considered as split attribute wolffd@0: %%%%% node_id is the index of current node considered for a split wolffd@0: % num_cat is the number of output categories for the decision tree wolffd@0: % output: wolffd@0: % new_node is the new node created wolffd@0: global tree wolffd@0: global cts_min_gain wolffd@0: wolffd@0: size_fam = size(fam_ev,1); %number of family size wolffd@0: output_type = node_types(size_fam); %the type of output for the tree (0 is discrete, 1 is continuous) wolffd@0: size_attrs = size(candidate_attrs,2); %number of candidate attributes wolffd@0: size_t = size(T,2); %number of training cases in this tree node wolffd@0: wolffd@0: %(1)computeFrequenceyForEachClass(T) wolffd@0: if (output_type==0) %discrete output wolffd@0: class_freqs = zeros(1,num_cat); wolffd@0: for i=1:size_t wolffd@0: case_id = T(i); wolffd@0: case_class = fam_ev(size_fam,case_id); %get the class label for this case wolffd@0: class_freqs(case_class)=class_freqs(case_class)+1; wolffd@0: end wolffd@0: else %cts output wolffd@0: N = size(fam_ev,2); wolffd@0: cases_T = fam_ev(size(fam_ev,1),T); %get the output value for cases T wolffd@0: std_T = std(cases_T); wolffd@0: end wolffd@0: wolffd@0: %(2) if OneClass (for discrete output) or same output value (for cts output) or Class With #examples < stop_cases wolffd@0: % return a leaf; wolffd@0: % create a decision node N; wolffd@0: wolffd@0: % get majority class in this node wolffd@0: if (output_type == 0) wolffd@0: top1_class = 0; %the class with the largest number of cases wolffd@0: top1_class_cases = 0; %the number of cases in top1_class wolffd@0: [top1_class_cases,top1_class]=max(class_freqs); wolffd@0: end wolffd@0: wolffd@0: if (size_t==0) %impossble wolffd@0: new_node=-1; wolffd@0: fprintf('Fatal error: please contact the author. \n'); wolffd@0: return; wolffd@0: end wolffd@0: wolffd@0: % stop splitting if needed wolffd@0: %for discrete output: one class wolffd@0: %for cts output, all output value in cases are same wolffd@0: %cases too little wolffd@0: if ( (output_type==0 & top1_class_cases == size_t) | (output_type==1 & std_T == 0) | (size_t < stop_cases)) wolffd@0: %create one new leaf node wolffd@0: tree.num_node=tree.num_node+1; wolffd@0: 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) wolffd@0: tree.nodes(tree.num_node).is_leaf=1; wolffd@0: tree.nodes(tree.num_node).children=[]; wolffd@0: tree.nodes(tree.num_node).split_id=0; %the attribute(parent) id to split this tree node wolffd@0: tree.nodes(tree.num_node).split_threshhold=0; wolffd@0: if (output_type==0) wolffd@0: tree.nodes(tree.num_node).probs=class_freqs/size_t; %the prob for each value of class node wolffd@0: wolffd@0: % tree.nodes(tree.num_node).probs=zeros(1,num_cat); %the prob for each value of class node wolffd@0: % tree.nodes(tree.num_node).probs(top1_class)=1; %use the majority class of parent node, like for binary class, wolffd@0: %and majority is class 2, then the CPT is [0 1] wolffd@0: %we may need to use prior to do smoothing, to get [0.001 0.999] wolffd@0: 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 wolffd@0: 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 wolffd@0: tree.nodes(tree.num_node).error.all_error_num=size_t - top1_class_cases; wolffd@0: 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 ); wolffd@0: else wolffd@0: avg_y_T = mean(cases_T); wolffd@0: tree.nodes(tree.num_node).mean = avg_y_T; wolffd@0: tree.nodes(tree.num_node).std = std_T; wolffd@0: 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); wolffd@0: end wolffd@0: new_node = tree.num_node; wolffd@0: return; wolffd@0: end wolffd@0: wolffd@0: %create one new node wolffd@0: tree.num_node=tree.num_node+1; wolffd@0: 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) wolffd@0: tree.nodes(tree.num_node).is_leaf=1; wolffd@0: tree.nodes(tree.num_node).children=[]; wolffd@0: tree.nodes(tree.num_node).split_id=0; wolffd@0: tree.nodes(tree.num_node).split_threshhold=0; wolffd@0: if (output_type==0) wolffd@0: tree.nodes(tree.num_node).error.self_error=1-top1_class_cases/size_t; wolffd@0: tree.nodes(tree.num_node).error.all_error=0; wolffd@0: tree.nodes(tree.num_node).error.all_error_num=0; wolffd@0: else wolffd@0: avg_y_T = mean(cases_T); wolffd@0: tree.nodes(tree.num_node).mean = avg_y_T; wolffd@0: tree.nodes(tree.num_node).std = std_T; wolffd@0: end wolffd@0: new_node = tree.num_node; wolffd@0: wolffd@0: %Stop splitting if no attributes left in this node wolffd@0: if (size_attrs==0) wolffd@0: if (output_type==0) wolffd@0: tree.nodes(tree.num_node).probs=class_freqs/size_t; %the prob for each value of class node wolffd@0: tree.nodes(tree.num_node).error.all_error=1-top1_class_cases/size_t; wolffd@0: tree.nodes(tree.num_node).error.all_error_num=size_t - top1_class_cases; wolffd@0: 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 ); wolffd@0: else wolffd@0: 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); wolffd@0: end wolffd@0: return; wolffd@0: end wolffd@0: wolffd@0: wolffd@0: %(3) for each attribute A wolffd@0: % ComputeGain(A); wolffd@0: max_gain=0; %the max gain score (for discrete information gain or gain ration, for cts node the R(T)) wolffd@0: best_attr=0; %the attribute with the max_gain wolffd@0: best_split = []; %the split of T according to the value of best_attr wolffd@0: cur_best_threshhold = 0; %the threshhold for split continuous attribute wolffd@0: best_threshhold=0; wolffd@0: wolffd@0: % compute Info(T) (for discrete output) wolffd@0: if (output_type == 0) wolffd@0: class_split_T = split_cases(fam_ev,node_sizes,node_types,T,size(fam_ev,1),0); %split cases according to class wolffd@0: info_T = compute_info (fam_ev, T, class_split_T); wolffd@0: else % compute R(T) (for cts output) wolffd@0: % N = size(fam_ev,2); wolffd@0: % cases_T = fam_ev(size(fam_ev,1),T); %get the output value for cases T wolffd@0: % std_T = std(cases_T); wolffd@0: % avg_y_T = mean(cases_T); wolffd@0: sqr_T = cases_T - avg_y_T; wolffd@0: R_T = sum(sqr_T.*sqr_T)/N; % get R(T) = 1/N * SUM(y-avg_y)^2 wolffd@0: info_T = R_T; wolffd@0: end wolffd@0: wolffd@0: for i=1:(size_fam-1) wolffd@0: if (myismember(i,candidate_attrs)) %if this attribute still in the candidate attribute set wolffd@0: if (node_types(i)==0) %discrete attibute wolffd@0: split_T = split_cases(fam_ev,node_sizes,node_types,T,i,0); %split cases according to value of attribute i wolffd@0: % For cts output, we compute the least square gain. wolffd@0: % For discrete output, we compute gain ratio wolffd@0: cur_gain = compute_gain(fam_ev,node_sizes,node_types,T,info_T,i,split_T,0,output_type); %gain ratio wolffd@0: else %cts attribute wolffd@0: %get the values of this attribute wolffd@0: ev = fam_ev(:,T); wolffd@0: values = ev(i,:); wolffd@0: sort_v = sort(values); wolffd@0: %remove the duplicate values in sort_v wolffd@0: v_set = unique(sort_v); wolffd@0: best_gain = 0; wolffd@0: best_threshhold = 0; wolffd@0: best_split1 = []; wolffd@0: wolffd@0: %find the best split for this cts attribute wolffd@0: % see "Quilan 96: Improved Use of Continuous Attributes in C4.5" wolffd@0: for j=1:(size(v_set,2)-1) wolffd@0: mid_v = (v_set(j)+v_set(j+1))/2; wolffd@0: split_T = split_cases(fam_ev,node_sizes,node_types,T,i,mid_v); %split cases according to value of attribute i (<=mid_v) wolffd@0: % For cts output, we compute the least square gain. wolffd@0: % For discrete output, we use Quilan 96: use information gain instead of gain ratio to select threshhold wolffd@0: cur_gain = compute_gain(fam_ev,node_sizes,node_types,T,info_T,i,split_T,1,output_type); wolffd@0: %if (i==6) wolffd@0: % fprintf('gain %8.5f threshhold %6.3f spliting %d\n', cur_gain, mid_v, size(split_T{1},2)); wolffd@0: %end wolffd@0: wolffd@0: if (best_gain < cur_gain) wolffd@0: best_gain = cur_gain; wolffd@0: best_threshhold = mid_v; wolffd@0: %best_split1 = split_T; %here we need to copy array, not good!!! (maybe we can compute after we get best_attr wolffd@0: end wolffd@0: end wolffd@0: %recalculate the gain_ratio of the best_threshhold wolffd@0: split_T = split_cases(fam_ev,node_sizes,node_types,T,i,best_threshhold); wolffd@0: best_gain = compute_gain(fam_ev,node_sizes,node_types,T,info_T,i,split_T,0,output_type); %gain_ratio wolffd@0: if (output_type==0) %for discrete output wolffd@0: 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 wolffd@0: else %for cts output wolffd@0: cur_gain = best_gain; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: if (max_gain < cur_gain) wolffd@0: max_gain = cur_gain; wolffd@0: best_attr = i; wolffd@0: cur_best_threshhold=best_threshhold; %save the threshhold wolffd@0: %best_split = split_T; %here we need to copy array, not good!!! So we will recalculate in below line 313 wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: % stop splitting if gain is too small wolffd@0: if (max_gain==0 | (output_type==0 & max_gain < min_gain) | (output_type==1 & max_gain < cts_min_gain)) wolffd@0: if (output_type==0) wolffd@0: tree.nodes(tree.num_node).probs=class_freqs/size_t; %the prob for each value of class node wolffd@0: tree.nodes(tree.num_node).error.all_error=1-top1_class_cases/size_t; wolffd@0: tree.nodes(tree.num_node).error.all_error_num=size_t - top1_class_cases; wolffd@0: 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 ); wolffd@0: else wolffd@0: 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); wolffd@0: end wolffd@0: return; wolffd@0: end wolffd@0: wolffd@0: %get the split of cases according to the best split attribute wolffd@0: if (node_types(best_attr)==0) %discrete attibute wolffd@0: best_split = split_cases(fam_ev,node_sizes,node_types,T,best_attr,0); wolffd@0: else wolffd@0: best_split = split_cases(fam_ev,node_sizes,node_types,T,best_attr,cur_best_threshhold); wolffd@0: end wolffd@0: wolffd@0: %(4) best_attr = AttributeWithBestGain; wolffd@0: %(5) if best_attr is continuous ???? why need this? maybe the value in the decision tree must appeared in data wolffd@0: % find threshhold in all cases that <= max_V wolffd@0: % change the split of T wolffd@0: tree.nodes(tree.num_node).split_id=best_attr; wolffd@0: tree.nodes(tree.num_node).split_threshhold=cur_best_threshhold; %for cts attribute only wolffd@0: wolffd@0: %note: below threshhold rejust is linera search, so it is slow. A better method is described in paper "Efficient C4.5" wolffd@0: %if (output_type==0) wolffd@0: if (node_types(best_attr)==1) %is a continuous attribute wolffd@0: %find the value that approximate best_threshhold from below (the largest that <= best_threshhold) wolffd@0: best_value=0; wolffd@0: for i=1:size(fam_ev,2) %note: need to search in all cases for all tree, not just in cases for this node wolffd@0: val = fam_ev(best_attr,i); wolffd@0: if (val <= cur_best_threshhold & val > best_value) %val is more clear to best_threshhold wolffd@0: best_value=val; wolffd@0: end wolffd@0: end wolffd@0: tree.nodes(tree.num_node).split_threshhold=best_value; %for cts attribute only wolffd@0: end wolffd@0: %end wolffd@0: wolffd@0: if (output_type == 0) wolffd@0: 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 ); wolffd@0: else wolffd@0: 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 ); wolffd@0: end wolffd@0: wolffd@0: %(6) Foreach T' in the split_T wolffd@0: % if T' is Empty wolffd@0: % Child of node_id is a leaf wolffd@0: % else wolffd@0: % Child of node_id = split_tree (T') wolffd@0: tree.nodes(new_node).is_leaf=0; %because this node will be split, it is not leaf now wolffd@0: for i=1:size(best_split,2) wolffd@0: if (size(best_split{i},2)==0) %T(i) is empty wolffd@0: %create one new leaf node wolffd@0: tree.num_node=tree.num_node+1; wolffd@0: 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) wolffd@0: tree.nodes(tree.num_node).is_leaf=1; wolffd@0: tree.nodes(tree.num_node).children=[]; wolffd@0: tree.nodes(tree.num_node).split_id=0; wolffd@0: tree.nodes(tree.num_node).split_threshhold=0; wolffd@0: if (output_type == 0) wolffd@0: tree.nodes(tree.num_node).probs=zeros(1,num_cat); %the prob for each value of class node wolffd@0: tree.nodes(tree.num_node).probs(top1_class)=1; %use the majority class of parent node, like for binary class, wolffd@0: %and majority is class 2, then the CPT is [0 1] wolffd@0: %we may need to use prior to do smoothing, to get [0.001 0.999] wolffd@0: tree.nodes(tree.num_node).error.self_error=0; wolffd@0: tree.nodes(tree.num_node).error.all_error=0; wolffd@0: tree.nodes(tree.num_node).error.all_error_num=0; wolffd@0: else wolffd@0: tree.nodes(tree.num_node).mean = avg_y_T; %just use parent node's mean value wolffd@0: tree.nodes(tree.num_node).std = std_T; wolffd@0: end wolffd@0: %add the new leaf node to parents wolffd@0: num_children=size(tree.nodes(new_node).children,2); wolffd@0: tree.nodes(new_node).children(num_children+1)=tree.num_node; wolffd@0: if (output_type==0) wolffd@0: fprintf('Create leaf node(nullset) %d. %d-th child of Father %d Class %d\n',tree.num_node, i, new_node, top1_class ); wolffd@0: else wolffd@0: fprintf('Create leaf node(nullset) %d. %d-th child of Father %d \n',tree.num_node, i, new_node ); wolffd@0: end wolffd@0: wolffd@0: else wolffd@0: if (node_types(best_attr)==0) % if attr is discrete, it should be removed from the candidate set wolffd@0: new_candidate_attrs = mysetdiff(candidate_attrs,[best_attr]); wolffd@0: else wolffd@0: new_candidate_attrs = candidate_attrs; wolffd@0: end wolffd@0: new_sub_node = split_dtree (CPD, fam_ev, node_sizes, node_types, stop_cases, min_gain, best_split{i}, new_candidate_attrs, num_cat); wolffd@0: %tree.nodes(parent_id).error.all_error += tree.nodes(new_sub_node).error.all_error; wolffd@0: fprintf('Add subtree node %d to %d. #nodes %d\n',new_sub_node,new_node, tree.num_node ); wolffd@0: wolffd@0: % 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; wolffd@0: %add the new leaf node to parents wolffd@0: num_children=size(tree.nodes(new_node).children,2); wolffd@0: tree.nodes(new_node).children(num_children+1)=new_sub_node; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: %(7) Compute errors of N; for doing pruning wolffd@0: % get the total error for the subtree wolffd@0: if (output_type==0) wolffd@0: tree.nodes(new_node).error.all_error=tree.nodes(new_node).error.all_error_num/size_t; wolffd@0: end wolffd@0: %doing pruning, but doing here is not so efficient, because it is bottom up. wolffd@0: %if tree.nodes() wolffd@0: %after doing pruning, need to update the all_error to self_error wolffd@0: wolffd@0: %(8) Return N wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: %(1) For discrete output, we use GainRatio defined as below wolffd@0: % Gain(X,T) wolffd@0: % GainRatio(X,T) = ---------- wolffd@0: % SplitInfo(X,T) wolffd@0: % where wolffd@0: % Gain(X,T) = Info(T) - Info(X,T) wolffd@0: % |Ti| wolffd@0: % Info(X,T) = Sum for i from 1 to n of ( ---- * Info(Ti)) wolffd@0: % |T| wolffd@0: wolffd@0: % SplitInfo(D,T) is the information due to the split of T on the basis wolffd@0: % of the value of the categorical attribute D. Thus SplitInfo(D,T) is wolffd@0: % I(|T1|/|T|, |T2|/|T|, .., |Tm|/|T|) wolffd@0: % where {T1, T2, .. Tm} is the partition of T induced by the value of D. wolffd@0: wolffd@0: % Definition of Info(Ti) wolffd@0: % If a set T of records is partitioned into disjoint exhaustive classes C1, C2, .., Ck on the basis of the wolffd@0: % value of the categorical attribute, then the information needed to identify the class of an element of T wolffd@0: % is Info(T) = I(P), where P is the probability distribution of the partition (C1, C2, .., Ck): wolffd@0: % P = (|C1|/|T|, |C2|/|T|, ..., |Ck|/|T|) wolffd@0: % Here I(P) is defined as wolffd@0: % I(P) = -(p1*log(p1) + p2*log(p2) + .. + pn*log(pn)) wolffd@0: % wolffd@0: %(2) For continuous output (regression tree), we use least squares score (adapted from Leo Breiman's book "Classification and regression trees", page 231 wolffd@0: % The original support only binary split, we further extend it to permit multiple-child split wolffd@0: % wolffd@0: % Delta_R = R(T) - Sum for all childe nodes Ti (R(Ti)) wolffd@0: % Where R(Ti)= 1/N * Sum for all cases i in node Ti ((yi - avg_y(Ti))^2) wolffd@0: % here N is the number of all training cases for construct the regression tree wolffd@0: % avg_y(Ti) is the average value for output variable for the cases in node Ti wolffd@0: wolffd@0: function gain_score = compute_gain (fam_ev, node_sizes, node_types, T, info_T, attr_id, split_T, score_type, output_type) wolffd@0: % COMPUTE_GAIN Compute the score for the split of cases T using attribute attr_id wolffd@0: % gain_score = compute_gain (fam_ev, T, attr_id, node_size, method) wolffd@0: % wolffd@0: % 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) wolffd@0: % T(i) is the index of i-th cases in current decision tree node, we need split it further wolffd@0: % attr_id is the index of current node considered for a split wolffd@0: % split_T{i} is the i_th subset in partition of cases T according to the value of attribute attr_id wolffd@0: % score_type if 0, is gain ratio, 1 is information gain (only apply to discrete output) wolffd@0: % node_size(i) the node size of i-th node in the family wolffd@0: % output_type: 0 means discrete output, 1 means continuous output. wolffd@0: gain_score=0; wolffd@0: % ***********for DISCRETE output******************************************************* wolffd@0: if (output_type == 0) wolffd@0: % compute Info(T) wolffd@0: total_cnt = size(T,2); wolffd@0: if (total_cnt==0) wolffd@0: return; wolffd@0: end; wolffd@0: %class_split_T = split_cases(fam_ev,node_sizes,node_types,T,size(fam_ev,1),0); %split cases according to class wolffd@0: %info_T = compute_info (fam_ev, T, class_split_T); wolffd@0: wolffd@0: % compute Info(X,T) wolffd@0: num_class = size(split_T,2); wolffd@0: subset_sizes = zeros(1,num_class); wolffd@0: info_ti = zeros(1,num_class); wolffd@0: for i=1:num_class wolffd@0: subset_sizes(i)=size(split_T{i},2); wolffd@0: if (subset_sizes(i)~=0) wolffd@0: class_split_Ti = split_cases(fam_ev,node_sizes,node_types,split_T{i},size(fam_ev,1),0); %split cases according to class wolffd@0: info_ti(i) = compute_info(fam_ev, split_T{i}, class_split_Ti); wolffd@0: end wolffd@0: end wolffd@0: ti_ratios = subset_sizes/total_cnt; %get the |Ti|/|T| wolffd@0: info_X_T = sum(ti_ratios.*info_ti); wolffd@0: wolffd@0: %get Gain(X,T) wolffd@0: gain_X_T = info_T - info_X_T; wolffd@0: wolffd@0: if (score_type == 1) %information gain wolffd@0: gain_score=gain_X_T; wolffd@0: return; wolffd@0: end wolffd@0: %compute the SplitInfo(X,T) //is this also for cts attr, only split into two subsets wolffd@0: splitinfo_T = compute_info (fam_ev, T, split_T); wolffd@0: if (splitinfo_T~=0) wolffd@0: gain_score = gain_X_T/splitinfo_T; wolffd@0: end wolffd@0: wolffd@0: % ************for continuous output************************************************** wolffd@0: else wolffd@0: N = size(fam_ev,2); wolffd@0: wolffd@0: % compute R(Ti) wolffd@0: num_class = size(split_T,2); wolffd@0: R_Ti = zeros(1,num_class); wolffd@0: for i=1:num_class wolffd@0: if (size(split_T{i},2)~=0) wolffd@0: cases_T = fam_ev(size(fam_ev,1),split_T{i}); wolffd@0: avg_y_T = mean(cases_T); wolffd@0: sqr_T = cases_T - avg_y_T; wolffd@0: R_Ti(i) = sum(sqr_T.*sqr_T)/N; % get R(Ti) = 1/N * SUM(y-avg_y)^2 wolffd@0: end wolffd@0: end wolffd@0: %delta_R = R(T) - SUM(R(Ti)) wolffd@0: gain_score = info_T - sum(R_Ti); wolffd@0: wolffd@0: end wolffd@0: wolffd@0: wolffd@0: % Definition of Info(Ti) wolffd@0: % If a set T of records is partitioned into disjoint exhaustive classes C1, C2, .., Ck on the basis of the wolffd@0: % value of the categorical attribute, then the information needed to identify the class of an element of T wolffd@0: % is Info(T) = I(P), where P is the probability distribution of the partition (C1, C2, .., Ck): wolffd@0: % P = (|C1|/|T|, |C2|/|T|, ..., |Ck|/|T|) wolffd@0: % Here I(P) is defined as wolffd@0: % I(P) = -(p1*log(p1) + p2*log(p2) + .. + pn*log(pn)) wolffd@0: function info = compute_info (fam_ev, T, split_T) wolffd@0: % COMPUTE_INFO compute the information for the split of T into split_T wolffd@0: % info = compute_info (fam_ev, T, split_T) wolffd@0: wolffd@0: total_cnt = size(T,2); wolffd@0: num_class = size(split_T,2); wolffd@0: subset_sizes = zeros(1,num_class); wolffd@0: probs = zeros(1,num_class); wolffd@0: log_probs = zeros(1,num_class); wolffd@0: for i=1:num_class wolffd@0: subset_sizes(i)=size(split_T{i},2); wolffd@0: end wolffd@0: wolffd@0: probs = subset_sizes/total_cnt; wolffd@0: %log_probs = log2(probs); % if probs(i)=0, the log2(probs(i)) will be Inf wolffd@0: for i=1:size(probs,2) wolffd@0: if (probs(i)~=0) wolffd@0: log_probs(i)=log2(probs(i)); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: info = sum(-(probs.*log_probs)); wolffd@0: