MCPcopy Create free account
hub / github.com/antmachineintelligence/mtgbmcode / Sample

Method Sample

include/LightGBM/utils/random.h:69–98  ·  view source on GitHub ↗

! * \brief Sample K data from {0,1,...,N-1} * \param N * \param K * \return K Ordered sampled data from {0,1,...,N-1} */

Source from the content-addressed store, hash-verified

67 * \return K Ordered sampled data from {0,1,...,N-1}
68 */
69 inline std::vector<int> Sample(int N, int K) {
70 std::vector<int> ret;
71 ret.reserve(K);
72 if (K > N || K <= 0) {
73 return ret;
74 } else if (K == N) {
75 for (int i = 0; i < N; ++i) {
76 ret.push_back(i);
77 }
78 } else if (K > 1 && K > (N / std::log2(K))) {
79 for (int i = 0; i < N; ++i) {
80 double prob = (K - ret.size()) / static_cast<double>(N - i);
81 if (NextFloat() < prob) {
82 ret.push_back(i);
83 }
84 }
85 } else {
86 std::set<int> sample_set;
87 while (static_cast<int>(sample_set.size()) < K) {
88 int next = RandInt32() % N;
89 if (sample_set.count(next) == 0) {
90 sample_set.insert(next);
91 }
92 }
93 for (auto iter = sample_set.begin(); iter != sample_set.end(); ++iter) {
94 ret.push_back(*iter);
95 }
96 }
97 return ret;
98 }
99
100 private:
101 inline int RandInt16() {

Callers 8

FindGroupsFunction · 0.45
GetUsedFeaturesMethod · 0.45
GetUsedFeaturesMethod · 0.45

Calls 7

push_backMethod · 0.80
reserveMethod · 0.45
sizeMethod · 0.45
countMethod · 0.45
insertMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected