Ejemplo n.º 1
0
def test_all_other_items_ItemBasedRecommender():
    items_strategy = AllPossibleItemsStrategy()
    similarity = ItemSimilarity(matrix_model, euclidean_distances)
    recsys = ItemBasedRecommender(matrix_model, similarity, items_strategy)

    assert_array_equal(np.array(['Lady in the Water']),
                       recsys.all_other_items('Lorena Abreu'))
    assert_array_equal(np.array([], dtype='|S'),
                       recsys.all_other_items('Marcel Caraciolo'))
    assert_array_equal(
        np.array([
            'Just My Luck', 'Lady in the Water', 'Snakes on a Plane',
            'Superman Returns', 'The Night Listener', 'You, Me and Dupree'
        ]), recsys.all_other_items('Maria Gabriela'))

    similarity = ItemSimilarity(boolean_matrix_model, jaccard_coefficient)
    recsys = ItemBasedRecommender(boolean_matrix_model, similarity,
                                  items_strategy)

    assert_array_equal(np.array(['Lady in the Water']),
                       recsys.all_other_items('Lorena Abreu'))
    assert_array_equal(np.array([], dtype='|S'),
                       recsys.all_other_items('Marcel Caraciolo'))
    assert_array_equal(
        np.array([
            'Just My Luck', 'Lady in the Water', 'Snakes on a Plane',
            'Superman Returns', 'The Night Listener', 'You, Me and Dupree'
        ]), recsys.all_other_items('Maria Gabriela'))
Ejemplo n.º 2
0
def test_recommend_because_ItemBasedRecommender():
    items_strategy = ItemsNeighborhoodStrategy()
    similarity = ItemSimilarity(matrix_model, euclidean_distances)
    #Full Recommendation Because
    recsys = ItemBasedRecommender(matrix_model, similarity, items_strategy)
    assert_array_equal(np.array(['The Night Listener', 'Superman Returns', \
    'Snakes on a Plane', 'Lady in the Water']), \
        recsys.recommended_because('Leopoldo Pires', 'Just My Luck'))
    #over-items
    recsys = ItemBasedRecommender(matrix_model, similarity, items_strategy)
    assert_array_equal(np.array(['The Night Listener', 'Superman Returns', \
    'Snakes on a Plane', 'Lady in the Water']), \
        recsys.recommended_because('Leopoldo Pires', 'Just My Luck', 20))
    #Semi
    recsys = ItemBasedRecommender(matrix_model, similarity, items_strategy)
    assert_array_equal(np.array(['The Night Listener', 'Superman Returns']), \
        recsys.recommended_because('Leopoldo Pires', 'Just My Luck', 2))

    #Non-Existing
    recsys = ItemBasedRecommender(matrix_model, similarity, items_strategy)
    assert_array_equal(np.array([]), \
        recsys.recommended_because('Maria Gabriela', 'Just My Luck', 2))

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

    #boolean_matrix_model
    similarity = ItemSimilarity(boolean_matrix_model, jaccard_coefficient)
    #Full Recommendation Because
    recsys = ItemBasedRecommender(boolean_matrix_model, similarity,
                                  items_strategy)
    assert_array_equal(np.array(['The Night Listener', 'Superman Returns', \
    'Snakes on a Plane', 'Lady in the Water']), \
        recsys.recommended_because('Leopoldo Pires', 'Just My Luck'))
    #over-items
    recsys = ItemBasedRecommender(boolean_matrix_model, similarity,
                                  items_strategy)
    assert_array_equal(np.array(['The Night Listener', 'Superman Returns', \
    'Snakes on a Plane', 'Lady in the Water']), \
        recsys.recommended_because('Leopoldo Pires', 'Just My Luck', 20))
    #Semi
    recsys = ItemBasedRecommender(boolean_matrix_model, similarity,
                                  items_strategy)
    assert_array_equal(np.array(['The Night Listener', 'Superman Returns']), \
        recsys.recommended_because('Leopoldo Pires', 'Just My Luck', 2))

    #Non-Existing
    recsys = ItemBasedRecommender(boolean_matrix_model, similarity,
                                  items_strategy)
    assert_array_equal(np.array([]), \
        recsys.recommended_because('Maria Gabriela', 'Just My Luck', 2))
