tree.h 17.4 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
6
7
8
#ifndef LIGHTGBM_TREE_H_
#define LIGHTGBM_TREE_H_

#include <LightGBM/meta.h>
#include <LightGBM/dataset.h>

#include <string>
#include <vector>
Guolin Ke's avatar
Guolin Ke committed
9
#include <memory>
Guolin Ke's avatar
Guolin Ke committed
10
11
12

namespace LightGBM {

13
#define kMaxTreeOutput (100)
Guolin Ke's avatar
Guolin Ke committed
14
15
#define kCategoricalMask (1)
#define kDefaultLeftMask (2)
Guolin Ke's avatar
Guolin Ke committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

/*!
* \brief Tree model
*/
class Tree {
public:
  /*!
  * \brief Constructor
  * \param max_leaves The number of max leaves
  */
  explicit Tree(int max_leaves);

  /*!
  * \brief Construtor, from a string
  * \param str Model string
  */
  explicit Tree(const std::string& str);

  ~Tree();

  /*!
Qiwei Ye's avatar
Qiwei Ye committed
37
38
39
  * \brief Performing a split on tree leaves.
  * \param leaf Index of leaf to be split
  * \param feature Index of feature; the converted index after removing useless features
40
  * \param bin_type type of this feature, numerical or categorical
Guolin Ke's avatar
Guolin Ke committed
41
42
  * \param threshold Threshold(bin) of split
  * \param real_feature Index of feature, the original index on data
43
  * \param threshold_double Threshold on feature value
Guolin Ke's avatar
Guolin Ke committed
44
45
  * \param left_value Model Left child output
  * \param right_value Model Right child output
Guolin Ke's avatar
Guolin Ke committed
46
47
  * \param left_cnt Count of left child
  * \param right_cnt Count of right child
Guolin Ke's avatar
Guolin Ke committed
48
  * \param gain Split gain
Guolin Ke's avatar
Guolin Ke committed
49
50
  * \param missing_type missing type
  * \param default_left default direction for missing value
Guolin Ke's avatar
Guolin Ke committed
51
52
  * \return The index of new leaf.
  */
Guolin Ke's avatar
Guolin Ke committed
53
54
  int Split(int leaf, int feature, BinType bin_type, uint32_t threshold, int real_feature, 
            double threshold_double, double left_value, double right_value, 
Guolin Ke's avatar
Guolin Ke committed
55
            data_size_t left_cnt, data_size_t right_cnt, double gain, MissingType missing_type, bool default_left);
Guolin Ke's avatar
Guolin Ke committed
56

Guolin Ke's avatar
Guolin Ke committed
57
  /*! \brief Get the output of one leaf */
58
  inline double LeafOutput(int leaf) const { return leaf_value_[leaf]; }
Guolin Ke's avatar
Guolin Ke committed
59

Guolin Ke's avatar
Guolin Ke committed
60
61
62
63
64
  /*! \brief Set the output of one leaf */
  inline void SetLeafOutput(int leaf, double output) {
    leaf_value_[leaf] = output;
  }

Guolin Ke's avatar
Guolin Ke committed
65
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
66
  * \brief Adding prediction value of this tree model to scores
Guolin Ke's avatar
Guolin Ke committed
67
68
69
70
  * \param data The dataset
  * \param num_data Number of total data
  * \param score Will add prediction to score
  */
71
72
73
  void AddPredictionToScore(const Dataset* data,
                            data_size_t num_data,
                            double* score) const;
Guolin Ke's avatar
Guolin Ke committed
74
75

  /*!
Qiwei Ye's avatar
Qiwei Ye committed
76
  * \brief Adding prediction value of this tree model to scorese
Guolin Ke's avatar
Guolin Ke committed
77
78
79
80
81
82
  * \param data The dataset
  * \param used_data_indices Indices of used data
  * \param num_data Number of total data
  * \param score Will add prediction to score
  */
  void AddPredictionToScore(const Dataset* data,
Qiwei Ye's avatar
Qiwei Ye committed
83
                            const data_size_t* used_data_indices,
84
                            data_size_t num_data, double* score) const;
Guolin Ke's avatar
Guolin Ke committed
85
86

