-
Notifications
You must be signed in to change notification settings - Fork 3
/
normalizers.rb
executable file
·119 lines (91 loc) · 2.45 KB
/
normalizers.rb
1
2
3
4
5
6
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
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env ruby
require './similarity'
include Similarity
module Normalizer
$threshold = 0.3
$alpha = 1
$item_based_normalization = false
$normalizing_rating = true
def set_threshold(x)
$threshold = x
end
def set_normalization(x)
$normalizing_rating = x
end
def set_normalization_type(x)
$item_based_normalization = x
end
def set_alpha(x)
$alpha = x
end
def normalize_rating(rating, user, movie)
return normalize_rating_z_score(rating, user, movie)
end
def denormalize_rating(rating, user, movie)
return denormalize_rating_z_score(rating, user, movie)
end
def normalize_rating_z_score(rating, user, movie)
if $item_based_normalization
then
if $std_dev_item_rating[movie].abs > EPSILON
then
return (rating - $average_item_rating[movie]) / $std_dev_item_rating[movie]
else
return 0
end
else
if $std_dev_user_rating[user].abs > EPSILON
then
return (rating - $average_user_rating[user]) / $std_dev_user_rating[user]
else
return 0
end
end
end
def denormalize_rating_z_score(rating, user, movie)
if $item_based_normalization
then
return $average_item_rating[movie] + $std_dev_item_rating[movie] * rating
else
return $average_user_rating[user] + $std_dev_user_rating[user] * rating
end
end
def normalize_rating_mean_centering(rating, user, movie)
if $item_based_normalization
then
return rating - $average_item_rating[movie]
else
return rating - $average_user_rating[user]
end
end
def denormalize_rating_mean_centering(rating, user, movie)
if $item_based_normalization
then
return rating + $average_item_rating[movie]
else
return rating + $average_user_rating[user]
end
end
def get_rating(user, item)
if $normalizing_rating
then
return $normalized_rating[[user, item]]
else
return $rated_movies_per_user[[user, item]]
end
end
def calculate_similarity(vec1, vec2, weight=nil)
return cosine_rule(vec1, vec2, weight)
end
def compute_expected_rating(rating_list, similarity_list)
weighted_rating = dot_product(rating_list, similarity_list)
total_similarity = 0
similarity_list.each {|similarity| total_similarity += similarity.abs}
if total_similarity.abs > EPSILON
then
return weighted_rating.to_f / total_similarity.to_f
else
return 0
end
end
end