Skip to content

Commit

Permalink
Fixed the recommenders to return the recommendations as list.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelcaraciolo committed Oct 10, 2011
1 parent cac20f6 commit 15f4056
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 62 deletions.
52 changes: 24 additions & 28 deletions scikits/crab/recommenders/knn/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,10 @@ class ItemBasedRecommender(ItemRecommender):
>>> recsys = ItemBasedRecommender(model, similarity, items_strategy)
>>> #Return the recommendations for the given user.
>>> recsys.recommend('Leopoldo Pires')
array(['Just My Luck', 'You, Me and Dupree'],\
dtype='|S18')
['Just My Luck', 'You, Me and Dupree']
>>> #Return the 2 explanations for the given recommendation.
>>> recsys.recommended_because('Leopoldo Pires', 'Just My Luck',2)
array(['The Night Listener', 'Superman Returns'],\
dtype='|S18')
['The Night Listener', 'Superman Returns']
Notes
-----------
Expand Down Expand Up @@ -245,21 +243,21 @@ def _top_matches(self, source_id, target_ids, how_many=None, **params):

preferences = estimate_preferences(source_id, target_ids)

preferences = preferences[~np.isnan(preferences)]
preference_values = preferences[~np.isnan(preferences)]
target_ids = target_ids[~np.isnan(preferences)]

sorted_preferences = np.lexsort((preferences,))[::-1]
sorted_preferences = np.lexsort((preference_values,))[::-1]

sorted_preferences = sorted_preferences[0:how_many] \
if how_many and sorted_preferences.size > how_many \
else sorted_preferences

if self.with_preference:
top_n_recs = np.array([(target_ids[ind], \
preferences[ind]) for ind in sorted_preferences])
top_n_recs = [(target_ids[ind], \
preferences[ind]) for ind in sorted_preferences]
else:
top_n_recs = np.array([target_ids[ind]
for ind in sorted_preferences])
top_n_recs = [target_ids[ind]
for ind in sorted_preferences]

return top_n_recs

Expand Down Expand Up @@ -336,11 +334,11 @@ def recommended_because(self, user_id, item_id, how_many=None, **params):
else sorted_preferences

if self.with_preference:
top_n_recs = np.array([(item_ids[ind], \
prefs[ind]) for ind in sorted_preferences])
top_n_recs = [(item_ids[ind], \
prefs[ind]) for ind in sorted_preferences]
else:
top_n_recs = np.array([item_ids[ind]
for ind in sorted_preferences])
top_n_recs = [item_ids[ind]
for ind in sorted_preferences]

return top_n_recs

Expand Down Expand Up @@ -425,12 +423,10 @@ class UserBasedRecommender(UserRecommender):
>>> recsys = UserBasedRecommender(model, similarity, nhood_strategy)
>>> #Return the recommendations for the given user.
>>> recsys.recommend('Leopoldo Pires')
array(['Just My Luck', 'You, Me and Dupree'],\
dtype='|S18')
['Just My Luck', 'You, Me and Dupree']
>>> #Return the 2 explanations for the given recommendation.
>>> recsys.recommended_because('Leopoldo Pires', 'Just My Luck',2)
array(['Lorena Abreu', 'Marcel Caraciolo'],\
dtype='|S16')
['Lorena Abreu', 'Marcel Caraciolo']
Notes
-----------
Expand Down Expand Up @@ -635,21 +631,21 @@ def _top_matches(self, source_id, target_ids, how_many=None, **params):

preferences = estimate_preferences(source_id, target_ids)

preferences = preferences[~np.isnan(preferences)]
preference_values = preferences[~np.isnan(preferences)]
target_ids = target_ids[~np.isnan(preferences)]

sorted_preferences = np.lexsort((preferences,))[::-1]
sorted_preferences = np.lexsort((preference_values,))[::-1]

sorted_preferences = sorted_preferences[0:how_many] \
if how_many and sorted_preferences.size > how_many \
else sorted_preferences

if self.with_preference:
top_n_recs = np.array([(target_ids[ind], \
preferences[ind]) for ind in sorted_preferences])
top_n_recs = [(target_ids[ind], \
preferences[ind]) for ind in sorted_preferences]
else:
top_n_recs = np.array([target_ids[ind]
for ind in sorted_preferences])
top_n_recs = [target_ids[ind]
for ind in sorted_preferences]

return top_n_recs

Expand Down Expand Up @@ -703,10 +699,10 @@ def recommended_because(self, user_id, item_id, how_many=None, **params):
else sorted_preferences

if self.with_preference:
top_n_recs = np.array([(user_ids[ind], \
prefs[ind]) for ind in sorted_preferences])
top_n_recs = [(user_ids[ind], \
prefs[ind]) for ind in sorted_preferences]
else:
top_n_recs = np.array([user_ids[ind]
for ind in sorted_preferences])
top_n_recs = [user_ids[ind]
for ind in sorted_preferences]

return top_n_recs
52 changes: 24 additions & 28 deletions scikits/crab/recommenders/knn/tests/test_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,12 @@ def test_recommend_ItemBasedRecommender():
assert_array_equal(np.array([]), recsys.recommend('Maria Gabriela'))

#with_preference
#recsys = ItemBasedRecommender(matrix_model, similarity, items_strategy, True, True)
#assert_array_equal(np.array([('Just My Luck', 3.20597319063), \
# ('You, Me and Dupree', 3.14717875510)]), \
# recsys.recommend('Leopoldo Pires'))
recsys = ItemBasedRecommender(matrix_model, similarity, items_strategy, True, True)
assert_equals('Just My Luck', recsys.recommend('Leopoldo Pires')[0][0])
assert_equals('You, Me and Dupree', recsys.recommend('Leopoldo Pires')[1][0])

