Reverted splitInfo to arma::vec for old decision tree for numric splits

This commit is contained in:
Rishabh Garg
2021-07-12 10:32:56 +05:30
parent 57572aa876
commit fdb90df2d3
6 changed files with 32 additions and 27 deletions
@@ -60,7 +60,7 @@ class BestBinaryNumericSplit
const WeightVecType& weights,
const size_t minimumLeafSize,
const double minimumGainSplit,
double& splitInfo,
arma::vec& splitInfo,
AuxiliarySplitInfo& aux);
/**
@@ -25,7 +25,7 @@ double BestBinaryNumericSplit<FitnessFunction>::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<FitnessFunction>::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<FitnessFunction>::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;
}
@@ -663,7 +663,7 @@ double DecisionTree<FitnessFunction,
UseWeights ? weights.subvec(begin, begin + count - 1) : weights,
minimumLeafSize,
minimumGainSplit,
classProbabilities[0],
classProbabilities,
*this);
}
@@ -836,7 +836,7 @@ double DecisionTree<FitnessFunction,
weights,
minimumLeafSize,
minimumGainSplit,
classProbabilities[0],
classProbabilities,
*this);
// If the splitter did not report that it improved, then move to the next
@@ -82,7 +82,7 @@ class RandomBinaryNumericSplit
const WeightVecType& weights,
const size_t minimumLeafSize,
const double minimumGainSplit,
double& splitInfo,
arma::vec& splitInfo,
AuxiliarySplitInfo& aux,
const bool splitIfBetterGain = false);
@@ -27,7 +27,7 @@ double RandomBinaryNumericSplit<FitnessFunction>::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<FitnessFunction>::SplitIfBetter(
if (gain < bestFoundGain && splitIfBetterGain)
return DBL_MAX;
splitInfo = randomPivot;
splitInfo.set_size(1);
splitInfo[0] = randomPivot;
if (UseWeights)
gain /= totalWeight;
+20 -18
View File
@@ -288,17 +288,16 @@ TEST_CASE("BestBinaryNumericSplitSimpleSplitTest", "[DecisionTreeTest]")
arma::rowvec weights(labels.n_elem);
weights.ones();
arma::vec classProbabilities(1);
arma::vec classProbabilities;
BestBinaryNumericSplit<GiniGain>::AuxiliarySplitInfo aux;
// Call the method to do the splitting.
const double bestGain = GiniGain::Evaluate<false>(labels, 2, weights);
const double gain = BestBinaryNumericSplit<GiniGain>::SplitIfBetter<false>(
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<GiniGain>::SplitIfBetter<true>(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<size_t> 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<GiniGain>::AuxiliarySplitInfo aux;
// Call the method to do the splitting.
const double bestGain = GiniGain::Evaluate<false>(labels, 2, weights);
const double gain = BestBinaryNumericSplit<GiniGain>::SplitIfBetter<false>(
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<GiniGain>::SplitIfBetter<true>(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<GiniGain>::AuxiliarySplitInfo aux;
// Call the method to do the splitting.
const double bestGain = GiniGain::Evaluate<false>(labels, 2, weights);
const double gain = BestBinaryNumericSplit<GiniGain>::SplitIfBetter<false>(
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<size_t> 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<GiniGain>::AuxiliarySplitInfo aux;
// Call the method to do the splitting.
const double bestGain = GiniGain::Evaluate<false>(labels, 2, weights);
const double gain = RandomBinaryNumericSplit<GiniGain>::SplitIfBetter<false>(
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<GiniGain>::SplitIfBetter<true>(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<GiniGain>::AuxiliarySplitInfo aux;
// Call the method to do the splitting.
const double bestGain = GiniGain::Evaluate<false>(labels, 2, weights);
const double gain = RandomBinaryNumericSplit<GiniGain>::SplitIfBetter<false>(
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<GiniGain>::AuxiliarySplitInfo aux;
RandomBinaryNumericSplit<GiniGain>::AuxiliarySplitInfo aux1;
@@ -459,12 +461,12 @@ TEST_CASE("RandomBinaryNumericSplitDiffSplitTest", "[DecisionTreeTest]")
{
// Call BestBinaryNumericSplit to do the splitting.
(void) BestBinaryNumericSplit<GiniGain>::SplitIfBetter<false>(
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<GiniGain>::SplitIfBetter<false>(
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])