From 9acf8d4b42bd08629985f056c9f4ad1efcb33698 Mon Sep 17 00:00:00 2001 From: Saurabh7 Date: Thu, 30 Jun 2016 15:46:31 +0530 Subject: [PATCH] extend to to other label types --- src/shogun/features/DenseFeatures.cpp | 2 +- src/shogun/labels/BinaryLabels.cpp | 13 +++++++++++++ src/shogun/labels/BinaryLabels.h | 3 +++ src/shogun/labels/MulticlassLabels.cpp | 2 +- src/shogun/labels/RegressionLabels.cpp | 12 ++++++++++++ src/shogun/labels/RegressionLabels.h | 2 ++ src/shogun/multiclass/tree/CARTree.cpp | 7 ++++--- 7 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/shogun/features/DenseFeatures.cpp b/src/shogun/features/DenseFeatures.cpp index 270d079bbd5..0b3f7d8e5fe 100644 --- a/src/shogun/features/DenseFeatures.cpp +++ b/src/shogun/features/DenseFeatures.cpp @@ -642,7 +642,7 @@ CFeatures* CDenseFeatures::shallow_subset_copy() CFeatures* shallow_copy_features=nullptr; SG_SDEBUG("Using underlying feature matrix with %d dimensions and %d feature vectors!\n", num_features, num_vectors); - SGMatrix shallow_copy_matrix = SGMatrix(feature_matrix.matrix, num_features, num_vectors, false); + SGMatrix shallow_copy_matrix(feature_matrix); shallow_copy_features=new CDenseFeatures(shallow_copy_matrix); if (m_subset_stack->has_subsets()) shallow_copy_features->add_subset(m_subset_stack->get_last_subset()->get_subset_idx()); diff --git a/src/shogun/labels/BinaryLabels.cpp b/src/shogun/labels/BinaryLabels.cpp index 82138551edd..2673e6e5130 100644 --- a/src/shogun/labels/BinaryLabels.cpp +++ b/src/shogun/labels/BinaryLabels.cpp @@ -138,3 +138,16 @@ void CBinaryLabels::scores_to_probabilities(float64_t a, float64_t b) SG_DEBUG("leaving CBinaryLabels::scores_to_probabilities()\n") } + +CLabels* CBinaryLabels::shallow_subset_copy() +{ + CLabels* shallow_copy_labels=nullptr; + SGVector shallow_copy_vector(m_labels); + shallow_copy_labels=new CBinaryLabels(m_labels.size()); + + ((CDenseLabels*) shallow_copy_labels)->set_labels(shallow_copy_vector); + if (m_subset_stack->has_subsets()) + shallow_copy_labels->add_subset(m_subset_stack->get_last_subset()->get_subset_idx()); + + return shallow_copy_labels; +} diff --git a/src/shogun/labels/BinaryLabels.h b/src/shogun/labels/BinaryLabels.h index 52f7b2c7fd6..3b6bdfc25e8 100644 --- a/src/shogun/labels/BinaryLabels.h +++ b/src/shogun/labels/BinaryLabels.h @@ -116,6 +116,9 @@ class CBinaryLabels : public CDenseLabels { return "BinaryLabels"; } + + virtual CLabels* shallow_subset_copy(); + }; } #endif diff --git a/src/shogun/labels/MulticlassLabels.cpp b/src/shogun/labels/MulticlassLabels.cpp index 668f1108bfe..71d5d47bd86 100644 --- a/src/shogun/labels/MulticlassLabels.cpp +++ b/src/shogun/labels/MulticlassLabels.cpp @@ -138,7 +138,7 @@ int32_t CMulticlassLabels::get_num_classes() CLabels* CMulticlassLabels::shallow_subset_copy() { CLabels* shallow_copy_labels=nullptr; - SGVector shallow_copy_vector = SGVector(m_labels.vector, m_labels.size(), false); + SGVector shallow_copy_vector(m_labels); shallow_copy_labels=new CMulticlassLabels(m_labels.size()); ((CDenseLabels*) shallow_copy_labels)->set_labels(shallow_copy_vector); diff --git a/src/shogun/labels/RegressionLabels.cpp b/src/shogun/labels/RegressionLabels.cpp index f14d62fd1ea..03bdba52815 100644 --- a/src/shogun/labels/RegressionLabels.cpp +++ b/src/shogun/labels/RegressionLabels.cpp @@ -25,3 +25,15 @@ ELabelType CRegressionLabels::get_label_type() const return LT_REGRESSION; } +CLabels* CRegressionLabels::shallow_subset_copy() +{ + CLabels* shallow_copy_labels=nullptr; + SGVector shallow_copy_vector(m_labels); + shallow_copy_labels=new CRegressionLabels(m_labels.size()); + + ((CDenseLabels*) shallow_copy_labels)->set_labels(shallow_copy_vector); + if (m_subset_stack->has_subsets()) + shallow_copy_labels->add_subset(m_subset_stack->get_last_subset()->get_subset_idx()); + + return shallow_copy_labels; +} diff --git a/src/shogun/labels/RegressionLabels.h b/src/shogun/labels/RegressionLabels.h index 1d922bfd6db..3af9a05cf9a 100644 --- a/src/shogun/labels/RegressionLabels.h +++ b/src/shogun/labels/RegressionLabels.h @@ -65,6 +65,8 @@ class CRegressionLabels : public CDenseLabels /** @return object name */ virtual const char* get_name() const { return "RegressionLabels"; } + + virtual CLabels* shallow_subset_copy(); }; } #endif diff --git a/src/shogun/multiclass/tree/CARTree.cpp b/src/shogun/multiclass/tree/CARTree.cpp index be4b95311d5..1b0f62361fb 100644 --- a/src/shogun/multiclass/tree/CARTree.cpp +++ b/src/shogun/multiclass/tree/CARTree.cpp @@ -603,6 +603,8 @@ int32_t CCARTree::compute_best_attribute(const SGMatrix& mat, const S { SGVector feats(num_vecs); SGVector sorted_args(num_vecs); + SGVector temp_count_indices(count_indices.size()); + memcpy(temp_count_indices.vector, count_indices.vector, sizeof(int32_t)*count_indices.size()); if (m_pre_sort) { @@ -613,13 +615,12 @@ int32_t CCARTree::compute_best_attribute(const SGMatrix& mat, const S { if (indices_mask[sorted_indices[j]]>=0) { - int32_t count_idx = count_indices[sorted_indices[j]]; - while(count_idx>0) + while(temp_count_indices[sorted_indices[j]]>0) { feats[count]=temp_col[j]; sorted_args[count]=indices_mask[sorted_indices[j]]; ++count; - --count_idx; + --temp_count_indices[sorted_indices[j]]; } if (count==num_vecs) break;