13 #ifndef MLPACK_METHODS_DECISION_TREE_MSE_GAIN_HPP 14 #define MLPACK_METHODS_DECISION_TREE_MSE_GAIN_HPP 43 template<
bool UseWeights,
typename VecType,
typename WeightVecType>
45 const WeightVecType& weights,
53 double accWeights = 0.0;
54 double weightedMean = 0.0;
55 WeightedSum(values, weights, begin, end, accWeights, weightedMean);
58 if (accWeights == 0.0)
61 weightedMean /= accWeights;
63 for (
size_t i = begin; i < end; ++i)
64 mse += weights[i] * std::pow(values[i] - weightedMean, 2);
71 Sum(values, begin, end, mean);
72 mean /= (double) (end - begin);
74 mse = arma::accu(arma::square(values.subvec(begin, end - 1) - mean));
75 mse /= (double) (end - begin);
87 template<
bool UseWeights,
typename VecType,
typename WeightVecType>
89 const WeightVecType& weights)
92 if (values.n_elem == 0)
95 return Evaluate<UseWeights>(values, weights, 0, values.n_elem);
111 double mseLeft = leftSumSquares / leftSize - leftMean * leftMean;
112 double mseRight = (totalSumSquares - leftSumSquares) / rightSize
113 - rightMean * rightMean;
115 return std::make_tuple(-mseLeft, -mseRight);
126 template<
bool UseWeights,
typename ResponsesType,
typename WeightVecType>
128 const WeightVecType& weights,
129 const size_t minimum)
131 typedef typename ResponsesType::elem_type RType;
132 typedef typename WeightVecType::elem_type WType;
139 leftSumSquares = 0.0;
140 totalSumSquares = 0.0;
144 totalSumSquares = arma::accu(weights % arma::square(responses));
145 for (
size_t i = 0; i < minimum - 1; ++i)
147 const WType w = weights[i];
148 const RType x = responses[i];
153 leftSumSquares += w * x * x;
156 leftMean /= leftSize;
158 for (
size_t i = minimum - 1; i < responses.n_elem; ++i)
160 const WType w = weights[i];
161 const RType x = responses[i];
167 if (rightSize > 1e-9)
168 rightMean /= rightSize;
172 totalSumSquares = arma::accu(arma::square(responses));
173 for (
size_t i = 0; i < minimum - 1; ++i)
175 const RType x = responses[i];
180 leftSumSquares += x * x;
183 leftMean /= leftSize;
185 for (
size_t i = minimum - 1; i < responses.n_elem; ++i)
187 const RType x = responses[i];
193 if (rightSize > 1e-9)
194 rightMean /= rightSize;
205 template<
bool UseWeights,
typename ResponsesType,
typename WeightVecType>
207 const WeightVecType& weights,
210 typedef typename ResponsesType::elem_type RType;
211 typedef typename WeightVecType::elem_type WType;
215 const WType w = weights[index];
216 const RType x = responses[index];
219 leftSumSquares += w * x * x;
222 leftMean = (leftMean * leftSize + w * x) / (leftSize + w);
225 rightMean = (rightMean * rightSize - w * x) / (rightSize - w);
230 const RType x = responses[index];
233 leftSumSquares += x * x;
236 leftMean = (leftMean * leftSize + x) / (leftSize + 1);
239 rightMean = (rightMean * rightSize - x) / (rightSize - 1);
250 double leftSumSquares;
260 double totalSumSquares;
void BinaryStep(const ResponsesType &responses, const WeightVecType &weights, const size_t index)
Updates the statistics for the given index.
Definition: mse_gain.hpp:206
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
static double Evaluate(const VecType &values, const WeightVecType &weights, const size_t begin, const size_t end)
Evaluate the mean squared error gain of values from begin to end index.
Definition: mse_gain.hpp:44
The core includes that mlpack expects; standard C++ includes and Armadillo.
void WeightedSum(const VecType &values, const WeightVecType &weights, const size_t begin, const size_t end, double &accWeights, double &weightedMean)
Calculates the weighted sum and total weight of labels.
Definition: utils.hpp:19
The MSE (Mean squared error) gain, is a measure of set purity based on the variance of response value...
Definition: mse_gain.hpp:28
void BinaryScanInitialize(const ResponsesType &responses, const WeightVecType &weights, const size_t minimum)
Caches the prefix sum of squares to efficiently compute gain value for each split.
Definition: mse_gain.hpp:127
static double Evaluate(const VecType &values, const WeightVecType &weights)
Evaluate the MSE gain on the complete vector.
Definition: mse_gain.hpp:88
std::tuple< double, double > BinaryGains()
Calculates the mean squared error gain for the left and right children for the current index...
Definition: mse_gain.hpp:109
void Sum(const VecType &values, const size_t begin, const size_t end, double &mean)
Sums up the labels vector.
Definition: utils.hpp:96