lib/libmf/model.rb in libmf-0.2.4 vs lib/libmf/model.rb in libmf-0.2.5
- old
+ new
@@ -12,29 +12,35 @@
eval_set = create_problem(eval_set)
FFI.mf_train_with_validation(train_set, eval_set, param)
else
FFI.mf_train(train_set, param)
end
+ raise Error, "fit failed" if @model.null?
nil
end
def predict(row, column)
FFI.mf_predict(model, row, column)
end
def cv(data, folds: 5)
problem = create_problem(data)
- FFI.mf_cross_validation(problem, folds, param)
+ # TODO update fork to differentiate between bad parameters and zero error
+ res = FFI.mf_cross_validation(problem, folds, param)
+ raise Error, "cv failed" if res == 0
+ res
end
def save_model(path)
- FFI.mf_save_model(model, path)
+ status = FFI.mf_save_model(model, path)
+ raise Error, "Cannot save model" if status != 0
end
def load_model(path)
@model = FFI.mf_load_model(path)
+ raise Error, "Cannot open model" if @model.null?
end
def rows
model[:m]
end
@@ -78,10 +84,26 @@
end
def param
param = FFI.mf_get_default_param
options = @options.dup
+
+ if options[:loss].is_a?(Symbol)
+ loss_map = {
+ real_l2: 0,
+ real_l1: 1,
+ real_kl: 2,
+ binary_log: 5,
+ binary_l2: 6,
+ binary_l1: 7,
+ one_class_row: 10,
+ one_class_col: 11,
+ one_class_l2: 12
+ }
+ options[:loss] = loss_map[options[:loss]] || (raise ArgumentError, "Unknown loss")
+ end
+
# silence insufficient blocks warning with default params
options[:bins] ||= 25 unless options[:nr_bins]
options[:copy_data] = false unless options.key?(:copy_data)
options_map = {
:loss => :fun,
@@ -103,9 +125,13 @@
def create_problem(data)
if data.is_a?(String)
# need to expand path so it's absolute
return FFI.mf_read_problem(File.expand_path(data))
+ end
+
+ if data.is_a?(Matrix)
+ data = data.data
end
raise Error, "No data" if data.empty?
# TODO do in C for better performance