  /*!
87
  * \brief Prediction on one record
Guolin Ke's avatar
Guolin Ke committed
88
89
90
  * \param feature_values Feature value of this record
  * \return Prediction result
  */
91
92
  inline double Predict(const double* feature_values) const;
  inline int PredictLeafIndex(const double* feature_values) const;
Guolin Ke's avatar
Guolin Ke committed
93

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  inline void PredictContrib(const double* feature_values, int num_features, double* output) const;

  inline double ExpectedValue(int node = 0) const;

  inline int MaxDepth() const;

  /*!
  * \brief Used by TreeSHAP for data we keep about our decision path
  */
  struct PathElement {
    int feature_index;
    double zero_fraction;
    double one_fraction;

    // note that pweight is included for convenience and is not tied with the other attributes,
    // the pweight of the i'th path element is the permuation weight of paths with i-1 ones in them
    double pweight;

    PathElement() {}
    PathElement(int i, double z, double o, double w) : feature_index(i), zero_fraction(z), one_fraction(o), pweight(w) {}
  };

  /*! \brief Polynomial time algorithm for SHAP values (https://arxiv.org/abs/1706.06060) */
  inline void TreeSHAP(const double *feature_values, double *phi,
                       int node, int unique_depth,
                       PathElement *parent_unique_path, double parent_zero_fraction,
                       double parent_one_fraction, int parent_feature_index) const;

Guolin Ke's avatar
Guolin Ke committed
122
123
124
  /*! \brief Get Number of leaves*/
  inline int num_leaves() const { return num_leaves_; }

Guolin Ke's avatar
Guolin Ke committed
125
126
127
  /*! \brief Get depth of specific leaf*/
  inline int leaf_depth(int leaf_idx) const { return leaf_depth_[leaf_idx]; }

wxchan's avatar
wxchan committed
128
  /*! \brief Get feature of specific split*/
Guolin Ke's avatar
Guolin Ke committed
129
  inline int split_feature(int split_idx) const { return split_feature_[split_idx]; }
wxchan's avatar
wxchan committed
130

Guolin Ke's avatar
Guolin Ke committed
131
132
  inline double split_gain(int split_idx) const { return split_gain_[split_idx]; }

133
134
135
  /*! \brief Get the number of data points that fall at or below this node*/
  inline int data_count(int node = 0) const { return node >= 0 ? internal_count_[node] : leaf_count_[~node]; }

Guolin Ke's avatar
Guolin Ke committed
136
137
  /*!
  * \brief Shrinkage for the tree's output
Qiwei Ye's avatar
Qiwei Ye committed
138
  *        shrinkage rate (a.k.a learning rate) is used to tune the traning process
Guolin Ke's avatar
Guolin Ke committed
139
140
  * \param rate The factor of shrinkage
  */
141
  inline void Shrinkage(double rate) {
Guolin Ke's avatar
Guolin Ke committed
142
    #pragma omp parallel for schedule(static, 512) if (num_leaves_ >= 1024)
Guolin Ke's avatar
Guolin Ke committed
143
    for (int i = 0; i < num_leaves_; ++i) {
Guolin Ke's avatar
Guolin Ke committed
144
      leaf_value_[i] *= rate;
145
      if (leaf_value_[i] > kMaxTreeOutput) { leaf_value_[i] = kMaxTreeOutput; }
146
      else if (leaf_value_[i] < -kMaxTreeOutput) { leaf_value_[i] = -kMaxTreeOutput; }
Guolin Ke's avatar
Guolin Ke committed
147
    }
Guolin Ke's avatar
Guolin Ke committed
148
    shrinkage_ *= rate;
Guolin Ke's avatar
Guolin Ke committed
149
150
  }

wxchan's avatar
wxchan committed
151
  /*! \brief Serialize this object to string*/
Guolin Ke's avatar
Guolin Ke committed
152
153
  std::string ToString();

wxchan's avatar
wxchan committed
154
155
156
  /*! \brief Serialize this object to json*/
  std::string ToJSON();

157
158
159
  /*! \brief Serialize this object to if-else statement*/
  std::string ToIfElse(int index, bool is_predict_leaf_index);

160
  template<typename T>
Guolin Ke's avatar
Guolin Ke committed
161
  inline static bool CategoricalDecision(T fval, T threshold) {
162
163
164
165
166
167
168
    if (static_cast<int>(fval) == static_cast<int>(threshold)) {
      return true;
    } else {
      return false;
    }
  }

Guolin Ke's avatar
Guolin Ke committed
169
  template<typename T>
Guolin Ke's avatar
Guolin Ke committed
170
  inline static bool NumericalDecision(T fval, T threshold) {
Guolin Ke's avatar
Guolin Ke committed
171
172
173
174
175
176
177
    if (fval <= threshold) {
      return true;
    } else {
      return false;
    }
  }

Guolin Ke's avatar
Guolin Ke committed
178
179
180
  inline static bool IsZero(double fval) {
    if (fval > -kZeroAsMissingValueRange && fval <= kZeroAsMissingValueRange) {
      return true;
Guolin Ke's avatar
Guolin Ke committed
181
    } else {
Guolin Ke's avatar
Guolin Ke committed
182
      return false;
Guolin Ke's avatar
Guolin Ke committed
183
184
185
    }
  }

Guolin Ke's avatar
Guolin Ke committed
186
187
188
189
190
191
192
  inline static bool GetDecisionType(int8_t decision_type, int8_t mask) {
    return (decision_type & mask) > 0;
  }

