Example #1
0
File: fitModel.C Project: anhi/ball
		Eigen::VectorXd FitModel::predict(const vector<double> & substance, bool transform)
		{
			if (training_result_.cols() == 0)
			{
				throw Exception::InconsistentUsage(__FILE__, __LINE__, "Model must be trained before it can predict the activitiy of substances!"); 
			}
			Eigen::VectorXd v = getSubstanceVector(substance, transform); 
			Eigen::VectorXd res(Y_.cols());
			
			String var="";
			// replace all x-values for the current substance
			for (unsigned int j = 0; j < v.rows(); j++)
			{
				var = var+"x"+String(j)+"="+String(v(j))+";";
			}
			
			//calculated all activities for given substance
			for (int c = 0; c < Y_.cols(); c++)
			{
				String coeff="";
				// get optimized coefficients
				for (int m = 0; m < training_result_.rows(); m++)
				{
					coeff = coeff+"b"+String(m)+"="+String(training_result_(m, c))+";";
				}
				ParsedFunction<float> f = coeff+var+allEquations_[c];
				res(c) = f(0);
			}
			
			if (transform && y_transformations_.cols() != 0)
			{
				backTransformPrediction(res); 
			}
			return res;	
		}
Example #2
0
Eigen::VectorXd ALLModel::predict(const vector<double> & substance, bool transform)
{
    if (descriptor_matrix_.cols() == 0)
    {
        throw Exception::InconsistentUsage(__FILE__, __LINE__, "Training data must be read into the ALL-model before the activity of a substance can be predicted!");
    }
    Eigen::VectorXd v0 = getSubstanceVector(substance, transform);

    Eigen::MatrixXd v(1, v0.rows());
    v.row(0) = v0;
    Eigen::MatrixXd dist;

    // calculate distances between the given substance and the substances of X
    // dimension of dist: 1xn
    calculateEuclDistanceMatrix(v, descriptor_matrix_, dist);
    Eigen::VectorXd w;
    calculateWeights(dist, w);

    Eigen::MatrixXd XX;

    // calculate X.t()*X as cross-products weighted by the similarity of the given substance to the respective row of X
    calculateXX(w, XX);

    Eigen::MatrixXd XY;

    // calculate X.t()*Y_ as cross-products weighted by the similarity of the given substance to the respective row of X
    calculateXY(w, XY);

    // rigde regression in order to be able to cope with linearly dependent columns, i.e. singular matrices
    Eigen::MatrixXd im(XX.rows(), XX.rows());
    im.setIdentity();
    im *= lambda_;
    XX += im;

    Eigen::MatrixXd train = XX.colPivHouseholderQr().solve(XY);

    Eigen::VectorXd res(Y_.cols());
    res = v0.transpose()*train;

    if (transform && y_transformations_.cols() != 0)
    {
        backTransformPrediction(res);
    }

    return res;
}
Example #3
0
		Eigen::VectorXd LinearModel::predict(const vector<double> & substance, bool transform)
		{
			if (training_result_.rows() == 0)
			{
				throw Exception::InconsistentUsage(__FILE__, __LINE__, "Model must be trained before it can predict the activitiy of substances!"); 
			}

			Eigen::VectorXd v = getSubstanceVector(substance, transform); 

			Eigen::VectorXd res = v.transpose()*training_result_;
			//if (offsets_.getSize() == res.getSize()) res -= offsets_; 

			if (transform && y_transformations_.cols() != 0)
			{
				backTransformPrediction(res); 
			}
			
			return res;
		}