Пример #1
0
RegressionTreeNode* RegressionTree::buildTree(const RegressionData &trainingData,RegressionTreeNode *parent,Vector< UINT > features,UINT nodeID){
    
    const UINT M = trainingData.getNumSamples();
    const UINT N = trainingData.getNumInputDimensions();
    const UINT T = trainingData.getNumTargetDimensions();
    VectorFloat regressionData(T);
    
    //Update the nodeID
    
    //Get the depth
    UINT depth = 0;
    
    if( parent != NULL )
        depth = parent->getDepth() + 1;
    
    //If there are no training data then return NULL
    if( trainingData.getNumSamples() == 0 )
        return NULL;
    
    //Create the new node
    RegressionTreeNode *node = new RegressionTreeNode;
    
    if( node == NULL )
        return NULL;
    
    //Set the parent
    node->initNode( parent, depth, nodeID );
    
    //If there are no features left then create a leaf node and return
    if( features.size() == 0 || M < minNumSamplesPerNode || depth >= maxDepth ){
        
        //Flag that this is a leaf node
        node->setIsLeafNode( true );
        
        //Compute the regression data that will be stored at this node
        computeNodeRegressionData( trainingData, regressionData );
        
        //Set the node
        node->set( trainingData.getNumSamples(), 0, 0, regressionData );
        
        Regressifier::trainingLog << "Reached leaf node. Depth: " << depth << " NumSamples: " << trainingData.getNumSamples() << std::endl;
        
        return node;
    }
    
    //Compute the best spilt point
    UINT featureIndex = 0;
    Float threshold = 0;
    Float minError = 0;
    if( !computeBestSpilt( trainingData, features, featureIndex, threshold, minError ) ){
        delete node;
        return NULL;
    }
    
    Regressifier::trainingLog << "Depth: " << depth << " FeatureIndex: " << featureIndex << " Threshold: " << threshold << " MinError: " << minError << std::endl;
    
    //If the minError is below the minRMSError then create a leaf node and return
    if( minError <= minRMSErrorPerNode ){
        //Compute the regression data that will be stored at this node
        computeNodeRegressionData( trainingData, regressionData );
        
        //Set the node
        node->set( trainingData.getNumSamples(), featureIndex, threshold, regressionData );
        
        Regressifier::trainingLog << "Reached leaf node. Depth: " << depth << " NumSamples: " << M << std::endl;
        
        return node;
    }
    
    //Set the node
    node->set( trainingData.getNumSamples(), featureIndex, threshold, regressionData );
    
    //Remove the selected feature so we will not use it again
    if( removeFeaturesAtEachSpilt ){
        for(UINT i=0; i<features.getSize(); i++){
            if( features[i] == featureIndex ){
                features.erase( features.begin()+i );
                break;
            }
        }
    }
    
    //Split the data
    RegressionData lhs(N,T);
    RegressionData rhs(N,T);
    
    for(UINT i=0; i<M; i++){
        if( node->predict( trainingData[i].getInputVector() ) ){
            rhs.addSample(trainingData[i].getInputVector(), trainingData[i].getTargetVector());
        }else lhs.addSample(trainingData[i].getInputVector(), trainingData[i].getTargetVector());
    }
    
    //Run the recursive tree building on the children
    node->setLeftChild( buildTree( lhs, node, features, nodeID ) );
    node->setRightChild( buildTree( rhs, node, features, nodeID ) );
    
    return node;
}
Пример #2
0
SEXP regression_Laplace(SEXP Rlocations, SEXP Robservations, SEXP Rmesh, SEXP Rorder, SEXP Rlambda,
				   SEXP Rcovariates, SEXP RBCIndices, SEXP RBCValues, SEXP DOF, SEXP RGCVmethod, SEXP Rnrealizations, SEXP RRNGstate, SEXP Rsolver, SEXP Rnprocessors, SEXP Rhosts)
{
    //Set data
	RegressionData regressionData(Rlocations, Robservations, Rorder, Rlambda, Rcovariates, RBCIndices, RBCValues, DOF, RGCVmethod, Rnrealizations, RRNGstate, Rsolver, Rnprocessors, Rhosts);

	//std::cout<< "Data loaded"<<std::endl;
	SEXP result = NILSXP;

    if(regressionData.getOrder()==1)
    {
		MeshHandler<1> mesh(Rmesh);
		//std::cout<< "Mesh loaded"<<std::endl;
		MixedFERegression<RegressionData, IntegratorTriangleP2,1> regression(mesh,regressionData);

		regression.smoothLaplace();

		const std::vector<VectorXr>& solution = regression.getSolution();
		const std::vector<Real>& dof = regression.getDOF();
		const std::vector<Real>& var = regression.getVar();

		//Copy result in R memory
		result = PROTECT(Rf_allocVector(VECSXP, 4));
		SET_VECTOR_ELT(result, 0, Rf_allocMatrix(REALSXP, solution[0].size(), solution.size()));
		SET_VECTOR_ELT(result, 1, Rf_allocVector(REALSXP, solution.size()));
		SET_VECTOR_ELT(result, 2, Rf_allocVector(REALSXP, solution.size()));
		SET_VECTOR_ELT(result, 3, Rf_allocVector(STRSXP, 1));

		Real *rans = REAL(VECTOR_ELT(result, 0));
		for(UInt j = 0; j < solution.size(); j++)
		{
			for(UInt i = 0; i < solution[0].size(); i++)
				rans[i + solution[0].size()*j] = solution[j][i];
		}

		Real *rans2 = REAL(VECTOR_ELT(result, 1));
		for(UInt i = 0; i < solution.size(); i++)
		{
			rans2[i] = dof[i];
		}

		Real *rans3 = REAL(VECTOR_ELT(result, 2));
		for(UInt i = 0; i < solution.size(); i++)
		{
			rans3[i] = var[i];
		}

		std::string RNGstate = regression.getFinalRNGstate();
		SET_STRING_ELT(VECTOR_ELT(result, 3), 0, Rf_mkChar(RNGstate.c_str()));

		UNPROTECT(1);

    }
	else if(regressionData.getOrder()==2)
	{
		MeshHandler<2> mesh(Rmesh);
		//std::cout<< "Mesh loaded"<<std::endl;
		MixedFERegression<RegressionData, IntegratorTriangleP4,2> regression(mesh,regressionData);

		regression.smoothLaplace();

		const std::vector<VectorXr>& solution = regression.getSolution();
		const std::vector<Real>& dof = regression.getDOF();
		const std::vector<Real>& var = regression.getVar();

		//Copy result in R memory
		result = PROTECT(Rf_allocVector(VECSXP, 4));
		SET_VECTOR_ELT(result, 0, Rf_allocMatrix(REALSXP, solution[0].size(), solution.size()));
		SET_VECTOR_ELT(result, 1, Rf_allocVector(REALSXP, solution.size()));
		SET_VECTOR_ELT(result, 2, Rf_allocVector(REALSXP, solution.size()));
		SET_VECTOR_ELT(result, 3, Rf_allocVector(STRSXP, 1));

		Real *rans = REAL(VECTOR_ELT(result, 0));
		for(UInt j = 0; j < solution.size(); j++)
		{
			for(UInt i = 0; i < solution[0].size(); i++)
				rans[i + solution[0].size()*j] = solution[j][i];
		}

		Real *rans2 = REAL(VECTOR_ELT(result, 1));
		for(UInt i = 0; i < solution.size(); i++)
		{
			rans2[i] = dof[i];
		}

		Real *rans3 = REAL(VECTOR_ELT(result, 2));
		for(UInt i = 0; i < solution.size(); i++)
		{
			rans3[i] = var[i];
		}

		std::string RNGstate = regression.getFinalRNGstate();
		SET_STRING_ELT(VECTOR_ELT(result, 3), 0, Rf_mkChar(RNGstate.c_str()));

		UNPROTECT(1);

    }

	return(result);
}
Пример #3
0
SEXP regression_Laplace(SEXP Rlocations, SEXP Robservations, SEXP Rmesh, SEXP Rorder, SEXP Rlambda,
				   SEXP Rcovariates, SEXP RBCIndices, SEXP RBCValues, SEXP DOF)
{
    //Set data
	RegressionData regressionData(Rlocations, Robservations, Rorder, Rlambda, Rcovariates, RBCIndices, RBCValues, DOF);

	//std::cout<< "Data loaded"<<std::endl;
	SEXP result;

    if(regressionData.getOrder()==1)
    {
		MeshHandler<1> mesh(Rmesh);
		//std::cout<< "Mesh loaded"<<std::endl;
		MixedFERegression<RegressionData, IntegratorTriangleP2,1> regression(mesh,regressionData);

		regression.smoothLaplace();

		const std::vector<VectorXr>& solution = regression.getSolution();
		const std::vector<Real>& dof = regression.getDOF();
		//Copy result in R memory
		result = PROTECT(allocVector(VECSXP, 2));
		SET_VECTOR_ELT(result, 0, allocMatrix(REALSXP, solution[0].size(), solution.size()));
		SET_VECTOR_ELT(result, 1, allocVector(REALSXP, solution.size()));

		Real *rans = REAL(VECTOR_ELT(result, 0));
		for(int j = 0; j < solution.size(); j++)
		{
			for(int i = 0; i < solution[0].size(); i++)
				rans[i + solution[0].size()*j] = solution[j][i];
		}

		Real *rans2 = REAL(VECTOR_ELT(result, 1));
		for(int i = 0; i < solution.size(); i++)
		{
			rans2[i] = dof[i];
		}
		UNPROTECT(1);

    }
	else if(regressionData.getOrder()==2)
	{
		MeshHandler<2> mesh(Rmesh);
		//std::cout<< "Mesh loaded"<<std::endl;
		MixedFERegression<RegressionData, IntegratorTriangleP4,2> regression(mesh,regressionData);

		regression.smoothLaplace();

		const std::vector<VectorXr>& solution = regression.getSolution();
		const std::vector<Real>& dof = regression.getDOF();
		//Copy result in R memory
		result = PROTECT(allocVector(VECSXP, 2));
		SET_VECTOR_ELT(result, 0, allocMatrix(REALSXP, solution[0].size(), solution.size()));
		SET_VECTOR_ELT(result, 1, allocVector(REALSXP, solution.size()));

		Real *rans = REAL(VECTOR_ELT(result, 0));
		for(int j = 0; j < solution.size(); j++)
		{
			for(int i = 0; i < solution[0].size(); i++)
				rans[i + solution[0].size()*j] = solution[j][i];
		}

		Real *rans2 = REAL(VECTOR_ELT(result, 1));
		for(int i = 0; i < solution.size(); i++)
		{
			rans2[i] = dof[i];
		}
		UNPROTECT(1);
    }

	return(result);
}