annotate toolboxes/FullBNT-1.0.7/bnt/examples/static/dtree/transform_data_into_bnt_format.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [bnt_data, old_values] = transform_data_into_bnt_format(data,cnodes)
wolffd@0 2 % TRANSFORM_DATA_TO_BNT_FORMAT Ensures discrete variables have values 1,2,..,k
wolffd@0 3 % e.g., if the values of a discrete are [0 1 6], they must be mapped to [1 2 3]
wolffd@0 4 %
wolffd@0 5 % data(i,j) is the value for i-th node in j-th case.
wolffd@0 6 % bnt_data(i,j) is the new value.
wolffd@0 7 % old_values{i} are the original values for node i.
wolffd@0 8 % cnodes is the list of all continous nodes, e.g. [3 5] means the 3rd and 5th node is continuous
wolffd@0 9 %
wolffd@0 10 % Author: yimin.zhang@intel.com
wolffd@0 11 % Last updated: Jan. 22, 2002 by Kevin Murphy.
wolffd@0 12
wolffd@0 13 num_nodes=size(data,1);
wolffd@0 14 num_cases=size(data,2);
wolffd@0 15 old_values=cell(1,num_nodes);
wolffd@0 16
wolffd@0 17 for i=1:num_nodes
wolffd@0 18 if (myismember(i,cnodes)==1) %cts nodes no need to be transformed
wolffd@0 19 %just copy the data
wolffd@0 20 bnt_data(i,:)=data(i,:);
wolffd@0 21 continue;
wolffd@0 22 end
wolffd@0 23 values = data(i,:);
wolffd@0 24 sort_v = sort(values);
wolffd@0 25 %remove the duplicate values in sort_v
wolffd@0 26 v_set = unique(sort_v);
wolffd@0 27
wolffd@0 28 %transform the values
wolffd@0 29 for j=1:size(values,2)
wolffd@0 30 index = binary_search(v_set,values(j));
wolffd@0 31 if (index==-1)
wolffd@0 32 fprintf('value not found in tranforming data to bnt format.\n');
wolffd@0 33 return;
wolffd@0 34 end
wolffd@0 35 bnt_data(i,j)=index;
wolffd@0 36 end
wolffd@0 37 old_values{i}=v_set;
wolffd@0 38 end
wolffd@0 39
wolffd@0 40
wolffd@0 41 %%%%%%%%%%%%
wolffd@0 42
wolffd@0 43 function index=binary_search(vector, value)
wolffd@0 44 % BI_SEARCH do binary search for value in the vector
wolffd@0 45 % Author: yimin.zhang@intel.com
wolffd@0 46 % Last updated: Jan. 19, 2002
wolffd@0 47
wolffd@0 48 begin_index=1;
wolffd@0 49 end_index=size(vector,2);
wolffd@0 50 index=-1;
wolffd@0 51 while (begin_index<=end_index)
wolffd@0 52 mid=floor((begin_index+end_index)/2);
wolffd@0 53 if (isstr(vector(mid)))
wolffd@0 54 % need to write a strcmp to return three result (< = >)
wolffd@0 55 else
wolffd@0 56 if (value==vector(mid))
wolffd@0 57 index=mid;
wolffd@0 58 return;
wolffd@0 59 elseif (value>vector(mid))
wolffd@0 60 begin_index=mid+1;
wolffd@0 61 else
wolffd@0 62 end_index=mid-1;
wolffd@0 63 end
wolffd@0 64 end
wolffd@0 65 end
wolffd@0 66 return;