⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 randomforest.cpp

📁 adaboost code in matlab
💻 CPP
📖 第 1 页 / 共 3 页
字号:
			rrd = pdo;
			rln = (float)0.;
			rld = (float)0.;
			/* build new array "wl[]"*/
			zer_array->zervr(&wl[1], nclass); 
			/* nclass is number of classes e.g. "7"*/ 
			for (j = 1; j <= *nclass; ++j) 
			{
				/* "wr[j]" get the numbers of k-th according to class. e.g. class 1 ->34, class 2 -> 437, class 3 -> 700 */
				wr[j] = tclasspop[j];
			}
			critvar = (float)-1e20;
			/*nsp -> 1 to (k-1) */
			for (nsp = *ndstart; nsp <= (*ndend - 1); ++nsp) 
			{
				nc = a[mvar + nsp * a_dim1];
				u = win[nc];
				k = cl[nc];
				rln += u * (wl[k] * 2 + u);
				rrn += u * (wr[k] * -2 + u);
				rld += u;
				rrd -= u;
				wl[k] += u;
				wr[k] -= u;
				if (b[mvar + nc * b_dim1] < b[mvar + a[mvar + (nsp + 1) *a_dim1] * b_dim1]) 
				{
					if (__min(rrd,rld) > (float)1e-5) 
					{
						crit = rln / rld + rrn / rrd;
						if (crit > critvar) 
						{
							nbestvar = nsp;
							critvar = crit;
						}
					}
				}
			}
			if (critvar > critmax) 
			{
				*msplit = mvar;
				*nbest = nbestvar;
				critmax = critvar;
			}
		} 
    }
    *decsplit = critmax - crit0;
    if (critmax < (float)-1e10) 
	{
		*jstat = 1;
    }
	delete zer_array;
	return 0;
} /* end findbestsplit */


int randomForest::movedata(int a[],int ta[],int *mdim,int *nsample,int *ndstart,int *ndend,
						   int idmove[],int ncase[],int *msplit,int cat[],int *nbest,int *ndendl)
{
    /* System generated locals */
    int a_dim1, a_offset;
	
    /* Local variables */
    static int icat[32];
    extern int myunpack();
    static int k, l, n, nc, ih, ndo, msh, nsp;
	
	/* compute idmove=indicator of case nos. going left */
    /* Parameter adjustments */
    --cat;
    --ncase;
    --idmove;
    --ta;
    a_dim1 = *mdim;
    a_offset = a_dim1 + 1;
    a -= a_offset;
	
    /* Function Body */
    if (cat[*msplit] == 1) 
	{
		for (nsp = *ndstart; nsp <= *nbest; ++nsp) 
		{
			nc = a[*msplit + nsp * a_dim1];
			idmove[nc] = 1;
		}
		for (nsp = *nbest + 1; nsp <= *ndend; ++nsp) 
		{
			nc = a[*msplit + nsp * a_dim1];
			idmove[nc] = 0;
		}
		*ndendl = *nbest;
    } 
	/* shift case. nos. right and left for numerical variables. */
    for (msh = 1; msh <= *mdim; ++msh) 
	{
		if (cat[msh] == 1) 
		{
			k = *ndstart - 1;
			for (n = *ndstart; n <= *ndend; ++n) 
			{
				ih = a[msh + n * a_dim1];
				if (idmove[ih] == 1) 
				{
					++k;
					ta[k] = a[msh + n * a_dim1];
				}
			}
			for (n = *ndstart; n <= *ndend; ++n) 
			{
				ih = a[msh + n * a_dim1];
				if (idmove[ih] == 0) 
				{
					++k;
					ta[k] = a[msh + n * a_dim1];
				}
			}
			for (k = *ndstart; k <= *ndend; ++k) 
			{
				a[msh + k * a_dim1] = ta[k];
			}
		}
    }
    ndo = 0;
    if (ndo == 1) 
	{
		for (msh = 1; msh <= *mdim; ++msh) 
		{
			if (cat[msh] > 1) 
			{
				k = *ndstart - 1;
				for (n = *ndstart; n <= *ndend; ++n) 
				{
					ih = ncase[n];
					if (idmove[ih] == 1) 
					{
						++k;
						ta[k] = a[msh + ih * a_dim1];
					}
				}
				for (n = *ndstart; n <= *ndend; ++n)
				{
					ih = ncase[n];
					if (idmove[ih] == 0) 
					{
						++k;
						ta[k] = a[msh + ih * a_dim1];
					}
				}
				for (k = *ndstart; k <= *ndend; ++k)
				{
					a[msh + k * a_dim1] = ta[k];
				}
			}
		}
    }
	/* compute case nos. for right and left nodes. */
    if (cat[*msplit] == 1) 
	{
		for (n = *ndstart; n <= *ndend; ++n) 
		{
			ncase[n] = a[*msplit + n * a_dim1];
		}
    } 
	else 
	{
		k = *ndstart - 1;
		for (n = *ndstart; n <= *ndend; ++n)
		{
			if (idmove[ncase[n]] == 1) 
			{
				++k;
				ta[k] = ncase[n];
			}
		}
		for (n = *ndstart; n <= *ndend; ++n) 
		{
			if (idmove[ncase[n]] == 0) 
			{
				++k;
				ta[k] = ncase[n];
			}
		}
		for (k = *ndstart; k <= *ndend; ++k) 
		{
			ncase[k] = ta[k];
		}
    }
	return 0;
} /* end movedata */


int randomForest::xtranslate(float x[],int *mdim,int *nrnodes,int *nsample,int bestvar[],
							 int bestsplit[],int bestsplitnext[],float xbestsplit[],
							 int nodestatus[],int cat[],int *ndbigtree)
{
    /* System generated locals */
    int x_dim1, x_offset;
	
    /* Local variables */
    static int k, m;
	
    /* Parameter adjustments */
    --cat;
    --nodestatus;
    --xbestsplit;
    --bestsplitnext;
    --bestsplit;
    --bestvar;
    x_dim1 = *mdim;
    x_offset = x_dim1 + 1;
    x -= x_offset;
	
    /* Function Body */
    for (k = 1; k <= *ndbigtree; ++k) 
	{
		if (nodestatus[k] == 1) 
		{
			m = bestvar[k];
			if (cat[m] == 1) 
			{
				xbestsplit[k] = (x[m + bestsplit[k] * x_dim1] + x[m + bestsplitnext[k] * x_dim1]) / 2;
			} 
			else 
			{
				xbestsplit[k] = (float) bestsplit[k];
			}
		}
		/* L10: */
    }
	return 0;
} /* end xtranslate */

int randomForest::testreebag(float *xts,int *nts,int *mdim,int *treemap,int nodestatus[],
							 float xbestsplit[],int *cbestsplit,int bestvar[],int nodeclass[],
							 int *nrnodes,int *ndbigtree,int cat[],int *nclass,int jts[],
							 int nodex[],int *maxcat)
{
    /* System generated locals */
    int xts_dim1, xts_offset, cbestsplit_dim1, cbestsplit_offset;
	
    /* Builtin functions */
    int i_nint();
	
    /* Local variables */
    static int icat[32], jcat, ncat;
    extern int zerv(), myunpack();
    static int j, k, l, m, n, kt;
	
    /* Parameter adjustments */
    --nodex;
    --jts;
    --cat;
    xts_dim1 = *mdim;
    xts_offset = xts_dim1 + 1;
    xts -= xts_offset;
    --nodeclass;
    --bestvar;
    --xbestsplit;
    --nodestatus;
    treemap -= 3;
    cbestsplit_dim1 = *maxcat;
    cbestsplit_offset = cbestsplit_dim1 + 1;
    cbestsplit -= cbestsplit_offset;
	
    /* Function Body */
	randomForest *zer_array = new randomForest(1,1);
	zer_array->zerv(&jts[1], nts);
    zer_array->zerv(&nodex[1], nts);
    for (k = 1; k <= *ndbigtree; ++k) 
	{
		if (bestvar[k] > 0) 
		{
			l = cat[bestvar[k]];
		}
    }
    for (n = 1; n <= *nts; ++n) 
	{
		kt = 1;
		for (k = 1; k <= *ndbigtree; ++k) 
		{
			if (nodestatus[kt] == -1) 
			{
				jts[n] = nodeclass[kt];
				nodex[n] = kt;
				goto L100;
			}
			m = bestvar[kt];
			if (cat[m] == 1) 
			{
				if (xts[m + n * xts_dim1] <= xbestsplit[kt]) 
				{
					kt = treemap[(kt << 1) + 1];
				} 
				else 
				{
					kt = treemap[(kt << 1) + 2];
				}
			} 
		}
L100:;
    }
	delete zer_array;
	return 0;
} /* end testreebag */

