Class: LightGBM::Dataset

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, label: nil, weight: nil, group: nil, params: nil, reference: nil, used_indices: nil, categorical_feature: "auto", feature_names: nil) ⇒ Dataset

Returns a new instance of Dataset.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/lightgbm/dataset.rb', line 5

def initialize(data, label: nil, weight: nil, group: nil, params: nil, reference: nil, used_indices: nil, categorical_feature: "auto", feature_names: nil)
  @data = data
  @label = label
  @weight = weight
  @group = group
  @params = params
  @reference = reference
  @used_indices = used_indices
  @categorical_feature = categorical_feature
  @feature_names = feature_names

  construct
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/lightgbm/dataset.rb', line 3

def data
  @data
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/lightgbm/dataset.rb', line 3

def params
  @params
end

Class Method Details

.finalize(pointer) ⇒ Object



101
102
103
104
# File 'lib/lightgbm/dataset.rb', line 101

def self.finalize(pointer)
  # must use proc instead of stabby lambda
  proc { FFI.LGBM_DatasetFree(pointer) }
end

Instance Method Details

#feature_namesObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lightgbm/dataset.rb', line 27

def feature_names
  # must preallocate space
  num_feature_names = ::FFI::MemoryPointer.new(:int)
  out_buffer_len = ::FFI::MemoryPointer.new(:size_t)
  len = 1000
  out_strs = ::FFI::MemoryPointer.new(:pointer, len)
  buffer_len = 255
  str_ptrs = len.times.map { ::FFI::MemoryPointer.new(:char, buffer_len) }
  out_strs.write_array_of_pointer(str_ptrs)
  check_result FFI.LGBM_DatasetGetFeatureNames(handle_pointer, len, num_feature_names, buffer_len, out_buffer_len, out_strs)
  str_ptrs[0, num_feature_names.read_int].map(&:read_string)
end

#feature_names=(feature_names) ⇒ Object



55
56
57
58
59
60
# File 'lib/lightgbm/dataset.rb', line 55

def feature_names=(feature_names)
  @feature_names = feature_names
  c_feature_names = ::FFI::MemoryPointer.new(:pointer, feature_names.size)
  c_feature_names.write_array_of_pointer(feature_names.map { |v| ::FFI::MemoryPointer.from_string(v) })
  check_result FFI.LGBM_DatasetSetFeatureNames(handle_pointer, c_feature_names, feature_names.size)
end

#group=(group) ⇒ Object



50
51
52
53
# File 'lib/lightgbm/dataset.rb', line 50

def group=(group)
  @group = group
  set_field("group", group, type: :int32)
end

#handle_pointerObject



97
98
99
# File 'lib/lightgbm/dataset.rb', line 97

def handle_pointer
  @handle.read_pointer
end

#labelObject



19
20
21
# File 'lib/lightgbm/dataset.rb', line 19

def label
  field("label")
end

#label=(label) ⇒ Object



40
41
42
43
# File 'lib/lightgbm/dataset.rb', line 40

def label=(label)
  @label = label
  set_field("label", label)
end

#num_dataObject



71
72
73
74
75
# File 'lib/lightgbm/dataset.rb', line 71

def num_data
  out = ::FFI::MemoryPointer.new(:int)
  check_result FFI.LGBM_DatasetGetNumData(handle_pointer, out)
  out.read_int
end

#num_featureObject



77
78
79
80
81
# File 'lib/lightgbm/dataset.rb', line 77

def num_feature
  out = ::FFI::MemoryPointer.new(:int)
  check_result FFI.LGBM_DatasetGetNumFeature(handle_pointer, out)
  out.read_int
end

#reference=(reference) ⇒ Object

TODO only update reference if not in chain



63
64
65
66
67
68
69
# File 'lib/lightgbm/dataset.rb', line 63

def reference=(reference)
  if reference != @reference
    @reference = reference
    free_handle
    construct
  end
end

#save_binary(filename) ⇒ Object



83
84
85
# File 'lib/lightgbm/dataset.rb', line 83

def save_binary(filename)
  check_result FFI.LGBM_DatasetSaveBinary(handle_pointer, filename)
end

#subset(used_indices, params: nil) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/lightgbm/dataset.rb', line 87

def subset(used_indices, params: nil)
  # categorical_feature passed via params
  params ||= self.params
  Dataset.new(nil,
    params: params,
    reference: self,
    used_indices: used_indices
  )
end

#weightObject



23
24
25
# File 'lib/lightgbm/dataset.rb', line 23

def weight
  field("weight")
end

#weight=(weight) ⇒ Object



45
46
47
48
# File 'lib/lightgbm/dataset.rb', line 45

def weight=(weight)
  @weight = weight
  set_field("weight", weight)
end