Ejemplo n.º 3
0
def test_create_ItemBasedRecommender():
    items_strategy = AllPossibleItemsStrategy()
    similarity = ItemSimilarity(matrix_model, euclidean_distances)
    recsys = ItemBasedRecommender(matrix_model, similarity, items_strategy)
    assert_equals(recsys.similarity, similarity)
    assert_equals(recsys.items_selection_strategy, items_strategy)
    assert_equals(recsys.model, matrix_model)
    assert_equals(recsys.capper, True)
Ejemplo n.º 4
0
def test_most_similar_items_ItemBasedRecommender():
    items_strategy = ItemsNeighborhoodStrategy()
    similarity = ItemSimilarity(matrix_model, euclidean_distances)
    recsys = ItemBasedRecommender(matrix_model, similarity, items_strategy)
    #semi items
    assert_array_equal(np.array(['Snakes on a Plane', \
        'The Night Listener', 'Lady in the Water', 'Just My Luck']), \
            recsys.most_similar_items('Superman Returns', 4))
    #all items
    assert_array_equal(np.array(['Lady in the Water', 'You, Me and Dupree', \
     'The Night Listener', 'Snakes on a Plane', 'Superman Returns']), \
            recsys.most_similar_items('Just My Luck'))
    #Non-existing
    assert_raises(ItemNotFoundError, recsys.most_similar_items,
                  'Back to the Future')
    #Exceed the limit
    assert_array_equal(np.array(['Lady in the Water', 'You, Me and Dupree', 'The Night Listener', \
       'Snakes on a Plane', 'Superman Returns']), \
            recsys.most_similar_items('Just My Luck', 20))
    #Empty
    assert_array_equal(np.array([]), \
            recsys.most_similar_items('Just My Luck', 0))

    items_strategy = ItemsNeighborhoodStrategy()
    similarity = ItemSimilarity(boolean_matrix_model, jaccard_coefficient)
    recsys = ItemBasedRecommender(boolean_matrix_model, similarity,
                                  items_strategy)
    #semi items
    assert_array_equal(np.array(['Snakes on a Plane', 'The Night Listener', \
    'You, Me and Dupree', 'Lady in the Water']), \
            recsys.most_similar_items('Superman Returns', 4))
    #all items
    assert_array_equal(np.array(['The Night Listener', 'You, Me and Dupree', \
        'Snakes on a Plane', 'Superman Returns', 'Lady in the Water']), \
            recsys.most_similar_items('Just My Luck'))
    #Non-existing
    assert_raises(ItemNotFoundError, recsys.most_similar_items,
                  'Back to the Future')
    #Exceed the limit
    assert_array_equal(np.array(['The Night Listener', 'You, Me and Dupree', 'Snakes on a Plane',
       'Superman Returns', 'Lady in the Water']), \
            recsys.most_similar_items('Just My Luck', 20))
    #Empty
    assert_array_equal(np.array([]), \
            recsys.most_similar_items('Just My Luck', 0))
Ejemplo n.º 5
0
def test_estimate_preference_ItemBasedRecommender():
    items_strategy = ItemsNeighborhoodStrategy()
    similarity = ItemSimilarity(matrix_model, euclidean_distances)
    recsys = ItemBasedRecommender(matrix_model, similarity, items_strategy)
    assert_almost_equals(
        3.5, recsys.estimate_preference('Marcel Caraciolo',
                                        'Superman Returns'))
    assert_almost_equals(
        3.14717875510,
        recsys.estimate_preference('Leopoldo Pires', 'You, Me and Dupree'))
    #With capper = False
    recsys = ItemBasedRecommender(matrix_model, similarity, items_strategy,
                                  False)
    assert_almost_equals(
        3.14717875510,
        recsys.estimate_preference('Leopoldo Pires', 'You, Me and Dupree'))
    #Non-Preferences
    assert_array_equal(
        np.nan,
        recsys.estimate_preference('Maria Gabriela', 'You, Me and Dupree'))

    items_strategy = ItemsNeighborhoodStrategy()
    similarity = ItemSimilarity(boolean_matrix_model, jaccard_coefficient)
    recsys = ItemBasedRecommender(boolean_matrix_model, similarity,
                                  items_strategy)
    assert_almost_equals(
        1.0, recsys.estimate_preference('Marcel Caraciolo',
                                        'Superman Returns'))
    assert_almost_equals(
        1.0, recsys.estimate_preference('Leopoldo Pires',
                                        'You, Me and Dupree'))
    #With capper = False
    recsys = ItemBasedRecommender(boolean_matrix_model, similarity,
                                  items_strategy, False)
    assert_almost_equals(
        1.0, recsys.estimate_preference('Leopoldo Pires',
                                        'You, Me and Dupree'))
    #Non-Preferences
    assert_array_equal(
        np.NaN,
        recsys.estimate_preference('Maria Gabriela', 'You, Me and Dupree'))