  inline static void SetDecisionType(int8_t* decision_type, bool input, int8_t mask) {
    if (input) {
      (*decision_type) |= mask;
Guolin Ke's avatar
Guolin Ke committed
193
    } else {
Guolin Ke's avatar
Guolin Ke committed
194
      (*decision_type) &= (127 - mask);
Guolin Ke's avatar
Guolin Ke committed
195
196
197
    }
  }

Guolin Ke's avatar
Guolin Ke committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  inline static int8_t GetMissingType(int8_t decision_type) {
    return (decision_type >> 2) & 3;
  }

  inline static void SetMissingType(int8_t* decision_type, int8_t input) {
    (*decision_type) &= 3;
    (*decision_type) |= (input << 2);
  }

  inline static uint32_t ConvertMissingValue(uint32_t fval, uint32_t threshold, int8_t decision_type, uint32_t default_bin, uint32_t max_bin) {
    uint8_t missing_type = GetMissingType(decision_type);
    if ((missing_type == 1 && fval == default_bin)
        || (missing_type == 2 && fval == max_bin)) {
      if (GetDecisionType(decision_type, kDefaultLeftMask)) {
        fval = threshold;
      } else {
        fval = threshold + 1;
      }
    }
    return fval;
  }

  inline static double ConvertMissingValue(double fval, double threshold, int8_t decision_type) {
    uint8_t missing_type = GetMissingType(decision_type);
    if (std::isnan(fval)) {
      if (missing_type != 2) {
        fval = 0.0f;
      }
    }
    if ((missing_type == 1 && IsZero(fval))
        || (missing_type == 2 && std::isnan(fval))) {
      if (GetDecisionType(decision_type, kDefaultLeftMask)) {
        fval = threshold;
      } else {
        fval = 10.0f * threshold;
      }
    }
    return fval;
  }
Guolin Ke's avatar
Guolin Ke committed
237

Guolin Ke's avatar
Guolin Ke committed
238
  inline static const char* GetDecisionTypeName(int8_t type) {
239
240
241
242
243
244
    if (type == 0) {
      return "no_greater";
    } else {
      return "is";
    }
  }
Guolin Ke's avatar
Guolin Ke committed
245

246
247
  static std::vector<bool(*)(uint32_t, uint32_t)> inner_decision_funs;
  static std::vector<bool(*)(double, double)> decision_funs;
Guolin Ke's avatar
Guolin Ke committed
248

249
private:
Guolin Ke's avatar
Guolin Ke committed
250
251

  /*!
Qiwei Ye's avatar
Qiwei Ye committed
252
  * \brief Find leaf index of which record belongs by features
Guolin Ke's avatar
Guolin Ke committed
253
254
255
  * \param feature_values Feature value of this record
  * \return Leaf index
  */
256
  inline int GetLeaf(const double* feature_values) const;
Guolin Ke's avatar
Guolin Ke committed
257

wxchan's avatar
wxchan committed
258
259
260
  /*! \brief Serialize one node to json*/
  inline std::string NodeToJSON(int index);

261
262
263
  /*! \brief Serialize one node to if-else statement*/
  inline std::string NodeToIfElse(int index, bool is_predict_leaf_index);

264
265
266
267
268
269
270
271
272
273
  /*! \brief Extend our decision path with a fraction of one and zero extensions for TreeSHAP*/
  inline static void ExtendPath(PathElement *unique_path, int unique_depth,
                                double zero_fraction, double one_fraction, int feature_index);

