SGVector<float64_t> CBaggingMachine::apply_get_outputs(CFeatures* data) { ASSERT(data != NULL); REQUIRE(m_combination_rule != NULL, "Combination rule is not set!"); ASSERT(m_num_bags == m_bags->get_num_elements()); SGMatrix<float64_t> output(data->get_num_vectors(), m_num_bags); output.zero(); #pragma omp parallel for num_threads(parallel->get_num_threads()) for (int32_t i = 0; i < m_num_bags; ++i) { CMachine* m = dynamic_cast<CMachine*>(m_bags->get_element(i)); CLabels* l = m->apply(data); SGVector<float64_t> lv = l->get_values(); float64_t* bag_results = output.get_column_vector(i); memcpy(bag_results, lv.vector, lv.vlen*sizeof(float64_t)); SG_UNREF(l); SG_UNREF(m); } SGVector<float64_t> combined = m_combination_rule->combine(output); return combined; }
float64_t CBaggingMachine::get_oob_error(CEvaluation* eval) const { REQUIRE(m_combination_rule != NULL, "Combination rule is not set!"); REQUIRE(m_bags->get_num_elements() > 0, "BaggingMachine is not trained!"); SGMatrix<float64_t> output(m_features->get_num_vectors(), m_bags->get_num_elements()); if (m_labels->get_label_type() == LT_REGRESSION) output.zero(); else output.set_const(NAN); /* TODO: add parallel support of applying the OOBs only possible when add_subset is thread-safe #pragma omp parallel for num_threads(parallel->get_num_threads()) */ for (index_t i = 0; i < m_bags->get_num_elements(); i++) { CMachine* m = dynamic_cast<CMachine*>(m_bags->get_element(i)); CDynamicArray<index_t>* current_oob = dynamic_cast<CDynamicArray<index_t>*>(m_oob_indices->get_element(i)); SGVector<index_t> oob(current_oob->get_array(), current_oob->get_num_elements(), false); oob.display_vector(); m_features->add_subset(oob); CLabels* l = m->apply(m_features); SGVector<float64_t> lv = l->get_values(); // assign the values in the matrix (NAN) that are in-bag! for (index_t j = 0; j < oob.vlen; j++) output(oob[j], i) = lv[j]; m_features->remove_subset(); SG_UNREF(current_oob); SG_UNREF(m); SG_UNREF(l); } output.display_matrix(); DynArray<index_t> idx; for (index_t i = 0; i < m_features->get_num_vectors(); i++) { if (m_all_oob_idx[i]) idx.push_back(i); } SGVector<float64_t> combined = m_combination_rule->combine(output); CLabels* predicted = NULL; switch (m_labels->get_label_type()) { case LT_BINARY: predicted = new CBinaryLabels(combined); break; case LT_MULTICLASS: predicted = new CMulticlassLabels(combined); break; case LT_REGRESSION: predicted = new CRegressionLabels(combined); break; default: SG_ERROR("Unsupported label type\n"); } m_labels->add_subset(SGVector<index_t>(idx.get_array(), idx.get_num_elements(), false)); float64_t res = eval->evaluate(predicted, m_labels); m_labels->remove_subset(); return res; }