predict on (subset) of training data
| 172 | |
| 173 | // predict on (subset) of training data |
| 174 | void SimpleSVM::predict(vector<Prediction>& predictions, vector<Size> indexes) const |
| 175 | { |
| 176 | if (model_ == nullptr) |
| 177 | { |
| 178 | throw Exception::Precondition(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, |
| 179 | "SVM model has not been trained (use the " |
| 180 | "'setup' method)"); |
| 181 | } |
| 182 | |
| 183 | Size n_obs = nodes_.size(); |
| 184 | if (indexes.empty()) |
| 185 | { |
| 186 | indexes.reserve(n_obs); |
| 187 | for (Size i = 0; i < n_obs; indexes.push_back(i++)){}; |
| 188 | } |
| 189 | Size n_classes = svm_get_nr_class(model_); |
| 190 | vector<int> outcomes(n_classes); |
| 191 | svm_get_labels(model_, &(outcomes[0])); |
| 192 | vector<double> probabilities(n_classes); |
| 193 | predictions.clear(); |
| 194 | predictions.reserve(indexes.size()); |
| 195 | for (vector<Size>::iterator it = indexes.begin(); it != indexes.end(); ++it) |
| 196 | { |
| 197 | if (*it >= n_obs) |
| 198 | { |
| 199 | String msg = "Invalid index for prediction; there are only " + |
| 200 | String(n_obs) + " observations."; |
| 201 | throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, |
| 202 | msg, String(*it)); |
| 203 | } |
| 204 | Prediction pred; |
| 205 | pred.outcome = svm_predict_probability(model_, &(nodes_[*it][0]), |
| 206 | &(probabilities[0])); |
| 207 | for (Size i = 0; i < n_classes; ++i) |
| 208 | { |
| 209 | pred.probabilities[outcomes[i]] = probabilities[i]; |
| 210 | } |
| 211 | predictions.push_back(pred); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | void scaleDataUsingTrainingRanges(SimpleSVM::PredictorMap& predictors, const map<String, pair<double, double>>& scaling) |
| 216 | { |