Class: Cmfrec::Recommender

Inherits:
Object
  • Object
show all
Defined in:
lib/cmfrec/recommender.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factors: 8, epochs: 10, verbose: true, user_bias: true, item_bias: true, add_implicit_features: false) ⇒ Recommender

Returns a new instance of Recommender.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/cmfrec/recommender.rb', line 5

def initialize(factors: 8, epochs: 10, verbose: true, user_bias: true, item_bias: true, add_implicit_features: false)
  set_params(
    k: factors,
    niter: epochs,
    verbose: verbose,
    user_bias: user_bias,
    item_bias: item_bias,
    add_implicit_features: add_implicit_features
  )

  @fit = false
  @user_map = {}
  @item_map = {}
  @user_info_map = {}
  @item_info_map = {}
end

Instance Attribute Details

#global_meanObject (readonly)

Returns the value of attribute global_mean.



3
4
5
# File 'lib/cmfrec/recommender.rb', line 3

def global_mean
  @global_mean
end

Instance Method Details

#fit(train_set, user_info: nil, item_info: nil) ⇒ Object



22
23
24
25
# File 'lib/cmfrec/recommender.rb', line 22

def fit(train_set, user_info: nil, item_info: nil)
  reset
  partial_fit(train_set, user_info: , item_info: item_info)
end

#item_bias(item_id = nil) ⇒ Object



108
109
110
# File 'lib/cmfrec/recommender.rb', line 108

def item_bias(item_id = nil)
  read_bias(@bias_b, item_id, @item_map) if @bias_b
end

#item_factors(item_id = nil) ⇒ Object



100
101
102
# File 'lib/cmfrec/recommender.rb', line 100

def item_factors(item_id = nil)
  read_factors(@b, [@n, @n_i].max, @k_item + @k + @k_main, item_id, @item_map)
end

#item_idsObject



92
93
94
# File 'lib/cmfrec/recommender.rb', line 92

def item_ids
  @item_map.keys
end

#new_user_recs(data, count: 5, user_info: nil, item_ids: nil) ⇒ Object



81
82
83
84
85
86
# File 'lib/cmfrec/recommender.rb', line 81

def new_user_recs(data, count: 5, user_info: nil, item_ids: nil)
  check_fit

  a_vec, a_bias, rated = factors_warm(data, user_info: )
  top_n(a_vec: a_vec, a_bias: a_bias, count: count, rated: rated, item_ids: item_ids)
end

#predict(data) ⇒ Object



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
# File 'lib/cmfrec/recommender.rb', line 27

def predict(data)
  check_fit

  data = to_dataset(data)

  u = data.map { |v| @user_map[v[:user_id]] || @user_map.size }
  i = data.map { |v| @item_map[v[:item_id]] || @item_map.size }

  row = int_ptr(u)
  col = int_ptr(i)
  n_predict = data.size
  predicted = Fiddle::Pointer.malloc(n_predict * Fiddle::SIZEOF_DOUBLE)

  if @implicit
    check_status FFI.predict_X_old_collective_implicit(
      row, col, predicted, n_predict,
      @a, @b,
      @k, @k_user, @k_item, @k_main,
      @m, @n,
      @nthreads
    )
  else
    check_status FFI.predict_X_old_collective_explicit(
      row, col, predicted, n_predict,
      @a, @bias_a,
      @b, @bias_b,
      @global_mean,
      @k, @k_user, @k_item, @k_main,
      @m, @n,
      @nthreads
    )
  end

  predictions = real_array(predicted)
  predictions.map! { |v| v.nan? ? @global_mean : v } if @implicit
  predictions
end

#similar_items(item_id, count: 5) ⇒ Object Also known as: item_recs



112
113
114
115
# File 'lib/cmfrec/recommender.rb', line 112

def similar_items(item_id, count: 5)
  check_fit
  similar(item_id, @item_map, item_factors, count, item_index)
end

#similar_users(user_id, count: 5) ⇒ Object



118
119
120
121
# File 'lib/cmfrec/recommender.rb', line 118

def similar_users(user_id, count: 5)
  check_fit
  similar(user_id, @user_map, user_factors, count, user_index)
end

#user_bias(user_id = nil) ⇒ Object



104
105
106
# File 'lib/cmfrec/recommender.rb', line 104

def user_bias(user_id = nil)
  read_bias(@bias_a, user_id, @user_map) if @bias_a
end

#user_factors(user_id = nil) ⇒ Object



96
97
98
# File 'lib/cmfrec/recommender.rb', line 96

def user_factors(user_id = nil)
  read_factors(@a, [@m, @m_u].max, @k_user + @k + @k_main, user_id, @user_map)
end

#user_idsObject



88
89
90
# File 'lib/cmfrec/recommender.rb', line 88

def user_ids
  @user_map.keys
end

#user_recs(user_id, count: 5, item_ids: nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cmfrec/recommender.rb', line 65

def user_recs(user_id, count: 5, item_ids: nil)
  check_fit
  user = @user_map[user_id]

  if user
    a_vec = @a[user * @k * Fiddle::SIZEOF_DOUBLE, @k * Fiddle::SIZEOF_DOUBLE]
    a_bias = @bias_a ? @bias_a[user * Fiddle::SIZEOF_DOUBLE, Fiddle::SIZEOF_DOUBLE].unpack1("d") : 0
    # @rated[user] will be nil for recommenders saved before 0.1.5
    top_n(a_vec: a_vec, a_bias: a_bias, count: count, rated: (@rated[user] || {}).keys, item_ids: item_ids)
  else
    # no items if user is unknown
    # TODO maybe most popular items
    []
  end
end