Class: LightGBM::InnerPredictor

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/lightgbm/inner_predictor.rb

Constant Summary collapse

MAX_INT32 =
(1 << 31) - 1

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(booster, pred_parameter) ⇒ InnerPredictor

Returns a new instance of InnerPredictor.



7
8
9
10
11
12
13
14
# File 'lib/lightgbm/inner_predictor.rb', line 7

def initialize(booster, pred_parameter)
  @handle = booster.instance_variable_get(:@handle)
  @pandas_categorical = booster.instance_variable_get(:@pandas_categorical)
  @pred_parameter = params_str(pred_parameter)

  # keep booster for cached_feature_name
  @booster = booster
end

Class Method Details

.from_booster(booster, pred_parameter) ⇒ Object



16
17
18
# File 'lib/lightgbm/inner_predictor.rb', line 16

def self.from_booster(booster, pred_parameter)
  new(booster, pred_parameter)
end

Instance Method Details

#predict(data, start_iteration: 0, num_iteration: -1,, raw_score: false, pred_leaf: false, pred_contrib: false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/lightgbm/inner_predictor.rb', line 20

def predict(data, start_iteration: 0, num_iteration: -1, raw_score: false, pred_leaf: false, pred_contrib: false)
  if data.is_a?(Dataset)
    raise TypeError, "Cannot use Dataset instance for prediction, please use raw data instead"
  end

  predict_type = FFI::C_API_PREDICT_NORMAL
  if raw_score
    predict_type = FFI::C_API_PREDICT_RAW_SCORE
  end
  if pred_leaf
    predict_type = FFI::C_API_PREDICT_LEAF_INDEX
  end
  if pred_contrib
    predict_type = FFI::C_API_PREDICT_CONTRIB
  end

  if daru?(data)
    data = data[*cached_feature_name].map_rows(&:to_a)
    singular = false
  elsif data.is_a?(Hash) # sort feature.values to match the order of model.feature_name
    data = [sorted_feature_values(data)]
    singular = true
  elsif data.is_a?(Array) && data.first.is_a?(Hash) # on multiple elems, if 1st is hash, assume they all are
    data = data.map(&method(:sorted_feature_values))
    singular = false
  elsif rover?(data)
    # TODO improve performance
    data = data[cached_feature_name].to_numo.to_a
    singular = false
  else
    data = data.to_a
    singular = !data.first.is_a?(Array)
    data = [data] if singular
    check_2d_array(data)
    data = data.map(&:dup) if @pandas_categorical&.any?
  end

  if @pandas_categorical&.any?
    apply_pandas_categorical(
      data,
      @booster.params["categorical_feature"],
      @pandas_categorical
    )
  end

  preds, nrow =
    pred_for_array(
      data,
      start_iteration,
      num_iteration,
      predict_type
    )

  if pred_leaf
    preds = preds.map(&:to_i)
  end

  if preds.size != nrow
    if preds.size % nrow == 0
      preds = preds.each_slice(preds.size / nrow).to_a
    else
      raise Error, "Length of predict result (#{preds.size}) cannot be divide nrow (#{nrow})"
    end
  end

  singular ? preds.first : preds
end