Class: Libmf::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/libmf/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Model

Returns a new instance of Model.



3
4
5
# File 'lib/libmf/model.rb', line 3

def initialize(**options)
  @options = options
end

Class Method Details

.load(path) ⇒ Object



53
54
55
56
57
# File 'lib/libmf/model.rb', line 53

def self.load(path)
  model = Model.new
  model.load_model(path)
  model
end

Instance Method Details

#accuracy(data) ⇒ Object



104
105
106
# File 'lib/libmf/model.rb', line 104

def accuracy(data)
  FFI.calc_accuracy(create_problem(data), model)
end

#auc(data, transpose) ⇒ Object



112
113
114
# File 'lib/libmf/model.rb', line 112

def auc(data, transpose)
  FFI.calc_auc(create_problem(data), model, transpose)
end

#biasObject



76
77
78
# File 'lib/libmf/model.rb', line 76

def bias
  model[:b]
end

#columnsObject



68
69
70
# File 'lib/libmf/model.rb', line 68

def columns
  model[:n]
end

#cv(data, folds: 5) ⇒ Object

Raises:



39
40
41
42
43
44
45
# File 'lib/libmf/model.rb', line 39

def cv(data, folds: 5)
  problem = create_problem(data)
  # 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

#factorsObject



72
73
74
# File 'lib/libmf/model.rb', line 72

def factors
  model[:k]
end

#fit(data, eval_set: nil) ⇒ Object

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/libmf/model.rb', line 7

def fit(data, eval_set: nil)
  train_set = create_problem(data)

  @model =
    if eval_set
      eval_set = create_problem(eval_set)
      param = self.param

      # LIBMF does not handle these cases
      if param[:fun] == 12
        if eval_set[:m] > train_set[:m]
          raise ArgumentError, "Eval set cannot have extra rows for one_class_l2 loss"
        end

        if eval_set[:n] > train_set[:n]
          raise ArgumentError, "Eval set cannot have extra columns for one_class_l2 loss"
        end
      end

      FFI.mf_train_with_validation(train_set, eval_set, param)
    else
      FFI.mf_train(train_set, self.param)
    end
  raise Error, "fit failed" if @model.null?

  nil
end

#gkl(data) ⇒ Object



96
97
98
# File 'lib/libmf/model.rb', line 96

def gkl(data)
  FFI.calc_gkl(create_problem(data), model)
end

#load_model(path) ⇒ Object

Raises:



59
60
61
62
# File 'lib/libmf/model.rb', line 59

def load_model(path)
  @model = FFI.mf_load_model(path)
  raise Error, "Cannot open model" if @model.null?
end

#logloss(data) ⇒ Object



100
101
102
# File 'lib/libmf/model.rb', line 100

def logloss(data)
  FFI.calc_logloss(create_problem(data), model)
end

#mae(data) ⇒ Object



92
93
94
# File 'lib/libmf/model.rb', line 92

def mae(data)
  FFI.calc_mae(create_problem(data), model)
end

#mpr(data, transpose) ⇒ Object



108
109
110
# File 'lib/libmf/model.rb', line 108

def mpr(data, transpose)
  FFI.calc_mpr(create_problem(data), model, transpose)
end

#p_factors(format: nil) ⇒ Object



80
81
82
# File 'lib/libmf/model.rb', line 80

def p_factors(format: nil)
  _factors(model[:p], rows, format)
end

#predict(row, column) ⇒ Object



35
36
37
# File 'lib/libmf/model.rb', line 35

def predict(row, column)
  FFI.mf_predict(model, row, column)
end

#q_factors(format: nil) ⇒ Object



84
85
86
# File 'lib/libmf/model.rb', line 84

def q_factors(format: nil)
  _factors(model[:q], columns, format)
end

#rmse(data) ⇒ Object



88
89
90
# File 'lib/libmf/model.rb', line 88

def rmse(data)
  FFI.calc_rmse(create_problem(data), model)
end

#rowsObject



64
65
66
# File 'lib/libmf/model.rb', line 64

def rows
  model[:m]
end

#save_model(path) ⇒ Object Also known as: save

Raises:



47
48
49
50
# File 'lib/libmf/model.rb', line 47

def save_model(path)
  status = FFI.mf_save_model(model, path)
  raise Error, "Cannot save model" if status != 0
end