示例#1
0
double find_branch(exPtrs &p, Tree *t, Appear &app) {
	double minTot = 10.0, minThr;
	int minBra = -1;
	for (Appear::IT it = app.B; it != app.E; it++) {
		int i = *it; // i-th feature for branching
		Comp comp(i);
		sort(p.B, p.E, comp);
		Unique u; // u stores Vm's
		for (int j = 0; j < p.size(); j++)
			u.insert(p[j]->feat[i]);

		int yl = 0, nl = 0, yr = Y(p), nr = N(p);
		exPtrs::IT k = p.B;
		Unique::IT j = u.B, prev = j;
		j++;
		for ( ; j != u.E; j++) { // j-th threshold
			double mid = ( *prev + *j ) / 2;
			prev = j;
			for ( ; k != p.E && (*k)->feat[i] < mid; k++) {
				if ((*k)->ans > 0) yl++, yr--;
				else			   nl++, nr--;
			}
			
			double curTot = total(yl, nl, yr, nr);
			if (curTot < minTot) {
				minTot = curTot;
				minBra = i;
				minThr = mid;
			}
		}
	}
	
	t->branch = minBra;
	t->thresh = minThr;
	return minBra;
}