cofi_cost 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -1 +1,3 @@
1
1
  *.txt
2
+ *.swp
3
+ *.gem
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'cofi_cost'
3
- s.version = '0.0.8'
3
+ s.version = '0.0.9'
4
4
  s.date = '2013-11-18'
5
5
  s.summary = "Collaborative filtering"
6
6
  s.description = "Playground for collaborative filtering in Ruby using NArray and rb-gsl."
@@ -6,14 +6,15 @@ include GSL::MultiMin
6
6
 
7
7
  class CofiCost
8
8
 
9
- attr_accessor :ratings, :num_features, :regularization, :iterations, :features, :theta, :max_rating
9
+ attr_accessor :ratings, :num_features, :regularization, :iterations, :features, :theta, :max_rating, :min_rating
10
10
  attr_reader :boolean_rated, :num_tracks, :num_users, :ratings_mean, :ratings_norm, :predictions, :cost
11
11
 
12
- def initialize(ratings, num_features = 2, regularization = 1, iterations = 10, max_rating = 5, features = nil, theta = nil)
12
+ def initialize(ratings, num_features = 2, regularization = 1, iterations = 10, max_rating = 5, min_rating = 0, features = nil, theta = nil)
13
13
  @ratings = ratings.to_f # make sure it's a float for correct normalization
14
14
  @num_features = num_features
15
15
  @cost = 0
16
16
  @max_rating = max_rating
17
+ @min_rating = min_rating
17
18
  @boolean_rated = @ratings > 0 # return 0 for all rated and 1 for all unrated
18
19
  @boolean_unrated = @boolean_rated.eq 0 # return 1 for all unrated and 0 for all unrated
19
20
  @num_tracks = @ratings.shape[1] # @ratings is users x tracks
@@ -124,12 +125,14 @@ class CofiCost
124
125
 
125
126
  def calc_predictions
126
127
  predicts = NArray.ref(NMatrix.ref(@features) * NMatrix.ref(@theta.transpose(1,0))) + @ratings_mean
127
- set_max_predictions(predicts)
128
+ set_max_min_predictions(predicts)
128
129
  end
129
130
 
130
- def set_max_predictions(predicts)
131
- a = predicts > @max_rating
132
- predicts[a] = @max_rating
131
+ def set_max_min_predictions(predicts)
132
+ over_max = predicts > @max_rating
133
+ under_min = predicts < @min_rating
134
+ predicts[over_max] = @max_rating
135
+ predicts[under_min] = @min_rating
133
136
  predicts
134
137
  end
135
138
 
@@ -7,9 +7,10 @@ describe CofiCost do
7
7
  regularization = 1
8
8
  iterations = 10
9
9
  max_rating = 5
10
+ min_rating = 0
10
11
  theta = NArray[[0.28544,-1.68427,0.26294],[0.50501,-0.45465,0.31746],[-0.43192,-0.47880,0.84671],[0.72860,-0.27189,0.32684]]
11
12
  features = NArray[[1.048686,-0.400232,1.194119],[0.780851,-0.385626,0.521198],[0.641509,-0.547854,-0.083796],[0.453618,-0.800218,0.680481],[0.937538,0.106090,0.361953]]
12
- @cofi = CofiCost.new(ratings, num_features, regularization, iterations, max_rating, features, theta)
13
+ @cofi = CofiCost.new(ratings, num_features, regularization, iterations, max_rating, min_rating, features, theta)
13
14
  end
14
15
 
15
16
  describe "#normalize_ratings" do
@@ -68,10 +69,10 @@ describe CofiCost do
68
69
  end
69
70
  end
70
71
 
71
- describe "#set_max_predictions(predicts)" do
72
- it "does not allow any prediction greater than self.max_rating" do
73
- test = NArray[[6.0,4.0,0.0,0.0],[3.0,0.0,6.5,0.0],[4.0,0.0,0.0,0.0],[3.0,0.0,0.0,0.0],[3.0,0.0,0.0,0.0]]
74
- @cofi.set_max_predictions(test).to_a.should == [[5.0,4.0,0.0,0.0],[3.0,0.0,5.0,0.0],[4.0,0.0,0.0,0.0],[3.0,0.0,0.0,0.0],[3.0,0.0,0.0,0.0]]
72
+ describe "#set_max_min_predictions(predicts)" do
73
+ it "does not allow any prediction greater than self.max_rating or less than self.min_rating" do
74
+ test = NArray[[6.0,4.0,-1.0,0.0],[3.0,0.0,6.5,0.0],[4.0,0.0,0.0,0.0],[3.0,0.0,0.0,0.0],[3.0,0.0,0.0,0.0]]
75
+ @cofi.set_max_min_predictions(test).to_a.should == [[5.0,4.0,0.0,0.0],[3.0,0.0,5.0,0.0],[4.0,0.0,0.0,0.0],[3.0,0.0,0.0,0.0],[3.0,0.0,0.0,0.0]]
75
76
  end
76
77
  end
77
78
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cofi_cost
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: