From fdb90df2d36886cbb467b50aa9ebe8c267e7f8d6 Mon Sep 17 00:00:00 2001 From: Rishabh Garg <56191449+RishabhGarg108@users.noreply.github.com> Date: Mon, 14 Jun 2021 08:52:15 +0530 Subject: [PATCH] Reverted splitInfo to arma::vec for old decision tree for numric splits --- .../best_binary_numeric_split.hpp | 2 +- .../best_binary_numeric_split_impl.hpp | 8 ++-- .../decision_tree/decision_tree_impl.hpp | 4 +- .../random_binary_numeric_split.hpp | 2 +- .../random_binary_numeric_split_impl.hpp | 5 ++- src/mlpack/tests/decision_tree_test.cpp | 38 ++++++++++--------- 6 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/mlpack/methods/decision_tree/best_binary_numeric_split.hpp b/src/mlpack/methods/decision_tree/best_binary_numeric_split.hpp index 207aac1f2a..d2e052f4dd 100644 --- a/src/mlpack/methods/decision_tree/best_binary_numeric_split.hpp +++ b/src/mlpack/methods/decision_tree/best_binary_numeric_split.hpp @@ -60,7 +60,7 @@ class BestBinaryNumericSplit const WeightVecType& weights, const size_t minimumLeafSize, const double minimumGainSplit, - double& splitInfo, + arma::vec& splitInfo, AuxiliarySplitInfo& aux); /** diff --git a/src/mlpack/methods/decision_tree/best_binary_numeric_split_impl.hpp b/src/mlpack/methods/decision_tree/best_binary_numeric_split_impl.hpp index fb9e23fb5d..ff84a5496f 100644 --- a/src/mlpack/methods/decision_tree/best_binary_numeric_split_impl.hpp +++ b/src/mlpack/methods/decision_tree/best_binary_numeric_split_impl.hpp @@ -25,7 +25,7 @@ double BestBinaryNumericSplit::SplitIfBetter( const WeightVecType& weights, const size_t minimumLeafSize, const double minimumGainSplit, - double& splitInfo, + arma::vec& splitInfo, AuxiliarySplitInfo& /* aux */) { // First sanity check: if we don't have enough points, we can't split. @@ -154,7 +154,8 @@ double BestBinaryNumericSplit::SplitIfBetter( // We can take a shortcut: no split will be better than this, so just // take this one. The actual split value will be halfway between the // value at index - 1 and index. - splitInfo = (data[sortedIndices[index - 1]] + + splitInfo.set_size(1); + splitInfo[0] = (data[sortedIndices[index - 1]] + data[sortedIndices[index]]) / 2.0; return gain; @@ -163,7 +164,8 @@ double BestBinaryNumericSplit::SplitIfBetter( { // We still have a better split. bestFoundGain = gain; - splitInfo = (data[sortedIndices[index - 1]] + + splitInfo.set_size(1); + splitInfo[0] = (data[sortedIndices[index - 1]] + data[sortedIndices[index]]) / 2.0; improved = true; } diff --git a/src/mlpack/methods/decision_tree/decision_tree_impl.hpp b/src/mlpack/methods/decision_tree/decision_tree_impl.hpp index 0c5d106002..e085fee9a4 100644 --- a/src/mlpack/methods/decision_tree/decision_tree_impl.hpp +++ b/src/mlpack/methods/decision_tree/decision_tree_impl.hpp @@ -663,7 +663,7 @@ double DecisionTree::SplitIfBetter( const WeightVecType& weights, const size_t minimumLeafSize, const double minimumGainSplit, - double& splitInfo, + arma::vec& splitInfo, AuxiliarySplitInfo& /* aux */, const bool splitIfBetterGain) { @@ -125,7 +125,8 @@ double RandomBinaryNumericSplit::SplitIfBetter( if (gain < bestFoundGain && splitIfBetterGain) return DBL_MAX; - splitInfo = randomPivot; + splitInfo.set_size(1); + splitInfo[0] = randomPivot; if (UseWeights) gain /= totalWeight; diff --git a/src/mlpack/tests/decision_tree_test.cpp b/src/mlpack/tests/decision_tree_test.cpp index 987b77179d..54213e417f 100644 --- a/src/mlpack/tests/decision_tree_test.cpp +++ b/src/mlpack/tests/decision_tree_test.cpp @@ -288,17 +288,16 @@ TEST_CASE("BestBinaryNumericSplitSimpleSplitTest", "[DecisionTreeTest]") arma::rowvec weights(labels.n_elem); weights.ones(); - arma::vec classProbabilities(1); + arma::vec classProbabilities; BestBinaryNumericSplit::AuxiliarySplitInfo aux; // Call the method to do the splitting. const double bestGain = GiniGain::Evaluate(labels, 2, weights); const double gain = BestBinaryNumericSplit::SplitIfBetter( - bestGain, values, labels, 2, weights, 3, 1e-7, classProbabilities[0], - aux); + bestGain, values, labels, 2, weights, 3, 1e-7, classProbabilities, aux); const double weightedGain = BestBinaryNumericSplit::SplitIfBetter(bestGain, values, - labels, 2, weights, 3, 1e-7, classProbabilities[0], aux); + labels, 2, weights, 3, 1e-7, classProbabilities, aux); // Make sure that a split was made. REQUIRE(gain > bestGain); @@ -326,22 +325,23 @@ TEST_CASE("BestBinaryNumericSplitMinSamplesTest", "[DecisionTreeTest]") arma::Row labels("0 0 0 0 0 1 1 1 1 1 1"); arma::rowvec weights(labels.n_elem); - arma::vec classProbabilities(1); + arma::vec classProbabilities; BestBinaryNumericSplit::AuxiliarySplitInfo aux; // Call the method to do the splitting. const double bestGain = GiniGain::Evaluate(labels, 2, weights); const double gain = BestBinaryNumericSplit::SplitIfBetter( - bestGain, values, labels, 2, weights, 8, 1e-7, classProbabilities[0], + bestGain, values, labels, 2, weights, 8, 1e-7, classProbabilities, aux); // This should make no difference because it won't split at all. const double weightedGain = BestBinaryNumericSplit::SplitIfBetter(bestGain, values, - labels, 2, weights, 8, 1e-7, classProbabilities[0], aux); + labels, 2, weights, 8, 1e-7, classProbabilities, aux); // Make sure that no split was made. REQUIRE(gain == DBL_MAX); REQUIRE(gain == weightedGain); + // REQUIRE(classProbabilities.n_elem == 0); **TODO** } /** @@ -361,17 +361,18 @@ TEST_CASE("BestBinaryNumericSplitNoGainTest", "[DecisionTreeTest]") labels[i + 1] = 1; } - arma::vec classProbabilities(1); + arma::vec classProbabilities; BestBinaryNumericSplit::AuxiliarySplitInfo aux; // Call the method to do the splitting. const double bestGain = GiniGain::Evaluate(labels, 2, weights); const double gain = BestBinaryNumericSplit::SplitIfBetter( - bestGain, values, labels, 2, weights, 10, 1e-7, classProbabilities[0], + bestGain, values, labels, 2, weights, 10, 1e-7, classProbabilities, aux); // Make sure there was no split. REQUIRE(gain == DBL_MAX); + // REQUIRE(classProbabilities.n_elem == 0); **TODO** } /** @@ -384,22 +385,22 @@ TEST_CASE("RandomBinaryNumericSplitMinSamplesTest", "[DecisionTreeTest]") arma::Row labels("0 0 0 0 0 1 1 1 1 1 1"); arma::rowvec weights(labels.n_elem); - arma::vec classProbabilities(1); + arma::vec classProbabilities; RandomBinaryNumericSplit::AuxiliarySplitInfo aux; // Call the method to do the splitting. const double bestGain = GiniGain::Evaluate(labels, 2, weights); const double gain = RandomBinaryNumericSplit::SplitIfBetter( - bestGain, values, labels, 2, weights, 8, 1e-7, classProbabilities[0], - aux); + bestGain, values, labels, 2, weights, 8, 1e-7, classProbabilities, aux); // This should make no difference because it won't split at all. const double weightedGain = RandomBinaryNumericSplit::SplitIfBetter(bestGain, values, - labels, 2, weights, 8, 1e-7, classProbabilities[0], aux); + labels, 2, weights, 8, 1e-7, classProbabilities, aux); // Make sure that no split was made. REQUIRE(gain == DBL_MAX); REQUIRE(gain == weightedGain); + // REQUIRE(classProbabilities.n_elem == 0); **TODO** } /** @@ -419,17 +420,18 @@ TEST_CASE("RandomBinaryNumericSplitNoGainTest", "[DecisionTreeTest]") labels[i + 1] = 1; } - arma::vec classProbabilities(1); + arma::vec classProbabilities; RandomBinaryNumericSplit::AuxiliarySplitInfo aux; // Call the method to do the splitting. const double bestGain = GiniGain::Evaluate(labels, 2, weights); const double gain = RandomBinaryNumericSplit::SplitIfBetter( - bestGain, values, labels, 2, weights, 10, 1e-7, classProbabilities[0], + bestGain, values, labels, 2, weights, 10, 1e-7, classProbabilities, aux, true); // Make sure there was no split. REQUIRE(gain == DBL_MAX); + // REQUIRE(classProbabilities.n_elem == 0); **TODO** } /** @@ -449,7 +451,7 @@ TEST_CASE("RandomBinaryNumericSplitDiffSplitTest", "[DecisionTreeTest]") labels[i + 1] = 1; } - arma::vec classProbabilities(1), classProbabilities1(1); + arma::vec classProbabilities, classProbabilities1; BestBinaryNumericSplit::AuxiliarySplitInfo aux; RandomBinaryNumericSplit::AuxiliarySplitInfo aux1; @@ -459,12 +461,12 @@ TEST_CASE("RandomBinaryNumericSplitDiffSplitTest", "[DecisionTreeTest]") { // Call BestBinaryNumericSplit to do the splitting. (void) BestBinaryNumericSplit::SplitIfBetter( - bestGain, values, labels, 2, weights, 3, 1e-7, classProbabilities[0], + bestGain, values, labels, 2, weights, 3, 1e-7, classProbabilities, aux); // Call RandomBinaryNumericSplit to do the splitting. (void) RandomBinaryNumericSplit::SplitIfBetter( - bestGain, values, labels, 2, weights, 3, 1e-7, classProbabilities1[0], + bestGain, values, labels, 2, weights, 3, 1e-7, classProbabilities1, aux1); if (classProbabilities[0] == classProbabilities1[0])