Eigen::VectorXd LDAModel::predict(const vector<double> & substance, bool transform) { if (sigma_.cols() == 0) { throw Exception::InconsistentUsage(__FILE__, __LINE__, "Model must be trained before it can predict the activitiy of substances!"); } // search class to which the given substance has the smallest distance Eigen::VectorXd s = getSubstanceVector(substance, transform); // dim: 1xm int min_k = 0; double min_dist = 99999999; Eigen::VectorXd result(mean_vectors_.size()); for (unsigned int c = 0; c < mean_vectors_.size(); c++) { for (int k = 0; k < mean_vectors_[c].rows(); k++) { Eigen::VectorXd diff(s.rows()); for (unsigned int i = 0; i < s.rows(); i++) { diff(i) = s(i)-mean_vectors_[c](k, i); } double dist = diff.transpose()*sigma_*diff; if (dist < min_dist) { min_dist = dist; min_k = k; } } result(c) = labels_[min_k]; } return result; }
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; }
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; }
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; }