1+ # coding=utf-8
12"""
2- Module containing evaluation functions suitable for judging the performance of a fitted
3- LightFM model.
3+ Module containing evaluation functions suitable for judging the performance of
4+ a fitted LightFM model.
45"""
56
67import numpy as np
78
89from ._lightfm_fast import (CSRMatrix ,
910 calculate_auc_from_rank )
1011
11-
1212__all__ = ['precision_at_k' ,
1313 'recall_at_k' ,
1414 'auc_score' ,
@@ -19,8 +19,8 @@ def precision_at_k(model, test_interactions, train_interactions=None,
1919 k = 10 , user_features = None , item_features = None ,
2020 preserve_rows = False , num_threads = 1 ):
2121 """
22- Measure the precision at k metric for a model: the fraction of known positives in the first k
23- positions of the ranked list of results.
22+ Measure the precision at k metric for a model: the fraction of known
23+ positives in the first k positions of the ranked list of results.
2424 A perfect score is 1.0.
2525
2626 Parameters
@@ -32,7 +32,7 @@ def precision_at_k(model, test_interactions, train_interactions=None,
3232 Non-zero entries representing known positives in the evaluation set.
3333 train_interactions: np.float32 csr_matrix of shape [n_users, n_items], optional
3434 Non-zero entries representing known positives in the train set. These
35- will be omitted from the score calulations to avoid re-recommending
35+ will be omitted from the score calculations to avoid re-recommending
3636 known positives.
3737 k: integer, optional
3838 The k parameter.
@@ -41,9 +41,10 @@ def precision_at_k(model, test_interactions, train_interactions=None,
4141 item_features: np.float32 csr_matrix of shape [n_items, n_item_features], optional
4242 Each row contains that item's weights over features.
4343 preserve_rows: boolean, optional
44- When False (default), the number of rows in the output will be equal to
45- the number of users with interactions in the evaluation set. When True,
46- the number of rows in the output will be equal to the number of users.
44+ When False (default), the number of rows in the output will be equal
45+ to the number of users with interactions in the evaluation set.
46+ When True, the number of rows in the output will be equal to the
47+ number of users.
4748 num_threads: int, optional
4849 Number of parallel computation threads to use. Should
4950 not be higher than the number of physical cores.
@@ -52,8 +53,8 @@ def precision_at_k(model, test_interactions, train_interactions=None,
5253 -------
5354
5455 np.array of shape [n_users with interactions or n_users,]
55- Numpy array containing precision@k scores for each user. If there are no interactions
56- for a given user the returned precision will be 0.
56+ Numpy array containing precision@k scores for each user. If there are
57+ no interactions for a given user the returned precision will be 0.
5758 """
5859
5960 ranks = model .predict_rank (test_interactions ,
@@ -76,9 +77,9 @@ def recall_at_k(model, test_interactions, train_interactions=None,
7677 k = 10 , user_features = None , item_features = None ,
7778 preserve_rows = False , num_threads = 1 ):
7879 """
79- Measure the recall at k metric for a model: the number of positive items in the first k
80- positions of the ranked list of results divided by the number of positive items
81- in the test period. A perfect score is 1.0.
80+ Measure the recall at k metric for a model: the number of positive items in
81+ the first k positions of the ranked list of results divided by the number
82+ of positive items in the test period. A perfect score is 1.0.
8283
8384 Parameters
8485 ----------
@@ -89,7 +90,7 @@ def recall_at_k(model, test_interactions, train_interactions=None,
8990 Non-zero entries representing known positives in the evaluation set.
9091 train_interactions: np.float32 csr_matrix of shape [n_users, n_items], optional
9192 Non-zero entries representing known positives in the train set. These
92- will be omitted from the score calulations to avoid re-recommending
93+ will be omitted from the score calculations to avoid re-recommending
9394 known positives.
9495 k: integer, optional
9596 The k parameter.
@@ -98,9 +99,10 @@ def recall_at_k(model, test_interactions, train_interactions=None,
9899 item_features: np.float32 csr_matrix of shape [n_items, n_item_features], optional
99100 Each row contains that item's weights over features.
100101 preserve_rows: boolean, optional
101- When False (default), the number of rows in the output will be equal to
102- the number of users with interactions in the evaluation set. When True,
103- the number of rows in the output will be equal to the number of users.
102+ When False (default), the number of rows in the output will be equal
103+ to the number of users with interactions in the evaluation set.
104+ When True, the number of rows in the output will be equal to the
105+ number of users.
104106 num_threads: int, optional
105107 Number of parallel computation threads to use. Should
106108 not be higher than the number of physical cores.
@@ -109,8 +111,9 @@ def recall_at_k(model, test_interactions, train_interactions=None,
109111 -------
110112
111113 np.array of shape [n_users with interactions or n_users,]
112- Numpy array containing recall@k scores for each user. If there are no interactions
113- for a given user having items in the test period, the returned recall will be 0.
114+ Numpy array containing recall@k scores for each user. If there are no
115+ interactions for a given user having items in the test period, the
116+ returned recall will be 0.
114117 """
115118
116119 ranks = model .predict_rank (test_interactions ,
@@ -135,8 +138,9 @@ def auc_score(model, test_interactions, train_interactions=None,
135138 user_features = None , item_features = None ,
136139 preserve_rows = False , num_threads = 1 ):
137140 """
138- Measure the ROC AUC metric for a model: the probability that a randomly chosen positive
139- example has a higher score than a randomly chosen negative example.
141+ Measure the ROC AUC metric for a model: the probability that a randomly
142+ chosen positive example has a higher score than a randomly chosen negative
143+ example.
140144 A perfect score is 1.0.
141145
142146 Parameters
@@ -148,16 +152,17 @@ def auc_score(model, test_interactions, train_interactions=None,
148152 Non-zero entries representing known positives in the evaluation set.
149153 train_interactions: np.float32 csr_matrix of shape [n_users, n_items], optional
150154 Non-zero entries representing known positives in the train set. These
151- will be omitted from the score calulations to avoid re-recommending
155+ will be omitted from the score calculations to avoid re-recommending
152156 known positives.
153157 user_features: np.float32 csr_matrix of shape [n_users, n_user_features], optional
154158 Each row contains that user's weights over features.
155159 item_features: np.float32 csr_matrix of shape [n_items, n_item_features], optional
156160 Each row contains that item's weights over features.
157161 preserve_rows: boolean, optional
158- When False (default), the number of rows in the output will be equal to
159- the number of users with interactions in the evaluation set. When True,
160- the number of rows in the output will be equal to the number of users.
162+ When False (default), the number of rows in the output will be equal
163+ to the number of users with interactions in the evaluation set.
164+ When True, the number of rows in the output will be equal to the
165+ number of users.
161166 num_threads: int, optional
162167 Number of parallel computation threads to use. Should
163168 not be higher than the number of physical cores.
@@ -166,21 +171,23 @@ def auc_score(model, test_interactions, train_interactions=None,
166171 -------
167172
168173 np.array of shape [n_users with interactions or n_users,]
169- Numpy array containing AUC scores for each user. If there are no interactions for a given
170- user the returned AUC will be 0.5.
174+ Numpy array containing AUC scores for each user. If there are no
175+ interactions for a given user the returned AUC will be 0.5.
171176 """
172177
173178 ranks = model .predict_rank (test_interactions ,
174179 train_interactions = train_interactions ,
175180 user_features = user_features ,
176181 item_features = item_features ,
177182 num_threads = num_threads )
183+
178184 assert np .all (ranks .data >= 0 )
179185
180186 auc = np .zeros (ranks .shape [0 ], dtype = np .float32 )
181187
182188 if train_interactions is not None :
183- num_train_positives = (np .squeeze (np .array (train_interactions .getnnz (axis = 1 ))
189+ num_train_positives = (np .squeeze (np .array (train_interactions
190+ .getnnz (axis = 1 ))
184191 .astype (np .int32 )))
185192 else :
186193 num_train_positives = np .zeros (test_interactions .shape [0 ],
@@ -217,16 +224,17 @@ def reciprocal_rank(model, test_interactions, train_interactions=None,
217224 Non-zero entries representing known positives in the evaluation set.
218225 train_interactions: np.float32 csr_matrix of shape [n_users, n_items], optional
219226 Non-zero entries representing known positives in the train set. These
220- will be omitted from the score calulations to avoid re-recommending
227+ will be omitted from the score calculations to avoid re-recommending
221228 known positives.
222229 user_features: np.float32 csr_matrix of shape [n_users, n_user_features], optional
223230 Each row contains that user's weights over features.
224231 item_features: np.float32 csr_matrix of shape [n_items, n_item_features], optional
225232 Each row contains that item's weights over features.
226233 preserve_rows: boolean, optional
227- When False (default), the number of rows in the output will be equal to
228- the number of users with interactions in the evaluation set. When True,
229- the number of rows in the output will be equal to the number of users.
234+ When False (default), the number of rows in the output will be equal
235+ to the number of users with interactions in the evaluation set.
236+ When True, the number of rows in the output will be equal to the
237+ number of users.
230238 num_threads: int, optional
231239 Number of parallel computation threads to use. Should
232240 not be higher than the number of physical cores.
@@ -236,7 +244,8 @@ def reciprocal_rank(model, test_interactions, train_interactions=None,
236244
237245 np.array of shape [n_users with interactions or n_users,]
238246 Numpy array containing reciprocal rank scores for each user.
239- If there are no interactions for a given user the returned value will be 0.0.
247+ If there are no interactions for a given user the returned value will
248+ be 0.0.
240249 """
241250
242251 ranks = model .predict_rank (test_interactions ,
0 commit comments