Ejemplo n.º 6
0
def test_recommend_ItemBasedRecommender():
    items_strategy = ItemsNeighborhoodStrategy()
    similarity = ItemSimilarity(matrix_model, euclidean_distances)
    #Empty Recommendation
    recsys = ItemBasedRecommender(matrix_model, similarity, items_strategy)
    assert_array_equal(np.array([]), recsys.recommend('Marcel Caraciolo'))

    #Semi Recommendation
    recsys = ItemBasedRecommender(matrix_model, similarity, items_strategy)
    assert_array_equal(np.array(['Just My Luck', 'You, Me and Dupree']), \
        recsys.recommend('Leopoldo Pires'))

    #Semi Recommendation
    recsys = ItemBasedRecommender(matrix_model, similarity, items_strategy)
    assert_array_equal(np.array(['Just My Luck']), \
        recsys.recommend('Leopoldo Pires', 1))

    #Empty Recommendation
    recsys = ItemBasedRecommender(matrix_model, similarity, items_strategy)
    assert_array_equal(np.array([]), recsys.recommend('Maria Gabriela'))

    #Test with params update
    recsys.recommend(user_id='Maria Gabriela', similarity=similarity)
    assert_array_equal(np.array([]), recsys.recommend('Maria Gabriela'))

    #with_preference
    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
    recsys = ItemBasedRecommender(boolean_matrix_model, similarity,
                                  items_strategy)
    assert_array_equal(np.array([]), recsys.recommend('Marcel Caraciolo'))

    #Semi Recommendation
    recsys = ItemBasedRecommender(boolean_matrix_model, similarity,
                                  items_strategy)
    assert_array_equal(np.array(['You, Me and Dupree', 'Just My Luck']), \
        recsys.recommend('Leopoldo Pires'))

    #Semi Recommendation
    recsys = ItemBasedRecommender(boolean_matrix_model, similarity,
                                  items_strategy)
    assert_array_equal(np.array(['You, Me and Dupree']), \
        recsys.recommend('Leopoldo Pires', 1))

    #Empty Recommendation
    recsys = ItemBasedRecommender(boolean_matrix_model, similarity,
                                  items_strategy)
    assert_array_equal(np.array([]), recsys.recommend('Maria Gabriela'))

    #Test with params update
    recsys.recommend(user_id='Maria Gabriela', similarity=similarity)
    assert_array_equal(np.array([]), recsys.recommend('Maria Gabriela'))

    #with_preference
    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)
Ejemplo n.º 7
0
def test__iter__ItemSimilarity():
    #MATRIXMODEL
    model = MatrixPreferenceDataModel(movies)
    similarity = ItemSimilarity(model, cosine_distances, 3)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), 3)

    similarity = ItemSimilarity(model, pearson_correlation)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), model.items_count())

    similarity = ItemSimilarity(model, manhattan_distances, 0)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), 0)

    similarity = ItemSimilarity(model, manhattan_distances, 20)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), model.items_count())

    #MatrixBooleanPrefDataModel
    model = MatrixBooleanPrefDataModel(movies)
    similarity = ItemSimilarity(model, sorensen_coefficient, 3)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), 3)

    similarity = ItemSimilarity(model, jaccard_coefficient)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), model.items_count())

    similarity = ItemSimilarity(model, loglikehood_coefficient, 0)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), 0)

    similarity = ItemSimilarity(model, sorensen_coefficient, 20)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), model.items_count())
Ejemplo n.º 8
0
def test_get_similarities__ItemSimilarity():
    #MatrixModel
    model = MatrixPreferenceDataModel(movies)

    similarity = ItemSimilarity(model, cosine_distances, 3)

    sim = similarity.get_similarities('Snakes on a Plane')

    assert_equals(len(sim), model.items_count())

    #Pearson Without limits
    similarity = ItemSimilarity(model, pearson_correlation)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, euclidean_distances)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, manhattan_distances, 0)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, manhattan_distances, 20)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    #MatrixBooleanPrefDataModel
    model = MatrixBooleanPrefDataModel(movies)

    similarity = ItemSimilarity(model, jaccard_coefficient, 3)

    sim = similarity.get_similarities('Snakes on a Plane')

    assert_equals(len(sim), model.items_count())

    #Sorensen Without limits
    similarity = ItemSimilarity(model, sorensen_coefficient)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, loglikehood_coefficient)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, loglikehood_coefficient, 0)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, sorensen_coefficient, 20)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())
Ejemplo n.º 9
0
def test_get__item___ItemSimilarity():
    #MATRIXMODEL
    #Cosine #With limits
    model = MatrixPreferenceDataModel(movies)
    similarity = ItemSimilarity(model, cosine_distances, 3)

    assert_array_equal(np.array([[1.]]), similarity['Snakes on a Plane'][0][1])
    assert_equals('Snakes on a Plane', similarity['Snakes on a Plane'][0][0])

    assert_array_almost_equal(np.array([[0.99773877]]),
                              similarity['Snakes on a Plane'][1][1])
    assert_equals('Lady in the Water', similarity['Snakes on a Plane'][1][0])

    assert_array_almost_equal(np.array([[0.9798780]]),
                              similarity['Snakes on a Plane'][2][1])
    assert_equals('Superman Returns', similarity['Snakes on a Plane'][2][0])

    #Pearson Without limits
    similarity = ItemSimilarity(model, pearson_correlation)

    assert_array_equal(np.array([[1.]]),
                       similarity['The Night Listener'][0][1])
    assert_equals('The Night Listener', similarity['The Night Listener'][0][0])

    assert_array_almost_equal(np.array([[0.55555556]]),
                              similarity['The Night Listener'][1][1])
    assert_equals('Just My Luck', similarity['The Night Listener'][1][0])

    assert_array_almost_equal(np.array([[-0.17984719]]),
                              similarity['The Night Listener'][2][1])
    assert_equals('Superman Returns', similarity['The Night Listener'][2][0])

    assert_array_almost_equal(np.array([[-0.25]]),
                              similarity['The Night Listener'][3][1])
    assert_equals('You, Me and Dupree', similarity['The Night Listener'][3][0])

    assert_array_almost_equal(np.array([[-0.56635211]]),
                              similarity['The Night Listener'][4][1])
    assert_equals('Snakes on a Plane', similarity['The Night Listener'][4][0])

    assert_array_almost_equal(np.array([[-0.61237244]]),
                              similarity['The Night Listener'][5][1])
    assert_equals('Lady in the Water', similarity['The Night Listener'][5][0])

    assert_array_almost_equal(np.array([[np.nan]]),
                              similarity['The Night Listener'][6][1])
    assert_equals('Back to the Future', similarity['The Night Listener'][6][0])

    similarity = ItemSimilarity(model, euclidean_distances)

    assert_array_equal(np.array([[1.]]),
                       similarity['The Night Listener'][0][1])
    assert_equals('The Night Listener', similarity['The Night Listener'][0][0])

    assert_array_almost_equal(np.array([[0.38742589]]),
                              similarity['The Night Listener'][1][1])
    assert_equals('Lady in the Water', similarity['The Night Listener'][1][0])

    assert_array_almost_equal(np.array([[0.32037724]]),
                              similarity['The Night Listener'][2][1])
    assert_equals('Snakes on a Plane', similarity['The Night Listener'][2][0])

    assert_array_almost_equal(np.array([[0.29893508]]),
                              similarity['The Night Listener'][3][1])
    assert_equals('Just My Luck', similarity['The Night Listener'][3][0])

    assert_array_almost_equal(np.array([[0.29429806]]),
                              similarity['The Night Listener'][4][1])
    assert_equals('You, Me and Dupree', similarity['The Night Listener'][4][0])

    assert_array_almost_equal(np.array([[0.25265031]]),
                              similarity['The Night Listener'][5][1])
    assert_equals('Superman Returns', similarity['The Night Listener'][5][0])

    assert_array_almost_equal(np.array([[np.nan]]),
                              similarity['The Night Listener'][6][1])
    assert_equals('Back to the Future', similarity['The Night Listener'][6][0])

    similarity = ItemSimilarity(model, manhattan_distances, 0)

    assert_equals([], similarity['Lady in the Water'])

    similarity = ItemSimilarity(model, manhattan_distances, 20)

    assert_array_almost_equal(np.array([[1.]]),
                              similarity['Snakes on a Plane'][0][1])
    assert_equals('Snakes on a Plane', similarity['Snakes on a Plane'][0][0])

    assert_array_almost_equal(np.array([[0.28571429]]),
                              similarity['Snakes on a Plane'][1][1])
    assert_equals('Superman Returns', similarity['Snakes on a Plane'][1][0])

    assert_array_almost_equal(np.array([[0.2]]),
                              similarity['Snakes on a Plane'][2][1])
    assert_equals('Lady in the Water', similarity['Snakes on a Plane'][2][0])

    assert_array_almost_equal(np.array([[0.16666667]]),
                              similarity['Snakes on a Plane'][3][1])
    assert_equals('The Night Listener', similarity['Snakes on a Plane'][3][0])

    assert_array_almost_equal(np.array([[-0.25]]),
                              similarity['Snakes on a Plane'][4][1])
    assert_equals('Just My Luck', similarity['Snakes on a Plane'][4][0])

    assert_array_almost_equal(np.array([[-0.33333333]]),
                              similarity['Snakes on a Plane'][5][1])
    assert_equals('You, Me and Dupree', similarity['Snakes on a Plane'][5][0])

    #MatrixBooleanPrefDataModel
    #Jaccard #With limits
    model = MatrixBooleanPrefDataModel(movies)
    similarity = ItemSimilarity(model, jaccard_coefficient, 3)

    assert_array_equal(np.array([[1.]]), similarity['Snakes on a Plane'][0][1])
    assert_equals('Snakes on a Plane', similarity['Snakes on a Plane'][0][0])

    assert_array_almost_equal(np.array([[1.]]),
                              similarity['Snakes on a Plane'][1][1])
    assert_equals('Superman Returns', similarity['Snakes on a Plane'][1][0])

    assert_array_almost_equal(np.array([[0.85714286]]),
                              similarity['Snakes on a Plane'][2][1])
    assert_equals('The Night Listener', similarity['Snakes on a Plane'][2][0])

    #Sorensen Without limits
    similarity = ItemSimilarity(model, sorensen_coefficient)

    assert_array_equal(np.array([[1.]]),
                       similarity['The Night Listener'][0][1])
    assert_equals('The Night Listener', similarity['The Night Listener'][0][0])

    assert_array_almost_equal(np.array([[0.92307692]]),
                              similarity['The Night Listener'][1][1])
    assert_equals('Snakes on a Plane', similarity['The Night Listener'][1][0])

    assert_array_almost_equal(np.array([[0.92307692]]),
                              similarity['The Night Listener'][2][1])
    assert_equals('Superman Returns', similarity['The Night Listener'][2][0])

    assert_array_almost_equal(np.array([[0.90909091]]),
                              similarity['The Night Listener'][3][1])
    assert_equals('Lady in the Water', similarity['The Night Listener'][3][0])

    assert_array_almost_equal(np.array([[0.83333333]]),
                              similarity['The Night Listener'][4][1])
    assert_equals('You, Me and Dupree', similarity['The Night Listener'][4][0])

    assert_array_almost_equal(np.array([[0.8]]),
                              similarity['The Night Listener'][5][1])
    assert_equals('Just My Luck', similarity['The Night Listener'][5][0])

    assert_array_almost_equal(np.array([[0.]]),
                              similarity['The Night Listener'][6][1])
    assert_equals('Back to the Future', similarity['The Night Listener'][6][0])

    similarity = ItemSimilarity(model, loglikehood_coefficient)

    assert_array_equal(np.array([[1.]]),
                       similarity['The Night Listener'][0][1])
    assert_equals('Snakes on a Plane', similarity['The Night Listener'][0][0])

    assert_array_almost_equal(np.array([[1.]]),
                              similarity['The Night Listener'][1][1])
    assert_equals('Superman Returns', similarity['The Night Listener'][1][0])

    assert_array_almost_equal(np.array([[1.]]),
                              similarity['The Night Listener'][2][1])
    assert_equals('The Night Listener', similarity['The Night Listener'][2][0])

    assert_array_almost_equal(np.array([[0.74804989]]),
                              similarity['The Night Listener'][3][1])
    assert_equals('Lady in the Water', similarity['The Night Listener'][3][0])

    assert_array_almost_equal(np.array([[0.65783229]]),
                              similarity['The Night Listener'][4][1])
    assert_equals('Just My Luck', similarity['The Night Listener'][4][0])

    assert_array_almost_equal(np.array([[0.25087682]]),
                              similarity['The Night Listener'][5][1])
    assert_equals('You, Me and Dupree', similarity['The Night Listener'][5][0])

    assert_array_almost_equal(np.array([[0.]]),
                              similarity['The Night Listener'][6][1])
    assert_equals('Back to the Future', similarity['The Night Listener'][6][0])

    similarity = ItemSimilarity(model, jaccard_coefficient, 0)

    assert_equals([], similarity['Lady in the Water'])

    similarity = ItemSimilarity(model, sorensen_coefficient, 20)

    assert_array_almost_equal(np.array([[1.]]),
                              similarity['Snakes on a Plane'][0][1])
    assert_equals('Snakes on a Plane', similarity['Snakes on a Plane'][0][0])

    assert_array_almost_equal(np.array([[1.]]),
                              similarity['Snakes on a Plane'][1][1])
    assert_equals('Superman Returns', similarity['Snakes on a Plane'][1][0])

    assert_array_almost_equal(np.array([[0.92307692]]),
                              similarity['Snakes on a Plane'][2][1])
    assert_equals('The Night Listener', similarity['Snakes on a Plane'][2][0])

    assert_array_almost_equal(np.array([[0.92307692]]),
                              similarity['Snakes on a Plane'][3][1])
    assert_equals('You, Me and Dupree', similarity['Snakes on a Plane'][3][0])

    assert_array_almost_equal(np.array([[0.8333333333]]),
                              similarity['Snakes on a Plane'][4][1])
    assert_equals('Lady in the Water', similarity['Snakes on a Plane'][4][0])

    assert_array_almost_equal(np.array([[0.72727272]]),
                              similarity['Snakes on a Plane'][5][1])
    assert_equals('Just My Luck', similarity['Snakes on a Plane'][5][0])

    assert_array_almost_equal(np.array([[0.]]),
                              similarity['Snakes on a Plane'][6][1])
    assert_equals('Back to the Future', similarity['Snakes on a Plane'][6][0])
Ejemplo n.º 10
0
def test_get_similarities__ItemSimilarity():
    #MatrixModel
    model = MatrixPreferenceDataModel(movies)

    similarity = ItemSimilarity(model, cosine_distances, 3)

    sim = similarity.get_similarities('Snakes on a Plane')

    assert_equals(len(sim), model.items_count())

    #Pearson Without limits
    similarity = ItemSimilarity(model, pearson_correlation)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, euclidean_distances)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, manhattan_distances, 0)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, manhattan_distances, 20)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    #MatrixBooleanPrefDataModel
    model = MatrixBooleanPrefDataModel(movies)

    similarity = ItemSimilarity(model, jaccard_coefficient, 3)

    sim = similarity.get_similarities('Snakes on a Plane')

    assert_equals(len(sim), model.items_count())

    #Sorensen Without limits
    similarity = ItemSimilarity(model, sorensen_coefficient)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, loglikehood_coefficient)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, loglikehood_coefficient, 0)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, sorensen_coefficient, 20)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())