mlpack 3.4.2
bootstrap.hpp
Go to the documentation of this file.
1
13#ifndef MLPACK_METHODS_RANDOM_FOREST_BOOTSTRAP_HPP
14#define MLPACK_METHODS_RANDOM_FOREST_BOOTSTRAP_HPP
15
16namespace mlpack {
17namespace tree {
18
22template<bool UseWeights,
23 typename MatType,
24 typename LabelsType,
25 typename WeightsType>
26void Bootstrap(const MatType& dataset,
27 const LabelsType& labels,
28 const WeightsType& weights,
29 MatType& bootstrapDataset,
30 LabelsType& bootstrapLabels,
31 WeightsType& bootstrapWeights)
32{
33 bootstrapDataset.set_size(dataset.n_rows, dataset.n_cols);
34 bootstrapLabels.set_size(labels.n_elem);
35 if (UseWeights)
36 bootstrapWeights.set_size(weights.n_elem);
37
38 // Random sampling with replacement.
39 arma::uvec indices = arma::randi<arma::uvec>(dataset.n_cols,
40 arma::distr_param(0, dataset.n_cols - 1));
41 for (size_t i = 0; i < dataset.n_cols; ++i)
42 {
43 bootstrapDataset.col(i) = dataset.col(indices[i]);
44 bootstrapLabels[i] = labels[indices[i]];
45 if (UseWeights)
46 bootstrapWeights[i] = weights[indices[i]];
47 }
48}
49
50} // namespace tree
51} // namespace mlpack
52
53#endif
void Bootstrap(const MatType &dataset, const LabelsType &labels, const WeightsType &weights, MatType &bootstrapDataset, LabelsType &bootstrapLabels, WeightsType &bootstrapWeights)
Given a dataset, create another dataset via bootstrap sampling, with labels.
Definition: bootstrap.hpp:26
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1