Skip to content

Instantly share code, notes, and snippets.

@Roffild
Created March 26, 2019 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Roffild/08e289db362b3349858169c849c1918c to your computer and use it in GitHub Desktop.
Save Roffild/08e289db362b3349858169c849c1918c to your computer and use it in GitHub Desktop.
Modification of a random forest with predictive significance calculations.
class CDForestUtils : public CDForest
{
protected:
static void DFProcessInternal(CDecisionForest &df,const int offs,
double &x[],double &y[],ulong &stats[])
{
//--- create variables
int k=0;
int idx=0;
//--- Set pointer to the root
k=offs+1;
//--- Navigate through the tree
while(true)
{
//--- check
if(df.m_trees[k]==-1.0)
{
//--- check
if(df.m_nclasses==1)
y[0]=y[0]+df.m_trees[k+1];
else
{
idx=(int)MathRound(df.m_trees[k+1]);
y[idx]=y[idx]+1;
}
//--- break the cycle
break;
}
//--- check
idx=(int)MathRound(df.m_trees[k]);
stats[idx]++;
if(x[idx]<df.m_trees[k+1])
k=k+m_innernodewidth;
else
k=offs+(int)MathRound(df.m_trees[k+2]);
}
}
public:
static void DFProcess(CDecisionForest &df,double &x[],double &y[],ulong &stats[])
{
//--- create variables
int offs=0;
int i=0;
double v=0;
int i_=0;
//--- Proceed
if(CAp::Len(y)<df.m_nclasses)
ArrayResizeAL(y,df.m_nclasses);
//--- initialization
offs=0;
for(i=0;i<=df.m_nclasses-1;i++)
y[i]=0;
for(i=0;i<=df.m_ntrees-1;i++)
{
//--- Process basic tree
DFProcessInternal(df,offs,x,y,stats);
//--- Next tree
offs=offs+(int)MathRound(df.m_trees[offs]);
}
//--- calculation
v=1.0/(double)df.m_ntrees;
for(i_=0;i_<=df.m_nclasses-1;i_++)
y[i_]=v*y[i_];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment