Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

sonar_tutorial_part2

Manlio Morini edited this page Jun 8, 2026 · 7 revisions

How to make predictions

Now let's backtrack a little bit. Somehow we have obtained an interesting individual, the accuracy and/or other performance measurements are promising and we would like to use it to make predictions.

Example examples/sonar02.cc explains how to proceed:

const auto predictor(s.predictor(result.best_individual));
const auto example(random::element(prob.data.selected()));
const auto prediction(predictor->tag(example.input));
std::cout << "Correct class: " << src::label(example)
 << " Prediction: " << prediction.label
 << " Sureness: " << prediction.sureness << '\n';

The ultra::src::search class provides the predictor(individual) member function that returns a functor exploitable for symbolic regression and classification tasks.

The key definition is:

const auto predictor(s.predictor(result.best_individual));

The predictor object is a std::unique_ptr<some strange object> smart pointer but this isn't very important. Rather it accepts an example (src::example) and gives the predicted class ((*predictor)(example)).

Even better it has the tag member functions:

const auto prediction(predictor->tag(example.input));

prediction is a struct containing the predicted class (prediction.label) and the sureness of the prediction (prediction.sureness which varies in the [0, 1] interval):

std::cout << "Correct class: " << src::label(example)
 << " Prediction: " << prediction.label
 << " Sureness: " << prediction.sureness << '\n';

Model serialization

The model can be made persistent:

std::stringstream ss;
src::serialize::save(ss, predictor); // save...
// ... and reload it when needed.
const auto predictor2(src::serialize::oracle::load(ss, prob.sset));
const auto prediction2(predictor2->tag(example.input));
std::cout << " Prediction: " << prediction2.label
 << " Sureness: " << prediction2.sureness << '\n';
assert(prediction2.label == prediction.label);

PROCEED TO PART 3 →

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /