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



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

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

Instance Method Details

#accuracy(data) ⇒ Object



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

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

#auc(data, transpose) ⇒ Object



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

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

#biasObject



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

def bias
  model[:b]
end

#columnsObject



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

def columns
  model[:n]
end

#cv(data, folds: 5) ⇒ Object

Raises:



26
27
28
29
30
31
32
# File 'lib/libmf/model.rb', line 26

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



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

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
# 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)
      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

#gkl(data) ⇒ Object



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

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

#load_model(path) ⇒ Object

Raises:



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

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

#logloss(data) ⇒ Object



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

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

#mae(data) ⇒ Object



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

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

#mpr(data, transpose) ⇒ Object



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

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

#p_factors(format: nil) ⇒ Object



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

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

#predict(row, column) ⇒ Object



22
23
24
# File 'lib/libmf/model.rb', line 22

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

#q_factors(format: nil) ⇒ Object



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

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

#rmse(data) ⇒ Object



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

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

#rowsObject



51
52
53
# File 'lib/libmf/model.rb', line 51

def rows
  model[:m]
end

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

Raises:



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

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