如前所述,通过传进来的空节点(已经设置最大层数为3)构造初始左右节点
如果不是构造根节点,都要指定父节点papa(为神马没有mama )
父节点其实很关键,尤其在calc_output的时候,其实是先让祖先分类,分剩下的才轮到子孙继续划分
tree_node_left = tree_node; tree_node_right = tree_node; if(nargin > 4) tree_node_left.parent = papa; tree_node_right.parent = papa; end
Distr = weights; trainpat = dataset; traintarg = labels; tr_size = size(trainpat, 2); T_MIN = zeros(3,size(trainpat,1)); d_min = 1; d_max = size(trainpat,1);
这整个for循环就是遍历(或者说穷举!)每一维数据的所有可能取值
因为输入本身就是离散数据,所以直接用每个输入值当作可能的取值
值得一提的应该是里面的while loop,还考虑了若干个输入数据去相同取值的情况
根据每个数据已知i的分类结果(+1或-1),把相应的权重加起来
这里用的分类准则应该是“误分类不纯度”,不过不确定,实在懒得动脑筋去想想(离奇!)
按说Gini Index应该是最常用的
然后把如果按照这一维的数据的这个取值分类得到的错误值,序号和分类标签,一起保存到T_MIN中(不知多少人能看懂这句话。。。。。表达的很差)
for 循环结束
for d = d_min : d_max; [DS, IX] = sort(trainpat(d,:)); TS = traintarg(IX); DiS = Distr(IX); lDS = length(DS); vPos = 0 * TS; vNeg = vPos; i = 1; j = 1; while i <= lDS k = 0; while i + k <= lDS && DS(i) == DS(i+k) if(TS(i+k) > 0) vPos(j) = vPos(j) + DiS(i+k); else vNeg(j) = vNeg(j) + DiS(i+k); end k = k + 1; end i = i + k; j = j + 1; end vNeg = vNeg(1:j-1); vPos = vPos(1:j-1); Error = zeros(1, j - 1); InvError = Error; IPos = vPos; INeg = vNeg; for i = 2 : length(IPos) IPos(i) = IPos(i-1) + vPos(i); INeg(i) = INeg(i-1) + vNeg(i); end Ntot = INeg(end); Ptot = IPos(end); for i = 1 : j - 1 Error(i) = IPos(i) + Ntot - INeg(i); InvError(i) = INeg(i) + Ptot - IPos(i); end idx_of_err_min = find(Error == min(Error)); if(length(idx_of_err_min) < 1) idx_of_err_min = 1; end if(length(idx_of_err_min) <1) idx_of_err_min = idx_of_err_min; end idx_of_err_min = idx_of_err_min(1); idx_of_inv_err_min = find(InvError == min(InvError)); if(length(idx_of_inv_err_min) < 1) idx_of_inv_err_min = 1; end idx_of_inv_err_min = idx_of_inv_err_min(1); if(Error(idx_of_err_min) < InvError(idx_of_inv_err_min)) T_MIN(1,d) = Error(idx_of_err_min); T_MIN(2,d) = idx_of_err_min; T_MIN(3,d) = -1; else T_MIN(1,d) = InvError(idx_of_inv_err_min); T_MIN(2,d) = idx_of_inv_err_min; T_MIN(3,d) = 1; end end
然后找出错误值最小的数据维度就是这个节点的划分维数,划分数值又做了点手脚:去该数据和后面数据的平均
这里才分完了第一层!!
前面说过,左节点右节点的划分值是一样的,也就是通过right_constraint和left_constraint哪个有值来判断该节点是左节点还是右节点
dim = []; best_dim = find(T_MIN(1,:) == min(T_MIN(1,:))); dim = best_dim(1); tree_node_left.dim = dim; tree_node_right.dim = dim; TDS = sort(trainpat(dim,:)); lDS = length(TDS); DS = TDS * 0; i = 1; j = 1; while i <= lDS k = 0; while i + k <= lDS && TDS(i) == TDS(i+k) DS(j) = TDS(i); k = k + 1; end i = i + k; j = j + 1; end DS = DS(1:j-1); split = (DS(T_MIN(2,dim)) + DS(min(T_MIN(2,dim) + 1, length(DS)))) / 2; split_error = T_MIN(1,dim); tree_node_left.right_constrain = split; tree_node_right.left_constrain = split;