bool Rank::train (ISamples &samples, ssi_size_t stream_index) { if (!_model) { ssi_wrn ("a model has not been set yet"); return false; } release (); _n_scores = samples.getStream (stream_index).dim; _scores = new score[_n_scores]; Evaluation eval; Trainer trainer (_model, stream_index); SSI_DBG (SSI_LOG_LEVEL_DEBUG, "evaluate dimensions:"); for (ssi_size_t ndim = 0; ndim < _n_scores; ndim++) { ISSelectDim samples_s (&samples); samples_s.setSelection (stream_index, 1, &ndim); if (_options.loo) { eval.evalLOO (&trainer, samples_s); } else if (_options.louo) { eval.evalLOUO (&trainer, samples_s); } else { eval.evalKFold (&trainer, samples_s, _options.kfold); } _scores[ndim].index = ndim; _scores[ndim].value = eval.get_classwise_prob (); SSI_DBG (SSI_LOG_LEVEL_DEBUG, " #%02u -> %.2f", _scores[ndim].index, _scores[ndim].value); } trainer.release (); return true; }
bool ex_eval(void *arg) { ssi_size_t n_classes = 2; ssi_size_t n_samples = 20; ssi_size_t n_streams = 1; ssi_real_t train_distr[][3] = { 0.3f, 0.3f, 0.2f, 0.3f, 0.6f, 0.2f, 0.6f, 0.3f, 0.2f, 0.6f, 0.6f, 0.2f }; ssi_real_t test_distr[][3] = { 0.5f, 0.5f, 0.5f }; SampleList samples; ModelTools::CreateTestSamples (samples, n_classes, n_samples, n_streams, train_distr); ssi_char_t string[SSI_MAX_CHAR]; for (ssi_size_t n_class = 1; n_class < n_classes; n_class++) { ssi_sprint (string, "class%02d", n_class); samples.addClassName (string); } Evaluation eval; NaiveBayes *model = ssi_create (NaiveBayes, 0, true); Trainer trainer (model); trainer.train (samples); Evaluation2Latex e2latex; e2latex.open ("eval.tex"); ssi_print_off ("devel set:\n"); eval.eval (&trainer, samples); eval.print (ssiout); eval.print_result_vec (); e2latex.writeHead (eval, "caption", "label"); e2latex.writeText ("results with different evaluation strategies", true); e2latex.writeEval ("devel", eval); ssi_print_off("k-fold:\n"); eval.evalKFold (&trainer, samples, 3); eval.print (); eval.print_result_vec (); e2latex.writeEval ("k-fold", eval); ssi_print_off("split:\n"); eval.evalSplit (&trainer, samples, 0.5f); eval.print (); eval.print_result_vec (); e2latex.writeEval ("split", eval); ssi_print_off("loo:\n"); eval.evalLOO (&trainer, samples); eval.print (); eval.print_result_vec (); e2latex.writeEval ("loo", eval); e2latex.writeTail (); e2latex.close (); FILE *fp = fopen("eval.csv", "w"); eval.print(fp, Evaluation::PRINT::CSV_EX); fclose(fp); return true; }