int randomForest::testreebagts(float *xts,int *nts,int *mdim,int *treemap,int nodestatus[],
							 float xbestsplit[],int *cbestsplit,int bestvar[],int nodeclass[],
							 int *nrnodes,int *ndbigtree,int cat[],int *nclass,int jts[],
							 int nodex[],int *maxcat)
{
    /* System generated locals */
    int xts_dim1, xts_offset, cbestsplit_dim1, cbestsplit_offset;
	
    /* Builtin functions */
    int i_nint();
	
    /* Local variables */
    static int icat[32], jcat, ncat;
    extern int zerv(), myunpack();
    static int j, k, l, m, n, kt;
	
    /* Parameter adjustments */
    --nodex;
    --jts;
    --cat;
    xts_dim1 = *mdim;
    xts_offset = xts_dim1 + 1;
    xts -= xts_offset;
    --nodeclass;
    --bestvar;
    --xbestsplit;
    --nodestatus;
    treemap -= 3;
    cbestsplit_dim1 = *maxcat;
    cbestsplit_offset = cbestsplit_dim1 + 1;
    cbestsplit -= cbestsplit_offset;
	
    /* Function Body */
	randomForest *zer_array = new randomForest(1,1);
	zer_array->zerv_1(&jts[1], nts);
    zer_array->zerv(&nodex[1], nts);
    for (k = 1; k <= *ndbigtree; ++k) 
	{
		if (bestvar[k] > 0) 
		{
			l = cat[bestvar[k]];
		}
    }
    for (n = 1; n <= *nts; ++n) 
	{
		kt = 1;
		for (k = 1; k <= *ndbigtree; ++k) 
		{
			if (nodestatus[kt] == -1) 
			{
				jts[n] = nodeclass[kt];
				nodex[n] = kt;
				goto L100;
			}
			m = bestvar[kt];
			if (cat[m] == 1) 
			{
				if (xts[m + n * xts_dim1] <= xbestsplit[kt]) 
				{
					kt = treemap[(kt << 1) + 1];
				} 
				else 
				{
					kt = treemap[(kt << 1) + 2];
				}
			} 
		}
L100:;
    }
	delete zer_array;
	return 0;
} /* end testreebagts */

int randomForest::comptserr(float countts[],int jts[],int clts[],int jet[],int *ntest,int *nclass,
							float *errts,float pid[],int *labelts,int hypothesis_test[],int label_test_array[])
{
    /* System generated locals */
    int countts_dim1, countts_offset;
	
    /* Local variables */
    static float cmax;
    static int j, n;
    static float rmissts;
	
    /* Parameter adjustments */
    --jet;
    --clts;
    --jts;
    --pid;
    countts_dim1 = *nclass;
    countts_offset = countts_dim1 + 1;
    countts -= countts_offset;
    /************ test for test file **********************/
	for (int kk=0; kk <= *ntest; ++kk) 
	{
		if (label_test_array[kk] > 3){
			clts[kk]=1;
		}
		else
		{
			clts[kk]=0;
		}
	}
    /*
	for (int kkk=0; kkk <= *ntest; ++kkk) 
	{
		if (jts[kkk] == 2 ){
			jts[kkk]=0;
		}
		else
		{
			jts[kkk]=1;
		}
	}
	/**************************************************************/
	rmissts = (float)0.;
    for (n = 1; n <= *ntest; ++n) 
	{
		++countts[jts[n] + n * countts_dim1];
    }
    if (*labelts == 1) 
	{
		for (n = 1; n <= *ntest; ++n) 
		{
			cmax = (float)0.;
			for (j = 1; j <= *nclass; ++j) 
			{
				if (countts[j + n * countts_dim1] > cmax) 
				{
					jet[n] = j;
					cmax = countts[j + n * countts_dim1];
				    //hypothesis_test[n]=0;
				}
			}
			if (jet[n]!= clts[n]) 
			{
				    rmissts += 1;
					//hypothesis_test[n]=1;
			}
			/*
			if (jet[n] == clts[n]) 
			{
					hypothesis_test[n]=0;
			}
			*/
		}
		*errts = rmissts / *ntest;
    }
	return 0;
} /* end comptserr */


int randomForest::oob(int *nsample,int *nclass,int jin[],int cl[],int jtr[],int jerr[],
					  int counttr[],int out[],float *errtr,float *errc,float rmargin[],float *q,
					  int jest[],float wtt[],int hypothesis_train[])
{
    /* System generated locals */
    int counttr_dim1, counttr_offset, q_dim1, q_offset;
    float r__1;
	
    /* Local variables */
    static float smax, outc;
    extern /* Subroutine */ int zerv();
    static int j, n;
    static float rmiss;
    static int inderr;
    static float rmissc, smaxtr, pth;
	
    /* Parameter adjustments */
    --wtt;
    --jest;
    --rmargin;
    --out;
    --jerr;
    --jtr;
    --cl;
    --jin;
	--hypothesis_train;
    q_dim1 = *nclass;
    q_offset = q_dim1 + 1;
    q -= q_offset;
    counttr_dim1 = *nclass;
    counttr_offset = counttr_dim1 + 1;
    counttr -= counttr_offset;
	randomForest *zer_array = new randomForest(1,1);
	zer_array->zerv(hypothesis_train, nsample);
    /* Function Body */
    inderr = 1;
    if (inderr == 1) {
		outc = (float)0.;
		rmissc = (float)0.;
		for (n = 1; n <= *nsample; ++n) 
		{
			if (jin[n] == 0) 
			{
				outc += (float)1.;
				if (jtr[n] != cl[n]) 
				{
					rmissc += (float)1.;
					hypothesis_train[n]=1;
					if (jtr[n] == 2){
						hypothesis_train[n]=0;
					}
				}
			}
			if (hypothesis_train[n] != 1)
			{
				hypothesis_train[n]=0;
				if (jtr[n] == 1){
					hypothesis_train[n]=1;				
				}
			}	
		}
		*errc = rmissc * 100 / outc;
    }
	zer_array->zerv(&jerr[1], nsample);
    rmiss = (float)0.;
    for (n = 1; n <= *nsample; ++n) 
	{
		if (out[n] > 0) 
		{
			smax = (float)0.;
			smaxtr = (float)0.;
			for (j = 1; j <= *nclass; ++j) 
			{
				q[j + n * q_dim1] = (float) counttr[j + n * counttr_dim1] / out[n];
				if (j != cl[n]) 
				{
					/* Computing MAX */
					r__1 = q[j + n * q_dim1];
					smax = __max(r__1,smax);
				}
				if (q[j + n * q_dim1] > smaxtr) 
				{
					smaxtr = q[j + n * q_dim1];
					jest[n] = j;
				}
			}
			if (jest[n] != cl[n])    // && (jest[n] != 0))
			{
				rmiss += (float)1.;
				jerr[n] = 1;
				/*
				hypothesis[n]=1;
				if (jest[n] == 2){
				hypothesis[n]=0;
				}
				*/
			}
			/*
			if (hypothesis[n] != 1)
			{
			hypothesis[n]=0;
			if (jest[n] == 1){
			hypothesis[n]=1;
			}
			}
			*/
			pth = q[cl[n] + n * q_dim1];
			rmargin[n] = pth - smax;
		}
    }
    *errtr = rmiss / *nsample;
	delete zer_array;
	return 0;
} /* oob */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -