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 +2 -0
- data/cofi_cost.gemspec +1 -1
- data/lib/cofi_cost.rb +9 -6
- data/spec/cofi_cost_spec.rb +6 -5
- metadata +1 -1
data/.gitignore
CHANGED
data/cofi_cost.gemspec
CHANGED
data/lib/cofi_cost.rb
CHANGED
@@ -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
|
-
|
128
|
+
set_max_min_predictions(predicts)
|
128
129
|
end
|
129
130
|
|
130
|
-
def
|
131
|
-
|
132
|
-
|
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
|
|
data/spec/cofi_cost_spec.rb
CHANGED
@@ -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 "#
|
72
|
-
it "does not allow any prediction greater than self.max_rating" do
|
73
|
-
test = NArray[[6.0,4.0
|
74
|
-
@cofi.
|
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
|