  /*! \brief Undo a previous extension of the decision path for TreeSHAP*/
  inline static void UnwindPath(PathElement *unique_path, int unique_depth, int path_index);

  /*! determine what the total permuation weight would be if we unwound a previous extension in the decision path*/
  inline static double UnwoundPathSum(const PathElement *unique_path, int unique_depth, int path_index);

Guolin Ke's avatar
Guolin Ke committed
274
275
276
277
278
279
  /*! \brief Number of max leaves*/
  int max_leaves_;
  /*! \brief Number of current levas*/
  int num_leaves_;
  // following values used for non-leaf node
  /*! \brief A non-leaf node's left child */
Guolin Ke's avatar
Guolin Ke committed
280
  std::vector<int> left_child_;
Guolin Ke's avatar
Guolin Ke committed
281
  /*! \brief A non-leaf node's right child */
Guolin Ke's avatar
Guolin Ke committed
282
  std::vector<int> right_child_;
Guolin Ke's avatar
Guolin Ke committed
283
  /*! \brief A non-leaf node's split feature */
Guolin Ke's avatar
Guolin Ke committed
284
  std::vector<int> split_feature_inner_;
Guolin Ke's avatar
Guolin Ke committed
285
  /*! \brief A non-leaf node's split feature, the original index */
Guolin Ke's avatar
Guolin Ke committed
286
  std::vector<int> split_feature_;
Guolin Ke's avatar
Guolin Ke committed
287
  /*! \brief A non-leaf node's split threshold in bin */
Guolin Ke's avatar
Guolin Ke committed
288
  std::vector<uint32_t> threshold_in_bin_;
Guolin Ke's avatar
Guolin Ke committed
289
  /*! \brief A non-leaf node's split threshold in feature value */
Guolin Ke's avatar
Guolin Ke committed
290
  std::vector<double> threshold_;
Guolin Ke's avatar
Guolin Ke committed
291
  /*! \brief Store the information for categorical feature handle and mising value handle. */
292
  std::vector<int8_t> decision_type_;
Guolin Ke's avatar
Guolin Ke committed
293
  /*! \brief A non-leaf node's split gain */
Guolin Ke's avatar
Guolin Ke committed
294
  std::vector<double> split_gain_;
Guolin Ke's avatar
Guolin Ke committed
295
296
  // used for leaf node
  /*! \brief The parent of leaf */
Guolin Ke's avatar
Guolin Ke committed
297
  std::vector<int> leaf_parent_;
Guolin Ke's avatar
Guolin Ke committed
298
  /*! \brief Output of leaves */
Guolin Ke's avatar
Guolin Ke committed
299
  std::vector<double> leaf_value_;
Guolin Ke's avatar
Guolin Ke committed
300
301
302
303
304
305
  /*! \brief DataCount of leaves */
  std::vector<data_size_t> leaf_count_;
  /*! \brief Output of non-leaf nodes */
  std::vector<double> internal_value_;
  /*! \brief DataCount of non-leaf nodes */
  std::vector<data_size_t> internal_count_;
Guolin Ke's avatar
Guolin Ke committed
306
  /*! \brief Depth for leaves */
Guolin Ke's avatar
Guolin Ke committed
307
  std::vector<int> leaf_depth_;
Guolin Ke's avatar
Guolin Ke committed
308
  double shrinkage_;
309
  bool has_categorical_;
Guolin Ke's avatar
Guolin Ke committed
310
311
};

312
inline double Tree::Predict(const double* feature_values) const {
Guolin Ke's avatar
Guolin Ke committed
313
314
315
316
317
318
  if (num_leaves_ > 1) {
    int leaf = GetLeaf(feature_values);
    return LeafOutput(leaf);
  } else {
    return 0.0f;
  }
Guolin Ke's avatar
Guolin Ke committed
319
320
}

321
inline int Tree::PredictLeafIndex(const double* feature_values) const {
Guolin Ke's avatar
Guolin Ke committed
322
323
324
325
326
327
  if (num_leaves_ > 1) {
    int leaf = GetLeaf(feature_values);
    return leaf;
  } else {
    return 0;
  }
wxchan's avatar
wxchan committed
328
329
}

330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
inline void Tree::ExtendPath(PathElement *unique_path, int unique_depth,
                                    double zero_fraction, double one_fraction, int feature_index) {
  unique_path[unique_depth].feature_index = feature_index;
  unique_path[unique_depth].zero_fraction = zero_fraction;
  unique_path[unique_depth].one_fraction = one_fraction;
  unique_path[unique_depth].pweight = (unique_depth == 0 ? 1 : 0);
  for (int i = unique_depth-1; i >= 0; i--) {
    unique_path[i+1].pweight += one_fraction*unique_path[i].pweight*(i+1)
                                / static_cast<double>(unique_depth+1);
    unique_path[i].pweight = zero_fraction*unique_path[i].pweight*(unique_depth-i)
                             / static_cast<double>(unique_depth+1);
  }
}

inline void Tree::UnwindPath(PathElement *unique_path, int unique_depth, int path_index) {
  const double one_fraction = unique_path[path_index].one_fraction;
  const double zero_fraction = unique_path[path_index].zero_fraction;
  double next_one_portion = unique_path[unique_depth].pweight;

  for (int i = unique_depth-1; i >= 0; --i) {
    if (one_fraction != 0) {
      const double tmp = unique_path[i].pweight;
      unique_path[i].pweight = next_one_portion*(unique_depth+1)
                               / static_cast<double>((i+1)*one_fraction);
      next_one_portion = tmp - unique_path[i].pweight*zero_fraction*(unique_depth-i)
                               / static_cast<double>(unique_depth+1);
    } else {
      unique_path[i].pweight = (unique_path[i].pweight*(unique_depth+1))
                               / static_cast<double>(zero_fraction*(unique_depth-i));
    }
  }

  for (int i = path_index; i < unique_depth; ++i) {
    unique_path[i].feature_index = unique_path[i+1].feature_index;
    unique_path[i].zero_fraction = unique_path[i+1].zero_fraction;
    unique_path[i].one_fraction = unique_path[i+1].one_fraction;
  }
}

inline double Tree::UnwoundPathSum(const PathElement *unique_path, int unique_depth, int path_index) {
  const double one_fraction = unique_path[path_index].one_fraction;
  const double zero_fraction = unique_path[path_index].zero_fraction;
  double next_one_portion = unique_path[unique_depth].pweight;
  double total = 0;
  for (int i = unique_depth-1; i >= 0; --i) {
    if (one_fraction != 0) {
      const double tmp = next_one_portion*(unique_depth+1)
                            / static_cast<double>((i+1)*one_fraction);
      total += tmp;
      next_one_portion = unique_path[i].pweight - tmp*zero_fraction*((unique_depth-i)
                         / static_cast<double>(unique_depth+1));
    } else {
      total += (unique_path[i].pweight/zero_fraction)/((unique_depth-i)
               / static_cast<double>(unique_depth+1));
    }
  }
  return total;
}

// recursive computation of SHAP values for a decision tree
inline void Tree::TreeSHAP(const double *feature_values, double *phi,
                           int node, int unique_depth,
                           PathElement *parent_unique_path, double parent_zero_fraction,
                           double parent_one_fraction, int parent_feature_index) const {

  // extend the unique path
  PathElement *unique_path = parent_unique_path + unique_depth;
  if (unique_depth > 0) std::copy(parent_unique_path, parent_unique_path+unique_depth, unique_path);
  ExtendPath(unique_path, unique_depth, parent_zero_fraction,
             parent_one_fraction, parent_feature_index);
  const int split_index = split_feature_[node];

  // leaf node
  if (node < 0) {
    for (int i = 1; i <= unique_depth; ++i) {
      const double w = UnwoundPathSum(unique_path, unique_depth, i);
      const PathElement &el = unique_path[i];
      phi[el.feature_index] += w*(el.one_fraction-el.zero_fraction)*leaf_value_[~node];
    }

  // internal node
  } else {
Guolin Ke's avatar
Guolin Ke committed
412
413
    const int hot_index = 
      decision_funs[GetDecisionType(decision_type_[node], kCategoricalMask)](feature_values[split_index], threshold_[node]);
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
    const int cold_index = (hot_index == left_child_[node] ? right_child_[node] : left_child_[node]);
    const double w = data_count(node);
    const double hot_zero_fraction = data_count(hot_index)/w;
    const double cold_zero_fraction = data_count(cold_index)/w;
    double incoming_zero_fraction = 1;
    double incoming_one_fraction = 1;

    // see if we have already split on this feature,
    // if so we undo that split so we can redo it for this node
    int path_index = 0;
    for (; path_index <= unique_depth; ++path_index) {
      if (unique_path[path_index].feature_index == split_index) break;
    }
    if (path_index != unique_depth+1) {
      incoming_zero_fraction = unique_path[path_index].zero_fraction;
      incoming_one_fraction = unique_path[path_index].one_fraction;
      UnwindPath(unique_path, unique_depth, path_index);
      unique_depth -= 1;
    }

    TreeSHAP(feature_values, phi, hot_index, unique_depth+1, unique_path,
             hot_zero_fraction*incoming_zero_fraction, incoming_one_fraction, split_index);

    TreeSHAP(feature_values, phi, cold_index, unique_depth+1, unique_path,
             cold_zero_fraction*incoming_zero_fraction, 0, split_index);
  }
}

inline void Tree::PredictContrib(const double* feature_values, int num_features, double *output) const {
  output[num_features] += ExpectedValue();

  // Run the recursion with preallocated space for the unique path data
  const int max_path_len = MaxDepth()+1;
  PathElement *unique_path_data = new PathElement[(max_path_len*(max_path_len+1))/2];
  TreeSHAP(feature_values, output, 0, 0, unique_path_data, 1, 1, -1);
  delete[] unique_path_data;
}

inline double Tree::ExpectedValue(int node) const {
  if (node >= 0) {
    const int l = left_child_[node];
    const int r = right_child_[node];
    return (data_count(l)*ExpectedValue(l) + data_count(r)*ExpectedValue(r))/data_count(node);
  } else {
    return LeafOutput(~node);
  }
}

inline int Tree::MaxDepth() const {
  int max_depth = 0;
  for (int i = 0; i < num_leaves(); ++i) {
    if (max_depth < leaf_depth_[i]) max_depth = leaf_depth_[i];
  }
  return max_depth;
}

470
inline int Tree::GetLeaf(const double* feature_values) const {
Guolin Ke's avatar
Guolin Ke committed
471
  int node = 0;
Guolin Ke's avatar
Guolin Ke committed
472
473
  if (has_categorical_) {
    while (node >= 0) {
Guolin Ke's avatar
Guolin Ke committed
474
475
      double fval = ConvertMissingValue(feature_values[split_feature_[node]], threshold_[node], decision_type_[node]);
      if (decision_funs[GetDecisionType(decision_type_[node], kCategoricalMask)](
Guolin Ke's avatar
Guolin Ke committed
476
        fval,
Guolin Ke's avatar
Guolin Ke committed
477
478
479
480
481
482
483
484
        threshold_[node])) {
        node = left_child_[node];
      } else {
        node = right_child_[node];
      }
    }
  } else {
    while (node >= 0) {
Guolin Ke's avatar
Guolin Ke committed
485
      double fval = ConvertMissingValue(feature_values[split_feature_[node]], threshold_[node], decision_type_[node]);
Guolin Ke's avatar
Guolin Ke committed
486
      if (NumericalDecision<double>(
Guolin Ke's avatar
Guolin Ke committed
487
        fval,
Guolin Ke's avatar
Guolin Ke committed
488
489
490
491
492
        threshold_[node])) {
        node = left_child_[node];
      } else {
        node = right_child_[node];
      }
Guolin Ke's avatar
Guolin Ke committed
493
494
495
496
497
498
499
    }
  }
  return ~node;
}

}  // namespace LightGBM

Guolin Ke's avatar
Guolin Ke committed
500
#endif   // LightGBM_TREE_H_