void AlgLibTools::Samples2MatrixWithClass (ISamples &samples, ssi_size_t stream_id, ae_matrix* m) { ae_int_t nfeatures = samples.get (0)->streams[stream_id]->dim; ae_int_t nsamples = samples.getSize (); ae_int_t i = 0; ae_int_t j = 0; ae_state state; ae_matrix_clear(m); ae_matrix_set_length(m, nsamples, nfeatures+1, &state); ssi_sample_t *sample; samples.reset (); while (sample = samples.next ()) { ssi_real_t *ptr = ssi_pcast (ssi_real_t, sample->streams[stream_id]->ptr); for (j = 0; j <= nfeatures-1; j++) { m->ptr.pp_double[i][j] = ssi_cast (double, *ptr++); } m->ptr.pp_double[i][j] = ssi_cast (double, sample->class_id); i++; } //delete sample; }
void ISNorm::norm (ISamples &samples) { samples.reset (); ssi_sample_t *sample = 0; while (sample = samples.next ()) { norm (*sample); } }
bool FileSamplesOut::write (ISamples &data) { data.reset (); ssi_sample_t *sample = 0; while (sample = data.next ()) { write (*sample); } return true; }
void Evaluation::eval (IFusion &fusion, ssi_size_t n_models, IModel **models, ISamples &samples) { // init confussion matrix _trainer = 0; destroy_conf_mat (); init_conf_mat (samples); ssi_size_t n_classes = samples.getClassSize (); ssi_real_t *probs = new ssi_real_t[n_classes]; _n_total = samples.getSize (); _result_vec = new ssi_size_t[2*_n_total]; _result_vec_ptr = _result_vec; samples.reset (); const ssi_sample_t *sample = 0; while (sample = samples.next ()) { ssi_size_t real_index = sample->class_id; *_result_vec_ptr++ = real_index; if (fusion.forward (n_models, models, sample->num, sample->streams, n_classes, probs)) { ssi_size_t max_ind = 0; ssi_real_t max_val = probs[0]; for (ssi_size_t i = 1; i < n_classes; i++) { if (probs[i] > max_val) { max_val = probs[i]; max_ind = i; } } *_result_vec_ptr++ = max_ind; _conf_mat_ptr[real_index][max_ind]++; _n_classified++; } else if (!_allow_unclassified) { ssi_size_t max_ind = _default_class_id; *_result_vec_ptr++ = max_ind; _conf_mat_ptr[real_index][max_ind]++; _n_classified++; } else { *_result_vec_ptr++ = SSI_ISAMPLES_GARBAGE_CLASS_ID; _n_unclassified++; } } delete[] probs; }
bool MyModel::train (ISamples &samples, ssi_size_t stream_index) { if (samples.getSize () == 0) { ssi_wrn ("empty sample list"); return false; } if (isTrained ()) { ssi_wrn ("already trained"); return false; } _n_classes = samples.getClassSize (); _n_features = samples.getStream (stream_index).dim; _centers = new ssi_real_t *[_n_classes]; for (ssi_size_t i = 0; i < _n_classes; i++) { _centers[i] = new ssi_real_t[_n_features]; for (ssi_size_t j = 0; j < _n_features; j++) { _centers[i][j] = 0; } } ssi_sample_t *sample; samples.reset (); ssi_real_t *ptr = 0; while (sample = samples.next ()) { ssi_size_t id = sample->class_id; ptr = ssi_pcast (ssi_real_t, sample->streams[stream_index]->ptr); for (ssi_size_t j = 0; j < _n_features; j++) { _centers[id][j] += ptr[j]; } } for (ssi_size_t i = 0; i < _n_classes; i++) { ssi_size_t num = samples.getSize (i); for (ssi_size_t j = 0; j < _n_features; j++) { _centers[i][j] /= num; } } return true; }
bool SimpleKNN::train (ISamples &samples, ssi_size_t stream_index) { if (samples.getSize () == 0) { ssi_wrn ("empty sample list"); return false; } if (samples.getSize () < _options.k) { ssi_wrn ("sample list has less than '%u' entries", _options.k); return false; } if (isTrained ()) { ssi_wrn ("already trained"); return false; } _n_classes = samples.getClassSize (); _n_samples = samples.getSize (); _n_features = samples.getStream (stream_index).dim; _data = new ssi_real_t[_n_features*_n_samples]; _classes = new ssi_size_t[_n_samples]; ssi_sample_t *sample; samples.reset (); ssi_real_t *data_ptr = _data; ssi_size_t *class_ptr = _classes; ssi_stream_t *stream_ptr = 0; ssi_size_t bytes_to_copy = _n_features * sizeof (ssi_real_t); while (sample = samples.next ()) { memcpy (data_ptr, sample->streams[stream_index]->ptr, bytes_to_copy); *class_ptr++ = sample->class_id; data_ptr += _n_features; } return true; }
void Evaluation::eval_h (ISamples &samples) { // walk through sample list and test trainer against each sample samples.reset (); const ssi_sample_t *sample = 0; ssi_size_t index, real_index; while (sample = samples.next ()) { real_index = sample->class_id; *_result_vec_ptr++ = real_index; if (_trainer->forward (sample->num, sample->streams, index)) { *_result_vec_ptr++ = index; _conf_mat_ptr[real_index][index]++; _n_classified++; } else if (!_allow_unclassified) { index = _default_class_id; *_result_vec_ptr++ = index; _conf_mat_ptr[real_index][index]++; _n_classified++; } else { *_result_vec_ptr++ = SSI_ISAMPLES_GARBAGE_CLASS_ID; _n_unclassified++; } } }