annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@tree_CPD/evaluate_tree_performance.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 [score,outputs] = evaluate(CPD, fam, data, ns, cnodes)
Daniel@0 2 % Evaluate evaluate the performance of the classification/regression tree on given complete data
Daniel@0 3 % score = evaluate(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 % Output
Daniel@0 11 % score is the classification accuracy (for classification)
Daniel@0 12 % or mean square deviation (for regression)
Daniel@0 13 % here for every case we use the mean value at the tree leaf node as its predicted value
Daniel@0 14 % outputs(i) is the predicted output value for case i
Daniel@0 15 %
Daniel@0 16 % Author: yimin.zhang@intel.com
Daniel@0 17 % Last updated: Jan. 19, 2002
Daniel@0 18
Daniel@0 19
Daniel@0 20 if iscell(data)
Daniel@0 21 local_data = cell2num(data(fam,:));
Daniel@0 22 else
Daniel@0 23 local_data = data(fam, :);
Daniel@0 24 end
Daniel@0 25
Daniel@0 26 %get local node sizes and node types
Daniel@0 27 node_sizes = ns(fam);
Daniel@0 28 node_types = zeros(1,size(ns,2)); %all nodes are disrete
Daniel@0 29 node_types(cnodes)=1;
Daniel@0 30 node_types=node_types(fam);
Daniel@0 31
Daniel@0 32 fam_size=size(fam,2);
Daniel@0 33 output_type = node_types(fam_size);
Daniel@0 34
Daniel@0 35 num_cases=size(local_data,2);
Daniel@0 36 total_error=0;
Daniel@0 37
Daniel@0 38 outputs=zeros(1,num_cases);
Daniel@0 39 for i=1:num_cases
Daniel@0 40 %class one case using the tree
Daniel@0 41 cur_node=CPD.tree.root; % at the root node of the tree
Daniel@0 42 while (1)
Daniel@0 43 if (CPD.tree.nodes(cur_node).is_leaf==1)
Daniel@0 44 if (output_type==0) %output is discrete
Daniel@0 45 %use the class with max probability as the output
Daniel@0 46 [maxvalue,class_id]=max(CPD.tree.nodes(cur_node).probs);
Daniel@0 47 outputs(i)=class_id;
Daniel@0 48 if (class_id~=local_data(fam_size,i))
Daniel@0 49 total_error=total_error+1;
Daniel@0 50 end
Daniel@0 51 else %output is continuous
Daniel@0 52 %use the mean as the value
Daniel@0 53 outputs(i)=CPD.tree.nodes(cur_node).mean;
Daniel@0 54 cur_deviation = CPD.tree.nodes(cur_node).mean-local_data(fam_size,i);
Daniel@0 55 total_error=total_error+cur_deviation*cur_deviation;
Daniel@0 56 end
Daniel@0 57 break;
Daniel@0 58 end
Daniel@0 59 cur_attr = CPD.tree.nodes(cur_node).split_id;
Daniel@0 60 attr_val = local_data(cur_attr,i);
Daniel@0 61 if (node_types(cur_attr)==0) %discrete attribute
Daniel@0 62 % goto the attr_val -th child
Daniel@0 63 cur_node = CPD.tree.nodes(cur_node).children(attr_val);
Daniel@0 64 else
Daniel@0 65 if (attr_val <= CPD.tree.nodes(cur_node).split_threshhold)
Daniel@0 66 cur_node = CPD.tree.nodes(cur_node).children(1);
Daniel@0 67 else
Daniel@0 68 cur_node = CPD.tree.nodes(cur_node).children(2);
Daniel@0 69 end
Daniel@0 70 end
Daniel@0 71 if (cur_node > CPD.tree.num_node)
Daniel@0 72 fprintf('Fatal error: Tree structure corrupted.\n');
Daniel@0 73 return;
Daniel@0 74 end
Daniel@0 75 end
Daniel@0 76 %update the classification error number
Daniel@0 77 end
Daniel@0 78 if (output_type==0)
Daniel@0 79 score=1-total_error/num_cases;
Daniel@0 80 else
Daniel@0 81 score=total_error/num_cases;
Daniel@0 82 end