assert_almost_equals(3.20597, recsys.recommend('Leopoldo Pires')[0][1], 2)
assert_almost_equals(3.147178755, recsys.recommend('Leopoldo Pires')[1][1], 2)

similarity = ItemSimilarity(boolean_matrix_model, jaccard_coefficient)
#Empty Recommendation
Expand All @@ -331,10 +333,12 @@ def test_recommend_ItemBasedRecommender():
assert_array_equal(np.array([]), recsys.recommend('Maria Gabriela'))

#with_preference
#recsys = ItemBasedRecommender(boolean_matrix_model, similarity, items_strategy, True, True)
#assert_array_equal(np.array([('Just My Luck', 3.20597), \
# ('You, Me and Dupree', 3.1471)]), \
# recsys.recommend('Leopoldo Pires'))
recsys = ItemBasedRecommender(boolean_matrix_model, similarity, items_strategy, True, True)
assert_equals('You, Me and Dupree', recsys.recommend('Leopoldo Pires')[0][0])
assert_equals('Just My Luck', recsys.recommend('Leopoldo Pires')[1][0])

assert_almost_equals(1.0, recsys.recommend('Leopoldo Pires')[0][1], 2)
assert_almost_equals(1.0, recsys.recommend('Leopoldo Pires')[1][1], 2)


def test_recommend_UserBasedRecommender():
Expand Down Expand Up @@ -363,10 +367,12 @@ def test_recommend_UserBasedRecommender():
assert_array_equal(np.array([]), recsys.recommend('Maria Gabriela'))

#with_preference
#recsys = ItemBasedRecommender(matrix_model, similarity, items_strategy, True, True)
#assert_array_equal(np.array([('Just My Luck', 3.20597319063), \
# ('You, Me and Dupree', 3.14717875510)]), \
# recsys.recommend('Leopoldo Pires'))
recsys = UserBasedRecommender(matrix_model, similarity, nhood_strategy, True, True)
assert_equals('Just My Luck', recsys.recommend('Leopoldo Pires')[0][0])
assert_equals('You, Me and Dupree', recsys.recommend('Leopoldo Pires')[1][0])

assert_almost_equals(2.456743361464, recsys.recommend('Leopoldo Pires')[0][1], 2)
assert_almost_equals(2.453379, recsys.recommend('Leopoldo Pires')[1][1], 2)

similarity = UserSimilarity(boolean_matrix_model, jaccard_coefficient)
#Empty Recommendation
Expand All @@ -392,10 +398,12 @@ def test_recommend_UserBasedRecommender():
assert_array_equal(np.array([]), recsys.recommend('Maria Gabriela'))

#with_preference
#recsys = ItemBasedRecommender(boolean_matrix_model, similarity, items_strategy, True, True)
#assert_array_equal(np.array([('Just My Luck', 3.20597), \
# ('You, Me and Dupree', 3.1471)]), \
# recsys.recommend('Leopoldo Pires'))
recsys = UserBasedRecommender(boolean_matrix_model, similarity, nhood_strategy, True, True)
assert_equals('You, Me and Dupree', recsys.recommend('Leopoldo Pires')[0][0])
assert_equals('Just My Luck', recsys.recommend('Leopoldo Pires')[1][0])

assert_almost_equals(1.0, recsys.recommend('Leopoldo Pires')[0][1], 2)
assert_almost_equals(1.0, recsys.recommend('Leopoldo Pires')[1][1], 2)


def test_recommend_because_ItemBasedRecommender():
Expand Down Expand Up @@ -449,11 +457,6 @@ def test_recommend_because_ItemBasedRecommender():
assert_array_equal(np.array([]), \
recsys.recommended_because('Maria Gabriela', 'Just My Luck', 2))

#with_preference
#recsys = ItemBasedRecommender(boolean_matrix_model, similarity, items_strategy, True, True)
#assert_array_equal(np.array([('The Night Listener', 1.0), \
# ('Superman Returns', 1.0)]), \
# recsys.recommended_because('Leopoldo Pires', 'Just My Luck', 2))


def test_recommend_because_UserBasedRecommender():
Expand Down Expand Up @@ -506,10 +509,3 @@ def test_recommend_because_UserBasedRecommender():
recsys = UserBasedRecommender(boolean_matrix_model, similarity, nhood_strategy)
assert_array_equal(np.array([]), \
recsys.recommended_because('Maria Gabriela', 'Just My Luck', 2))

#with_preference
#recsys = ItemBasedRecommender(boolean_matrix_model, similarity, items_strategy, True, True)
#assert_array_equal(np.array([('The Night Listener', 1.0), \
# ('Superman Returns', 1.0)]), \
# recsys.recommended_because('Leopoldo Pires', 'Just My Luck', 2))

12 changes: 6 additions & 6 deletions scikits/crab/recommenders/svd/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ class MatrixFactorBasedRecommender(SVDRecommender):
n_features=2)
>>> #Return the recommendations for the given user.
>>> recsys.recommend('Leopoldo Pires')
array(['Just My Luck', 'You, Me and Dupree'], \
dtype='|S18')
['Just My Luck', 'You, Me and Dupree']
Notes
-----------
Expand Down Expand Up @@ -383,10 +383,10 @@ def _top_matches(self, source_id, target_ids, how_many=None, **params):
else sorted_preferences

if self.with_preference:
top_n_recs = np.array([(target_ids[ind], \
preferences[ind]) for ind in sorted_preferences])
top_n_recs = [(target_ids[ind], \
preferences[ind]) for ind in sorted_preferences]
else:
top_n_recs = np.array([target_ids[ind]
for ind in sorted_preferences])
top_n_recs = [target_ids[ind]
for ind in sorted_preferences]

return top_n_recs

0 comments on commit 15f4056

Please sign in to comment.